class Matrioska::AppRunner

Constants

VALID_DIGITS

Public Class Methods

new(call) click to toggle source
# File lib/matrioska/app_runner.rb, line 7
def initialize(call)
  @call = call
  register_runner_with_call
  @app_map = {}
end

Public Instance Methods

handle_input_complete(event) click to toggle source
# File lib/matrioska/app_runner.rb, line 68
def handle_input_complete(event)
  if @state == :stopped
    logger.warn "Stopped runner #{self} received stop event."
    return
  end
  logger.debug "MATRIOSKA HANDLING INPUT"
  result = event.reason.respond_to?(:utterance) ? event.reason.utterance : nil
  digit = parse_dtmf result
  match_and_run digit
end
map_app(pattern, controller = nil, &block) click to toggle source
# File lib/matrioska/app_runner.rb, line 49
def map_app(pattern, controller = nil, &block)
  pattern = pattern.to_s
  range = "1234567890*#"

  if VALID_DIGITS.match(pattern)
    raise ArgumentError, "The first argument should be a String or number containing only 1234567890*#"
  end

  payload = if block_given?
    raise ArgumentError, "You cannot specify both a block and a controller name." if controller.is_a? Class
    block
  else
    raise ArgumentError, "You need to provide a block or a controller name." unless controller.is_a? Class
    controller
  end

  @app_map[pattern] = payload
end
running?() click to toggle source
# File lib/matrioska/app_runner.rb, line 33
def running?
  !!(@component && @component.executing?)
end
start() click to toggle source
# File lib/matrioska/app_runner.rb, line 13
def start
  if started? && running?
    logger.warn "Already-active runner #{self} received start event."
    return
  end

  @state = :started
  logger.debug "MATRIOSKA STARTING LISTENER"
  @component = Punchblock::Component::Input.new mode: :dtmf, inter_digit_timeout: Adhearsion.config[:matrioska].timeout * 1_000, grammar: { value: build_grammar }
  @component.register_event_handler Punchblock::Event::Complete do |event|
    handle_input_complete event
  end
  @call.write_and_await_response @component if @call.alive? && @call.active?
end
started?() click to toggle source
# File lib/matrioska/app_runner.rb, line 41
def started?
  @state == :started
end
status() click to toggle source
# File lib/matrioska/app_runner.rb, line 37
def status
  @state
end
stop!() click to toggle source
# File lib/matrioska/app_runner.rb, line 28
def stop!
  @state = :stopped
  @component.stop! if running?
end
stopped?() click to toggle source
# File lib/matrioska/app_runner.rb, line 45
def stopped?
  @state == :stopped
end

Private Instance Methods

app_map() click to toggle source
# File lib/matrioska/app_runner.rb, line 81
def app_map
  @app_map
end
build_grammar() click to toggle source
# File lib/matrioska/app_runner.rb, line 115
def build_grammar
  current_app_map = app_map
  RubySpeech::GRXML.draw mode: :dtmf, root: 'options' do
    rule id: 'options', scope: 'public' do
      one_of do
        current_app_map.keys.each do |index|
          item do
            index
          end
        end
      end
    end
  end
end
match_and_run(digit) click to toggle source
# File lib/matrioska/app_runner.rb, line 85
def match_and_run(digit)
  if match = @app_map[digit]
    logger.debug "MATRIOSKA #match_and_run called with #{digit}"
    callback = lambda do |call|
      @running = false
      if call.alive? && call.active? && started?
        logger.debug "MATRIOSKA CALLBACK RESTARTING LISTENER"
        start
      else
        logger.debug "MATRIOSKA CALLBACK NOT DOING ANYTHING BECAUSE CALL IS DEAD"
      end
    end

    if match.is_a? Proc
      logger.debug "MATRIOSKA EXECUTING #{match} AS BLOCK"
      @call.execute_controller(nil, callback, &match)
    end

    if match.is_a? Class
      payload = match.new(@call)
      logger.debug "MATRIOSKA EXECUTING #{payload.to_s} AS CONTROLLER"
      @call.execute_controller(payload, callback)
    end
  else
    start
  end
rescue Adhearsion::Call::Hangup
  logger.debug "Matrioska terminated because the call was disconnected"
end
register_runner_with_call() click to toggle source
# File lib/matrioska/app_runner.rb, line 130
def register_runner_with_call
  @call[:matrioska_runners] ||= []
  @call[:matrioska_runners] << self
end