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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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, 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



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_timerObject



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

#commandObject



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, message, :bargein => @options[:bargein]).
    before { call.clear_input }.
    after  {
      if prompt_finished?
        finalise
      else
        call.add_observer self
        add_digit_timer
        add_input_timer
      end
    }
end

#command_nameObject



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

def command_name
  ([:play, :speak, :phrase] & @options.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

#finaliseObject



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

#inputObject



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_timeoutObject



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

def interdigit_timeout
  @options[:interdigit_timeout]
end

#maximum_lengthObject



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

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

#messageObject



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

def message
  @options[command_name.to_sym]
end

#minimum_lengthObject



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

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

#prompt_finished?Boolean

Returns:

  • (Boolean)


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

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

#termcharObject



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

def termchar
  @options[:termchar]
end

#timeoutObject



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

def timeout
  @options[:timeout]
end

#valid_length?Boolean

Returns:

  • (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