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.
Constant Summary collapse
- COMMAND_OPTIONS =
[:play, :speak, :phrase]
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.
18 19 20 21 22 |
# File 'lib/larynx/prompt.rb', line 18 def initialize(call, , &block) @call, @options, @block = call, , 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
#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
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_timer ⇒ Object
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 |
#command ⇒ Object
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, , :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_name ⇒ Object
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 |
#finalise ⇒ Object
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 |
#input ⇒ Object
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_timeout ⇒ Object
59 60 61 |
# File 'lib/larynx/prompt.rb', line 59 def interdigit_timeout @options[:interdigit_timeout] end |
#maximum_length ⇒ Object
55 56 57 |
# File 'lib/larynx/prompt.rb', line 55 def maximum_length @options[:max_length] || @options[:length] end |
#message ⇒ Object
71 72 73 |
# File 'lib/larynx/prompt.rb', line 71 def @options[command_name.to_sym] end |
#minimum_length ⇒ Object
51 52 53 |
# File 'lib/larynx/prompt.rb', line 51 def minimum_length @options[:min_length] || @options[:length] || 1 end |
#prompt_finished? ⇒ Boolean
43 44 45 |
# File 'lib/larynx/prompt.rb', line 43 def prompt_finished? call.input.last == termchar || call.input.size == maximum_length end |
#termchar ⇒ Object
47 48 49 |
# File 'lib/larynx/prompt.rb', line 47 def termchar @options[:termchar] end |
#timeout ⇒ Object
63 64 65 |
# File 'lib/larynx/prompt.rb', line 63 def timeout @options[:timeout] end |
#valid_length? ⇒ 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 |