Class: Punchblock::Translator::Asterisk::Component::Input

Inherits:
Component show all
Defined in:
lib/punchblock/translator/asterisk/component/input.rb

Instance Attribute Summary collapse

Attributes inherited from Component

#call, #id, #internal

Instance Method Summary collapse

Methods inherited from Component

#call_id, #execute_command, #initialize, #logger_id, #send_complete_event, #send_event

Constructor Details

This class inherits a constructor from Punchblock::Translator::Asterisk::Component::Component

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



7
8
9
# File 'lib/punchblock/translator/asterisk/component/input.rb', line 7

def buffer
  @buffer
end

#grammarObject (readonly)

Returns the value of attribute grammar.



7
8
9
# File 'lib/punchblock/translator/asterisk/component/input.rb', line 7

def grammar
  @grammar
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/punchblock/translator/asterisk/component/input.rb', line 14

def execute
  @call.answer_if_not_answered
  initial_timeout = @component_node.initial_timeout || -1
  @inter_digit_timeout = @component_node.inter_digit_timeout || -1

  return with_error 'option error', 'A grammar document is required.' unless @component_node.grammar
  return with_error 'option error', 'A mode value other than DTMF is unsupported on Asterisk.' unless @component_node.mode == :dtmf
  return with_error 'option error', 'An initial timeout value that is negative (and not -1) is invalid.' unless initial_timeout >= -1
  return with_error 'option error', 'An inter-digit timeout value that is negative (and not -1) is invalid.' unless @inter_digit_timeout >= -1

  send_ref

  case @media_engine
  when :asterisk, nil
    @grammar = @component_node.grammar.value.clone
    grammar.inline!
    grammar.tokenize!
    grammar.normalize_whitespace

    begin_initial_timer initial_timeout/1000 unless initial_timeout == -1

    component = current_actor

    @active = true

    call.register_handler :ami, :name => 'DTMF' do |event|
      component.process_dtmf! event['Digit'] if event['End'] == 'Yes'
    end
  end
end

#process_dtmf(digit) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/punchblock/translator/asterisk/component/input.rb', line 45

def process_dtmf(digit)
  return unless @active
  pb_logger.trace "Processing incoming DTMF digit #{digit}"
  buffer << digit
  cancel_initial_timer
  case (match = grammar.match buffer.dup)
  when RubySpeech::GRXML::Match
    pb_logger.trace "Found a match against buffer #{buffer}"
    complete success_reason(match)
  when RubySpeech::GRXML::NoMatch
    pb_logger.trace "Buffer #{buffer} does not match grammar"
    complete Punchblock::Component::Input::Complete::NoMatch.new
  when RubySpeech::GRXML::PotentialMatch
    pb_logger.trace "Buffer #{buffer} potentially matches grammar. Waiting..."
    reset_inter_digit_timer
  end
end

#setupObject



9
10
11
12
# File 'lib/punchblock/translator/asterisk/component/input.rb', line 9

def setup
  @media_engine = call.translator.media_engine
  @buffer       = ""
end