Class: Larynx::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/larynx/prompt.rb

Overview

The prompt class neatly wraps up a convention where you prompt for input of certain length. The prompt waits until the required input length is reached, the user presses the terminator button or the time runs out. Think of the play_and_get_digits command except it works for speak as well. It also provides a bargein option to allow or prevent the user from interrupting the speech or playback.

Pass a block to the method as a callback which receives input as an argument.

Constant Summary collapse

COMMAND_OPTIONS =
[:play, :speak, :phrase]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call, options, &block) ⇒ Prompt

Returns a new instance of Prompt.



18
19
20
21
22
# File 'lib/larynx/prompt.rb', line 18

def initialize(call, options, &block)
  @call, @options, @block = call, options, block
  @options.reverse_merge!(:bargein => true, :timeout => 10, :interdigit_timeout => 3, :termchar => '#')
  raise NoPromptCommandValue, "No output command value supplied. Use one of playback, speak or phrase keys." if command_name.blank?
end

Instance Attribute Details

#callObject (readonly)

Returns the value of attribute call.



14
15
16
# File 'lib/larynx/prompt.rb', line 14

def call
  @call
end

Instance Method Details

#add_digit_timerObject



95
96
97
98
99
100
# File 'lib/larynx/prompt.rb', line 95

def add_digit_timer
  call.add_timer(:digit, interdigit_timeout) {
    call.cancel_timer :input
    finalise
  }
end

#add_input_timerObject



102
103
104
105
106
107
# File 'lib/larynx/prompt.rb', line 102

def add_input_timer
  call.add_timer(:input, timeout) {
    call.cancel_timer :digit
    finalise
  }
end

#commandObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/larynx/prompt.rb', line 24

def command
  @command ||= AppCommand.new(command_name, message, :bargein => @options[:bargein]).
    before { call.clear_input unless @options[:bargein] }.
    after  {
      call.clear_input unless @options[:bargein]
      if prompt_finished?
        finalise
      else
        call.add_observer self
        add_digit_timer
        add_input_timer
      end
    }
end

#command_nameObject



67
68
69
# File 'lib/larynx/prompt.rb', line 67

def command_name
  (COMMAND_OPTIONS & @options.keys).first.to_s
end

#dtmf_received(digit) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/larynx/prompt.rb', line 86

def dtmf_received(digit)
  if prompt_finished?
    call.stop_timer(:input)
    call.cancel_timer(:digit)
  else
    call.restart_timer(:digit)
  end
end

#finaliseObject



80
81
82
83
84
# File 'lib/larynx/prompt.rb', line 80

def finalise
  call.remove_observer self
  @block.arity == 2 ? @block.call(input, valid_length?) : @block.call(input)
  call.clear_input
end

#inputObject



39
40
41
# File 'lib/larynx/prompt.rb', line 39

def input
  (call.input.last == termchar ? call.input[0..-2] : call.input).join
end

#interdigit_timeoutObject



59
60
61
# File 'lib/larynx/prompt.rb', line 59

def interdigit_timeout
  @options[:interdigit_timeout]
end

#maximum_lengthObject



55
56
57
# File 'lib/larynx/prompt.rb', line 55

def maximum_length
  @options[:max_length] || @options[:length]
end

#messageObject



71
72
73
# File 'lib/larynx/prompt.rb', line 71

def message
  @options[command_name.to_sym]
end

#minimum_lengthObject



51
52
53
# File 'lib/larynx/prompt.rb', line 51

def minimum_length
  @options[:min_length] || @options[:length] || 1
end

#prompt_finished?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/larynx/prompt.rb', line 43

def prompt_finished?
  call.input.last == termchar || call.input.size == maximum_length
end

#termcharObject



47
48
49
# File 'lib/larynx/prompt.rb', line 47

def termchar
  @options[:termchar]
end

#timeoutObject



63
64
65
# File 'lib/larynx/prompt.rb', line 63

def timeout
  @options[:timeout]
end

#valid_length?Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/larynx/prompt.rb', line 75

def valid_length?
  length = input.size
  length >= minimum_length && length <= (maximum_length || length)
end