Class: ElectricSlide::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/electric_slide/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Agent

Returns a new instance of Agent.

Parameters:

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

    Agent parameters

Options Hash (opts):

  • :id (String)

    The Agent’s ID

  • :address (String)

    The Agent’s contact address

  • :presence (Symbol)

    The Agent’s current presence. Must be one of :available, :on_call, :after_call, :unavailable



12
13
14
15
16
# File 'lib/electric_slide/agent.rb', line 12

def initialize(opts = {})
  @id = opts[:id]
  @address = opts[:address]
  @presence = opts[:presence] || :available
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/electric_slide/agent.rb', line 5

def address
  @address
end

#callObject

Returns the value of attribute call.



5
6
7
# File 'lib/electric_slide/agent.rb', line 5

def call
  @call
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/electric_slide/agent.rb', line 5

def id
  @id
end

#presenceObject (readonly)

Returns the value of attribute presence.



6
7
8
# File 'lib/electric_slide/agent.rb', line 6

def presence
  @presence
end

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/electric_slide/agent.rb', line 5

def queue
  @queue
end

Class Method Details

.on_connect(&block) ⇒ Object

Provide a block to be called when this agent is connected to a caller The block will be passed the queue, the agent call and the client call



47
48
49
# File 'lib/electric_slide/agent.rb', line 47

def self.on_connect(&block)
  @connect_callback = block
end

.on_connection_failed(&block) ⇒ Object

Provide a block to be called when the agent connection to the callee fails The block will be passed the queue, the agent call and the client call



65
66
67
# File 'lib/electric_slide/agent.rb', line 65

def self.on_connection_failed(&block)
  @connection_failed_callback = block
end

.on_disconnect(&block) ⇒ Object

Provide a block to be called when this agent is disconnected to a caller The block will be passed the queue, the agent call and the client call



53
54
55
# File 'lib/electric_slide/agent.rb', line 53

def self.on_disconnect(&block)
  @disconnect_callback = block
end

.on_presence_change(&block) ⇒ Object

Provide a block to be called when this agent’s presence changes The block will be passed the queue, the agent call, and the new presence



59
60
61
# File 'lib/electric_slide/agent.rb', line 59

def self.on_presence_change(&block)
  @presence_change_callback = block
end

Instance Method Details

#callable?Boolean

Returns ‘true` if the agent can be called, i.e., has a contact address

Returns:

  • (Boolean)


89
90
91
# File 'lib/electric_slide/agent.rb', line 89

def callable?
  address.present?
end

#callback(type, *args) ⇒ Object



40
41
42
43
# File 'lib/electric_slide/agent.rb', line 40

def callback(type, *args)
  callback = self.class.instance_variable_get "@#{type}_callback"
  instance_exec *args, &callback if callback && callback.respond_to?(:call)
end

#dial_options_for(queue, queued_call) ⇒ Object

Called to provide options for calling this agent that are passed to #dial



74
75
76
# File 'lib/electric_slide/agent.rb', line 74

def dial_options_for(queue, queued_call)
  {}
end

#fromObject

FIXME: Use delegator?



84
85
86
# File 'lib/electric_slide/agent.rb', line 84

def from
  @call.from
end

#join(queued_call) ⇒ Object



78
79
80
81
# File 'lib/electric_slide/agent.rb', line 78

def join(queued_call)
  # For use in queues that need bridge connections
  @call.join queued_call
end

#on_call?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/electric_slide/agent.rb', line 69

def on_call?
  @presence == :on_call
end

#update(attrs = {}) ⇒ Agent

Updates an agent instance’s attributes.

Parameters:

  • opts (Hash)

    Agent attributes

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/electric_slide/agent.rb', line 21

def update(attrs = {})
  unless Hash === attrs
    raise ArgumentError.new('Agent attributes must be a hash')
  end

  attrs.each do |attr, value|
    setter = "#{attr}="
    send setter, value
  end

  self
end

#update_presence(new_presence, extra_params = {}) ⇒ Object



34
35
36
37
38
# File 'lib/electric_slide/agent.rb', line 34

def update_presence(new_presence, extra_params = {})
  old_presence = @presence
  @presence = new_presence
  callback :presence_change, queue, @call, new_presence, old_presence, extra_params
end