Class: Adhearsion::OutboundCall

Inherits:
Call show all
Defined in:
lib/adhearsion/outbound_call.rb

Constant Summary

Constants inherited from Call

Call::CommandTimeout, Call::ExpiredError, Call::Hangup

Instance Attribute Summary collapse

Attributes inherited from Call

#after_hangup_lifetime, #auto_hangup, #controllers, #end_code, #end_reason, #end_time, #start_time, #variables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Call

#active?, #commands, #deliver_message, #duration, #execute_controller, #from, #hangup, #initialize, #join, #mute, #on_end, #on_joined, #on_unjoined, #peers, #redirect, #register_event_handler, #remove_tag, #send_message, #tag, #tagged_with?, #tags, #terminating!, #terminating?, #to, #unjoin, #unmute, uri, #uri, #wait_for_end, #wait_for_joined, #wait_for_unjoined

Methods included from Celluloid

logger

Constructor Details

This class inherits a constructor from Adhearsion::Call

Instance Attribute Details

#dial_commandObject (readonly)

Returns the value of attribute dial_command.



7
8
9
# File 'lib/adhearsion/outbound_call.rb', line 7

def dial_command
  @dial_command
end

Class Method Details

.originate(to, opts = {}) { ... } ⇒ OutboundCall

Create a new outbound call

By default, the call will enter the router when it is answered, similar to incoming calls. Alternatively, a controller may be specified.

Parameters:

  • to (String)

    the URI of the party to dial

  • opts (Hash) (defaults to: {})

    modifier options

Options Hash (opts):

  • :controller (Class)

    the controller to execute when the call is answered

  • :controller_metadata (Hash)

    key-value pairs of metadata to set on the controller

Yields:

  • Call controller routine in block form

Returns:

See Also:



27
28
29
30
31
32
# File 'lib/adhearsion/outbound_call.rb', line 27

def originate(to, opts = {}, &controller_block)
  new.tap do |call|
    call.execute_controller_or_router_on_answer opts.delete(:controller), opts.delete(:controller_metadata), &controller_block
    call.dial to, opts
  end
end

Instance Method Details

#accept(*args) ⇒ Object



55
56
# File 'lib/adhearsion/outbound_call.rb', line 55

def accept(*args)
end

#answer(*args) ⇒ Object



58
59
# File 'lib/adhearsion/outbound_call.rb', line 58

def answer(*args)
end

#clientObject



51
52
53
# File 'lib/adhearsion/outbound_call.rb', line 51

def client
  PunchblockPlugin::Initializer.client
end

#dial(to, options = {}) ⇒ Object

Dial out an existing outbound call

Parameters:

  • to (String)

    the URI of the party to dial

  • options (Hash) (defaults to: {})

    modifier options

Options Hash (options):

  • :from (String, Optional)

    what to set the Caller ID to

  • :timeout (Integer, Optional)

    in seconds

  • :headers (Hash, Optional)

    SIP headers to attach to the new call.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/adhearsion/outbound_call.rb', line 74

def dial(to, options = {})
  options = options.dup
  options[:to] = to
  if options[:timeout]
    wait_timeout = options[:timeout]
    options[:timeout] = options[:timeout] * 1000
  else
    wait_timeout = 60
  end

  uri = client.new_call_uri
  options[:uri] = uri

  @dial_command = Punchblock::Command::Dial.new(options)

  ref = Punchblock::Ref.new uri: uri
  @transport = ref.scheme
  @id = ref.call_id
  @domain = ref.domain

  Adhearsion.active_calls << current_actor

  write_and_await_response(@dial_command, wait_timeout, true).tap do |dial_command|
    if @dial_command.uri != self.uri
      logger.warn "Requested call URI (#{uri}) was not respected. Tracking by new URI #{self.uri}. This might cause a race in event handling, please upgrade your Rayo server."
      Adhearsion.active_calls << current_actor
      Adhearsion.active_calls.delete(@id)
    end
    Adhearsion::Events.trigger_immediately :call_dialed, current_actor
  end
rescue
  clear_from_active_calls
  raise
end

#domainObject



43
44
45
46
47
48
49
# File 'lib/adhearsion/outbound_call.rb', line 43

def domain
  if dial_command
    dial_command.domain || @domain
  else
    @domain
  end
end

#execute_controller_or_router_on_answer(controller, metadata = {}, &controller_block) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/adhearsion/outbound_call.rb', line 131

def execute_controller_or_router_on_answer(controller,  = {}, &controller_block)
  if controller || controller_block
    route = Router::Route.new 'inbound', controller, &controller_block
    route. = 
    on_answer { route.dispatch current_actor }
  else
    run_router_on_answer
  end
end

#idObject



35
36
37
38
39
40
41
# File 'lib/adhearsion/outbound_call.rb', line 35

def id
  if dial_command
    dial_command.target_call_id || @id
  else
    @id
  end
end

#on_answer(&block) ⇒ Object



127
128
129
# File 'lib/adhearsion/outbound_call.rb', line 127

def on_answer(&block)
  register_event_handler Punchblock::Event::Answered, &block
end

#reject(*args) ⇒ Object



61
62
# File 'lib/adhearsion/outbound_call.rb', line 61

def reject(*args)
end

#run_routerObject



115
116
117
118
119
# File 'lib/adhearsion/outbound_call.rb', line 115

def run_router
  catching_standard_errors do
    Adhearsion.router.handle current_actor
  end
end

#run_router_on_answerObject



121
122
123
124
125
# File 'lib/adhearsion/outbound_call.rb', line 121

def run_router_on_answer
  register_event_handler Punchblock::Event::Answered do |event|
    run_router
  end
end