Class: Adhearsion::Call

Inherits:
Object show all
Includes:
Celluloid, HasGuardedHandlers
Defined in:
lib/adhearsion/call.rb

Overview

Encapsulates call-related data and behavior.

Direct Known Subclasses

OutboundCall

Constant Summary

Hangup =
Class.new Adhearsion::Error
CommandTimeout =
Class.new Adhearsion::Error
ExpiredError =
Class.new Celluloid::DeadActorError

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Celluloid

logger

Constructor Details

- (Call) initialize(offer = nil)

A new instance of Call



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/adhearsion/call.rb', line 36

def initialize(offer = nil)
  register_initial_handlers

  @tags         = []
  @commands     = CommandRegistry.new
  @variables    = {}
  @controllers  = []
  @end_reason   = nil

  self << offer if offer
end

Instance Attribute Details

- (Object) variables

Returns the value of attribute variables



31
32
33
# File 'lib/adhearsion/call.rb', line 31

def variables
  @variables
end

Class Method Details

+ (Object) new(*args, &block)



18
19
20
21
22
23
24
25
26
# File 'lib/adhearsion/call.rb', line 18

def self.new(*args, &block)
  super.tap do |proxy|
    def proxy.method_missing(*args)
      super
    rescue Celluloid::DeadActorError
      raise ExpiredError, "This call is expired and is no longer accessible"
    end
  end
end

Instance Method Details

- (Object) accept(headers = nil)



153
154
155
# File 'lib/adhearsion/call.rb', line 153

def accept(headers = nil)
  @accept_command ||= write_and_await_response Punchblock::Command::Accept.new(:headers => headers)
end

- (Boolean) active?

If the call is currently active or not (disconnected)

Returns:

  • (Boolean)

    if the call is currently active or not (disconnected)



149
150
151
# File 'lib/adhearsion/call.rb', line 149

def active?
  !end_reason
end

- (Object) answer(headers = nil)



157
158
159
# File 'lib/adhearsion/call.rb', line 157

def answer(headers = nil)
  write_and_await_response Punchblock::Command::Answer.new(:headers => headers)
end

- (Object) deliver_message(message) Also known as: <<



94
95
96
97
# File 'lib/adhearsion/call.rb', line 94

def deliver_message(message)
  logger.debug "Receiving message: #{message.inspect}"
  catching_standard_errors { trigger_handler :event, message }
end

- (Object) execute_controller(controller, completion_callback = nil)



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/adhearsion/call.rb', line 291

def execute_controller(controller, completion_callback = nil)
  call = current_actor
  Thread.new do
    catching_standard_errors do
      begin
        CallController.exec controller
      ensure
        completion_callback.call call if completion_callback
      end
    end
  end.tap { |t| Adhearsion::Process.important_threads << t }
end

- (Object) hangup(headers = nil)



165
166
167
168
169
170
# File 'lib/adhearsion/call.rb', line 165

def hangup(headers = nil)
  return false unless active?
  logger.info "Hanging up"
  @end_reason = true
  write_and_await_response Punchblock::Command::Hangup.new(:headers => headers)
end

- (String?) id

The globally unique ID for the call

Returns:

  • (String, nil)

    The globally unique ID for the call



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

def id
  offer.target_call_id if offer
end

- (Object) join(target, options = {})

Joins this call to another call or a mixer

Parameters:

  • target (Call, String, Hash)

    the target to join to. May be a Call object, a call ID (String, Hash) or a mixer name (Hash)

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

    further options to be joined with

Options Hash (target):

  • call_id (String)

    The call ID to join to

  • mixer_name (String)

    The mixer to join to



185
186
187
188
# File 'lib/adhearsion/call.rb', line 185

def join(target, options = {})
  command = Punchblock::Command::Join.new options.merge(join_options_with_target(target))
  write_and_await_response command
end

- (Object) mute



231
232
233
# File 'lib/adhearsion/call.rb', line 231

def mute
  write_and_await_response ::Punchblock::Command::Mute.new
end

- (Object) on_end(&block)



139
140
141
142
143
144
# File 'lib/adhearsion/call.rb', line 139

def on_end(&block)
  register_event_handler Punchblock::Event::End do |event|
    block.call event
    throw :pass
  end
end

- (Object) register_event_handler(*guards, &block)



90
91
92
# File 'lib/adhearsion/call.rb', line 90

def register_event_handler(*guards, &block)
  register_handler :event, *guards, &block
end

- (Object) reject(reason = :busy, headers = nil)



161
162
163
# File 'lib/adhearsion/call.rb', line 161

def reject(reason = :busy, headers = nil)
  write_and_await_response Punchblock::Command::Reject.new(:reason => reason, :headers => headers)
end

- (Object) remove_tag(label)

Remove a label

Parameters:

  • label (String, Symbol)


77
78
79
# File 'lib/adhearsion/call.rb', line 77

def remove_tag(label)
  @tags.reject! { |tag| tag == label }
end

- (Object) tag(label)

Tag a call with an arbitrary label

Parameters:

  • label (String, Symbol)

    String or Symbol with which to tag this call



67
68
69
70
# File 'lib/adhearsion/call.rb', line 67

def tag(label)
  abort ArgumentError.new "Tag must be a String or Symbol" unless [String, Symbol].include?(label.class)
  @tags << label
end

- (Boolean) tagged_with?(label)

Establish if the call is tagged with the provided label

Parameters:

  • label (String, Symbol)

Returns:

  • (Boolean)


86
87
88
# File 'lib/adhearsion/call.rb', line 86

def tagged_with?(label)
  @tags.include? label
end

- (Array) tags

The set of labels with which this call has been tagged.

Returns:

  • (Array)

    The set of labels with which this call has been tagged.



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

def tags
  @tags.clone
end

- (Object) unjoin(target)

Unjoins this call from another call or a mixer

Parameters:

  • target (Call, String, Hash)

    the target to unjoin from. May be a Call object, a call ID (String, Hash) or a mixer name (Hash)

Options Hash (target):

  • call_id (String)

    The call ID to unjoin from

  • mixer_name (String)

    The mixer to unjoin from



197
198
199
200
# File 'lib/adhearsion/call.rb', line 197

def unjoin(target)
  command = Punchblock::Command::Unjoin.new join_options_with_target(target)
  write_and_await_response command
end

- (Object) unmute



235
236
237
# File 'lib/adhearsion/call.rb', line 235

def unmute
  write_and_await_response ::Punchblock::Command::Unmute.new
end

- (Object) wait_for_joined(expected_target)



217
218
219
220
221
222
# File 'lib/adhearsion/call.rb', line 217

def wait_for_joined(expected_target)
  target = nil
  until target == expected_target do
    target = wait :joined
  end
end

- (Object) wait_for_unjoined(expected_target)



224
225
226
227
228
229
# File 'lib/adhearsion/call.rb', line 224

def wait_for_unjoined(expected_target)
  target = nil
  until target == expected_target do
    target = wait :unjoined
  end
end