class AdhearsionCpa::ToneDetector

Attributes

timeout[RW]
tones[RW]

Public Class Methods

new(controller) click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 6
def initialize(controller)
  @controller = controller
end

Public Instance Methods

detect_tones(tones, options) { |event| ... } click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 10
def detect_tones(tones, options)
  @tones = tones
  process options

  if async?
    component.register_event_handler Punchblock::Component::Input::Signal do |event|
      yield event if block_given?
    end

    component.register_event_handler Punchblock::Event::Complete do |event|
      yield event.reason if block_given? && event.reason.is_a?(Punchblock::Component::Input::Signal)
    end
  end

  call.write_and_await_response component if call_alive?

  if async?
    call.after(timeout) do
      if component_running?
        component.stop!
      end
    end if timeout

    component
  else
    component.complete_event(timeout).reason
  end
rescue Timeout::Error
  component.stop! if component_running?
  nil
end

Private Instance Methods

async?() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 83
def async?
  @async
end
build_grammar_for(tone, opts={}) click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 69
def build_grammar_for(tone, opts={})
  opts.merge! @options
  ns_url = "#{Punchblock::BASE_RAYO_NAMESPACE}:cpa:#{tone}:#{Punchblock::RAYO_VERSION}"
  opts.each_with_index do |(k, v), i|
    if i == 0
      ns_url << "?#{k}=#{v}"
    else
      ns_url << ";#{k}=#{v}"
    end
  end

  Punchblock::Component::Input::Grammar.new url: ns_url
end
call() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 87
def call
  @controller.call
end
call_alive?() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 91
def call_alive?
  call && call.active?
end
component() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 52
def component
  @component ||= Punchblock::Component::Input.new mode: :cpa, grammars: tone_grammars
end
component_running?() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 95
def component_running?
  component && component.executing?
end
process(opts) click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 44
def process(opts)
  @async   = opts.delete :async
  @timeout = opts.delete :timeout
  opts[:terminate] == false ? opts.delete(:terminate) : opts[:terminate] = true

  @options = opts
end
tone_grammars() click to toggle source
# File lib/adhearsion_cpa/tone_detector.rb, line 56
def tone_grammars
  tones.map do |tone_object|
    if tone_object.is_a? Hash
      tone_object.map do |tone, individual_options|
        combined_options = @options.merge individual_options
        build_grammar_for tone, combined_options
      end
    else
      build_grammar_for tone_object
    end
  end.flatten
end