Class: Librevox::Listener::Outbound

Inherits:
Base
  • Object
show all
Includes:
Applications
Defined in:
lib/librevox/listener/outbound.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#response

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Applications

#answer, #att_xfer, #bind_meta_app, #bridge, #deflect, #export, #gentones, #hangup, #multiset, #park, #play_and_get_digits, #playback, #pre_answer, #read, #record, #redirect, #respond, #set, #transfer, #unbind_meta_app, #unset

Methods inherited from Base

#api, #disconnect, event, hooks, #on_event, #receive_message, #send_message

Constructor Details

#initialize(connection) ⇒ Outbound

Returns a new instance of Outbound.



42
43
44
45
46
# File 'lib/librevox/listener/outbound.rb', line 42

def initialize(connection)
  super(connection)
  @session = nil
  @app_complete_queue = Async::Queue.new
end

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



36
37
38
# File 'lib/librevox/listener/outbound.rb', line 36

def session
  @session
end

Class Method Details

.run(barrier, host: "localhost", port: 8084, **options) ⇒ Object



10
11
12
13
14
# File 'lib/librevox/listener/outbound.rb', line 10

def self.run(barrier, host: "localhost", port: 8084, **options)
  endpoint = IO::Endpoint.tcp(host, port, **options)
  server = Server.new(self, endpoint)
  barrier.async { server.run }
end

Instance Method Details

#application(app, args = nil, **params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/librevox/listener/outbound.rb', line 16

def application(app, args = nil, **params)
  variable_name = params.delete(:variable)

  headers = params
    .merge(
      event_lock:        true,
      call_command:      "execute",
      execute_app_name:  app,
      execute_app_arg:   args,
    )
    .map { |key, value| "#{key.to_s.tr('_', '-')}: #{value}" }

  send_message "sendmsg\n#{headers.join("\n")}"

  response = @app_complete_queue.dequeue
  @session = response.content

  variable(variable_name) if variable_name
end

#handle_responseObject



55
56
57
58
59
60
61
62
63
# File 'lib/librevox/listener/outbound.rb', line 55

def handle_response
  if response.event? && response.event == "CHANNEL_DATA"
    @session = response.content
  elsif response.event? && response.event == "CHANNEL_EXECUTE_COMPLETE"
    @app_complete_queue.push(response)
  end

  super
end

#run_sessionObject



48
49
50
51
52
53
# File 'lib/librevox/listener/outbound.rb', line 48

def run_session
  @session = send_message("connect").headers
  send_message "myevents"
  send_message "linger"
  session_initiated
end

#session_initiatedObject

Called when a new session is initiated.



39
40
# File 'lib/librevox/listener/outbound.rb', line 39

def session_initiated
end

#update_sessionObject



69
70
71
72
# File 'lib/librevox/listener/outbound.rb', line 69

def update_session
  response = api.command "uuid_dump", session[:unique_id]
  @session = response.content
end

#variable(name) ⇒ Object



65
66
67
# File 'lib/librevox/listener/outbound.rb', line 65

def variable(name)
  session[:"variable_#{name}"]
end