Class: Matrioska::AppRunner

Inherits:
Object
  • Object
show all
Includes:
Adhearsion::CallController::Utility
Defined in:
lib/matrioska/app_runner.rb

Constant Summary collapse

VALID_DIGITS =
/[^0-9*#]/

Instance Method Summary collapse

Constructor Details

#initialize(call) ⇒ AppRunner

Returns a new instance of AppRunner


7
8
9
10
11
# File 'lib/matrioska/app_runner.rb', line 7

def initialize(call)
  @call = call
  register_runner_with_call
  @app_map = {}
end

Instance Method Details

#handle_input_complete(event) ⇒ Object


68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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?Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/matrioska/app_runner.rb', line 33

def running?
  !!(@component && @component.executing?)
end

#startObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/matrioska/app_runner.rb', line 41

def started?
  @state == :started
end

#statusObject


37
38
39
# File 'lib/matrioska/app_runner.rb', line 37

def status
  @state
end

#stop!Object


28
29
30
31
# File 'lib/matrioska/app_runner.rb', line 28

def stop!
  @state = :stopped
  @component.stop! if running?
end

#stopped?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/matrioska/app_runner.rb', line 45

def stopped?
  @state == :stopped
end