Class: Larynx::Prompt
- Inherits:
-
Object
- Object
- Larynx::Prompt
- 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.
Instance Attribute Summary collapse
-
#call ⇒ Object
readonly
Returns the value of attribute call.
Instance Method Summary collapse
- #add_digit_timer ⇒ Object
- #add_input_timer ⇒ Object
- #command ⇒ Object
- #command_name ⇒ Object
- #dtmf_received(digit) ⇒ Object
- #finalise ⇒ Object
-
#initialize(call, options, &block) ⇒ Prompt
constructor
A new instance of Prompt.
- #input ⇒ Object
- #interdigit_timeout ⇒ Object
- #maximum_length ⇒ Object
- #message ⇒ Object
- #minimum_length ⇒ Object
- #prompt_finished? ⇒ Boolean
- #termchar ⇒ Object
- #timeout ⇒ Object
- #valid_length? ⇒ Boolean
Constructor Details
#initialize(call, options, &block) ⇒ Prompt
Returns a new instance of Prompt.
16 17 18 19 20 |
# File 'lib/larynx/prompt.rb', line 16 def initialize(call, , &block) @call, , @block = call, , block .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
#call ⇒ Object (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_timer ⇒ Object
92 93 94 95 96 97 |
# File 'lib/larynx/prompt.rb', line 92 def add_digit_timer call.add_timer(:digit, interdigit_timeout) { call.cancel_timer :input finalise } end |
#add_input_timer ⇒ Object
99 100 101 102 103 104 |
# File 'lib/larynx/prompt.rb', line 99 def add_input_timer call.add_timer(:input, timeout) { call.cancel_timer :digit finalise } end |
#command ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/larynx/prompt.rb', line 22 def command @command ||= AppCommand.new(command_name, , :bargein => [:bargein]). before { call.clear_input }. after { if prompt_finished? finalise else call.add_observer self add_digit_timer add_input_timer end } end |
#command_name ⇒ Object
64 65 66 |
# File 'lib/larynx/prompt.rb', line 64 def command_name ([:play, :speak, :phrase] & .keys).first.to_s end |
#dtmf_received(digit) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/larynx/prompt.rb', line 83 def dtmf_received(digit) if prompt_finished? call.stop_timer(:input) call.cancel_timer(:digit) else call.restart_timer(:digit) end end |
#finalise ⇒ Object
77 78 79 80 81 |
# File 'lib/larynx/prompt.rb', line 77 def finalise call.remove_observer self @block.arity == 2 ? @block.call(input, valid_length?) : @block.call(input) call.clear_input end |
#input ⇒ Object
36 37 38 |
# File 'lib/larynx/prompt.rb', line 36 def input (call.input.last == termchar ? call.input[0..-2] : call.input).join end |
#interdigit_timeout ⇒ Object
56 57 58 |
# File 'lib/larynx/prompt.rb', line 56 def interdigit_timeout [:interdigit_timeout] end |
#maximum_length ⇒ Object
52 53 54 |
# File 'lib/larynx/prompt.rb', line 52 def maximum_length [:max_length] || [:length] end |
#message ⇒ Object
68 69 70 |
# File 'lib/larynx/prompt.rb', line 68 def [command_name.to_sym] end |
#minimum_length ⇒ Object
48 49 50 |
# File 'lib/larynx/prompt.rb', line 48 def minimum_length [:min_length] || [:length] || 1 end |
#prompt_finished? ⇒ Boolean
40 41 42 |
# File 'lib/larynx/prompt.rb', line 40 def prompt_finished? call.input.last == termchar || call.input.size == maximum_length end |
#termchar ⇒ Object
44 45 46 |
# File 'lib/larynx/prompt.rb', line 44 def termchar [:termchar] end |
#timeout ⇒ Object
60 61 62 |
# File 'lib/larynx/prompt.rb', line 60 def timeout [:timeout] end |
#valid_length? ⇒ Boolean
72 73 74 75 |
# File 'lib/larynx/prompt.rb', line 72 def valid_length? length = input.size length >= minimum_length && length <= (maximum_length || length) end |