Class: Adhearsion::VoIP::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/menu_state_machine/menu_class.rb

Defined Under Namespace

Modules: MenuGetAnotherDigit Classes: ClearableStringBuffer, MenuGetAnotherDigitOrFinish, MenuGetAnotherDigitOrTimeout, MenuResult, MenuResultFound, MenuResultInvalid

Constant Summary collapse

DEFAULT_MAX_NUMBER_OF_TRIES =
1
DEFAULT_TIMEOUT =

seconds

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|@builder| ... } ⇒ Menu

Returns a new instance of Menu.

Yields:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 12

def initialize(options={}, &block)
  @tries_count         = 0 # Counts the number of tries the menu's been executed

  @timeout             = options[:timeout] || DEFAULT_TIMEOUT
  @max_number_of_tries = options[:tries]   || DEFAULT_MAX_NUMBER_OF_TRIES

  @builder = menu_builder.new
  yield @builder

  initialize_digit_buffer
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



11
12
13
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 11

def builder
  @builder
end

#max_number_of_triesObject (readonly)

Returns the value of attribute max_number_of_tries.



11
12
13
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 11

def max_number_of_tries
  @max_number_of_tries
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



11
12
13
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 11

def timeout
  @timeout
end

#tries_countObject (readonly)

Returns the value of attribute tries_count.



11
12
13
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 11

def tries_count
  @tries_count
end

Instance Method Details

#<<(other) ⇒ Object



24
25
26
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 24

def <<(other)
  digit_buffer << other
end

#continueObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 40

def continue
  raise MenuGetAnotherDigitOrTimeout if digit_buffer_empty?

  calculated_matches = builder.calculate_matches_for digit_buffer_string

  if calculated_matches.exact_match_count >= 1
    first_exact_match = calculated_matches.exact_matches.first
    if calculated_matches.potential_match_count.zero?
      # Match found with no extenuating ambiguities! Go with the first exact match
      menu_result_found! first_exact_match.match_payload, digit_buffer_string
    else
      get_another_digit_or_finish!(first_exact_match.match_payload, first_exact_match.query)
    end
  elsif calculated_matches.potential_match_count >= 1
    get_another_digit_or_timeout!
  else
    invalid!
  end
end

#digit_bufferObject



28
29
30
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 28

def digit_buffer
  @digit_buffer
end

#digit_buffer_empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 36

def digit_buffer_empty?
  digit_buffer.empty?
end

#digit_buffer_stringObject



32
33
34
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 32

def digit_buffer_string
  digit_buffer.to_s
end

#execute_failure_hookObject



77
78
79
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 77

def execute_failure_hook
  builder.execute_hook_for(:failure, digit_buffer_string)
end

#execute_invalid_hookObject



69
70
71
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 69

def execute_invalid_hook
  builder.execute_hook_for(:invalid, digit_buffer_string)
end

#execute_timeout_hookObject



73
74
75
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 73

def execute_timeout_hook
  builder.execute_hook_for(:premature_timeout, digit_buffer_string)
end

#restart!Object



64
65
66
67
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 64

def restart!
  @tries_count += 1
  digit_buffer.clear!
end

#should_continue?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/adhearsion/voip/menu_state_machine/menu_class.rb', line 60

def should_continue?
  tries_count < max_number_of_tries
end