Class: Adhearsion::DialPlan::ConfirmationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call) ⇒ ConfirmationManager

Returns a new instance of ConfirmationManager.



43
44
45
46
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 43

def initialize(call)
  @call = call
  extend Adhearsion::VoIP::Commands.for(call.originating_voip_platform)
end

Instance Attribute Details

#callObject (readonly)

Returns the value of attribute call.



42
43
44
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 42

def call
  @call
end

Class Method Details

.confirmation_call?(call) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 24

def confirmation_call?(call)
  call.variables.has_key?(:network_script) && call.variables[:network_script].starts_with?('confirm!')
end

.decode_hash(encoded_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 28

def decode_hash(encoded_hash)
  encoded_hash = encoded_hash =~ /^M\((.+)\)$/ ? $1 : encoded_hash
  encoded_hash = encoded_hash =~ /^([^:]+\^)?(.+)$/ ? $2 : encoded_hash # Remove the macro name if it's there
  unencoded = URI.unescape(encoded_hash).split('!')
  unencoded.shift unless unencoded.first.include?(':')
  unencoded = unencoded.map { |pair| key, value = pair.split(':'); [key.to_sym ,value] }.flatten
  Hash[*unencoded].tap do |hash|
    hash[:timeout]    &&= hash[:timeout].to_i
    hash[:play]       &&= hash[:play].split('++')
  end
end

.encode_hash_for_dial_macro_argument(options) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 7

def encode_hash_for_dial_macro_argument(options)
  options    = options.clone
  macro_name = options.delete :macro
  options[:play] &&= options[:play].kind_of?(Array) ? options[:play].join('++') : options[:play]
  encoded_options = URI.escape options.map { |key,value| "#{key}:#{value}" }.join('!')
  "M(#{macro_name}^#{encoded_options})".tap do |str|
    if str.rindex('^') != str.index('^')
      raise ArgumentError, "You seem to have supplied a :confirm option with a caret (^) in it!" +
                           " Please remove it. This will blow Asterisk up."
    end
  end
end

.handle(call) ⇒ Object



20
21
22
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 20

def handle(call)
  new(call).handle
end

Instance Method Details

#handleObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb', line 48

def handle
  variables = self.class.decode_hash call.variables[:network_script]

  answer
  loop do
    response = interruptible_play(*variables[:play])
    if response && response.to_s == variables[:key].to_s
      # Don't set a variable to pass through to dial()
      break
    elsif response && response.to_s != variables[:key].to_s
      next
    else
      response = wait_for_digit variables[:timeout]
      if response
        if response.to_s == variables[:key].to_s
          # Don't set a variable to pass through to dial()
          break
        else
          next
        end
      else
        # By setting MACRO_RESULT to CONTINUE, we cancel the dial.
        variable 'MACRO_RESULT' => "CONTINUE"
        break
      end
    end
  end

end