Class: Larynx::CallHandler

Inherits:
EventMachine::Protocols::HeaderAndContentProtocol
  • Object
show all
Includes:
Commands, Observable
Defined in:
lib/larynx/call_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#answer, #break!, #connect, #filter, #hangup, #linger, #myevents, #phrase, #playback, #prompt, #read, #speak

Methods included from Observable

#add_observer, #clear_observers!, #notify_current_observer, #notify_observers, #remove_observer

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def input
  @input
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def last_command
  @last_command
end

#observersObject (readonly)

Returns the value of attribute observers.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def observers
  @observers
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def response
  @response
end

#sessionObject (readonly)

Returns the value of attribute session.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def session
  @session
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/larynx/call_handler.rb', line 6

def state
  @state
end

Instance Method Details

#add_timer(name, timeout, &block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/larynx/call_handler.rb', line 54

def add_timer(name, timeout, &block)
  @timers[name] = [RestartableTimer.new(timeout) {
    timer = @timers.delete(name)
    timer[1].call if timer[1]
    notify_observers :timed_out
    send_next_command if @state == :ready
  }, block]
end

#called_numberObject



23
24
25
# File 'lib/larynx/call_handler.rb', line 23

def called_number
  @session[:caller_destination_number]
end

#caller_idObject



27
28
29
# File 'lib/larynx/call_handler.rb', line 27

def caller_id
  @session[:caller_caller_id_number]
end

#cancel_all_timersObject



71
72
73
74
# File 'lib/larynx/call_handler.rb', line 71

def cancel_all_timers
  @timers.values.each {|t| t[0].cancel }
  @timers = {}
end

#cancel_timer(name) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/larynx/call_handler.rb', line 63

def cancel_timer(name)
  if timer = @timers[name]
    timer[0].cancel
    @timers.delete(name)
    send_next_command if @state == :ready
  end
end

#cleanupObject



167
168
169
170
171
172
# File 'lib/larynx/call_handler.rb', line 167

def cleanup
  break! if @state == :executing
  cancel_all_timers
  clear_observers!
  @session = nil
end

#clear_inputObject



31
32
33
# File 'lib/larynx/call_handler.rb', line 31

def clear_input
  @input = []
end

#command_to_sendObject



156
157
158
# File 'lib/larynx/call_handler.rb', line 156

def command_to_send
  current_command.try(:interrupted?) ? next_command : current_command
end

#current_commandObject



35
36
37
# File 'lib/larynx/call_handler.rb', line 35

def current_command
  @queue.first
end

#execute(command, immediately = false) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/larynx/call_handler.rb', line 43

def execute(command, immediately=false)
  log "Queued: #{command.name}"
  if immediately
    @queue.unshift command
    send_next_command
  else
    @queue << command
  end
  command
end

#finalize_commandObject



149
150
151
152
153
154
# File 'lib/larynx/call_handler.rb', line 149

def finalize_command
  if command = @queue.shift
    command.finalize unless command.interrupted?
    @last_command = command
  end
end

#handle_dtmfObject



135
136
137
138
139
140
# File 'lib/larynx/call_handler.rb', line 135

def handle_dtmf
  @input << @response.body[:dtmf_digit]
  interrupt_command
  notify_observers :dtmf_received, @response.body[:dtmf_digit]
  send_next_command if @state == :ready
end

#handle_requestObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/larynx/call_handler.rb', line 98

def handle_request
  case
  when @response.reply? && current_command.is_a?(CallCommand)
    log "Completed: #{current_command.name}"
    finalize_command
    @state = :ready
    send_next_command
  when @response.answered?
    log 'Answered call'
    finalize_command
    @state = :ready
    Larynx.fire_callback(:answer, self)
    send_next_command if @state == :ready
  when @response.executing?
    log "Executing: #{current_command.name}"
    current_command.setup
    @state = :executing
  when @response.executed? && current_command
    this_command = current_command
    finalize_command
    unless this_command.interrupted?
      current_command.finalize if this_command.command == 'break'
      @state = :ready
      send_next_command
    end
  when @response.dtmf?
    log "Button pressed: #{@response.body[:dtmf_digit]}"
    handle_dtmf
  when @response.disconnect?
    log "Disconnected."
    cleanup
    notify_observers :hungup
    Larynx.fire_callback(:hungup, self)
    @state = :waiting
  end
end

#interrupt_commandObject



142
143
144
145
146
147
# File 'lib/larynx/call_handler.rb', line 142

def interrupt_command
  if @state == :executing && current_command.interruptable?
    current_command.interrupted = true
    break!
  end
end

#log(msg) ⇒ Object



174
175
176
# File 'lib/larynx/call_handler.rb', line 174

def log(msg)
  LARYNX_LOGGER.info msg
end

#next_commandObject



39
40
41
# File 'lib/larynx/call_handler.rb', line 39

def next_command
  @queue[1]
end

#post_initObject

EM hook which is run when call is received



9
10
11
12
13
# File 'lib/larynx/call_handler.rb', line 9

def post_init
  @queue, @input, @timers = [], [], {}
  connect { start_session }
  send_next_command
end

#receive_request(header, content) ⇒ Object



93
94
95
96
# File 'lib/larynx/call_handler.rb', line 93

def receive_request(header, content)
  @response = Response.new(header, content)
  handle_request
end

#restart_timer(name) ⇒ Object



87
88
89
90
91
# File 'lib/larynx/call_handler.rb', line 87

def restart_timer(name)
  if timer = @timers[name]
    timer[0].restart
  end
end

#send_next_commandObject



160
161
162
163
164
165
# File 'lib/larynx/call_handler.rb', line 160

def send_next_command
  if command = command_to_send
    @state = :sending
    send_data command.to_s
  end
end

#start_sessionObject



15
16
17
18
19
20
21
# File 'lib/larynx/call_handler.rb', line 15

def start_session
  @session = Session.new(@response.header)
  log "Call received from #{caller_id}"
  myevents
  linger
  Larynx.fire_callback(:connect, self)
end

#stop_timer(name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/larynx/call_handler.rb', line 76

def stop_timer(name)
  if timer = @timers[name]
    # only run callback if it was actually cancelled (i.e. returns false)
    if timer[0].cancel == false && timer[1]
      timer[1].call
    end
    @timers.delete(name)
    send_next_command if @state == :ready
  end
end