Class: RubyAMI::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ami/action.rb

Constant Summary

CAUSAL_EVENT_NAMES =
%w[queuestatus sippeers iaxpeers parkedcalls dahdishowchannels coreshowchannels
dbget status agents konferencelist confbridgelist confbridgelistrooms]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, headers = {}, &block) ⇒ Action



8
9
10
11
12
13
14
15
16
# File 'lib/ruby_ami/action.rb', line 8

def initialize(name, headers = {}, &block)
  @name       = name.to_s.downcase.freeze
  @headers    = headers.freeze
  @action_id  = RubyAMI.new_uuid
  @response   = nil
  @complete   = false
  @events     = []
  @callback   = block
end

Instance Attribute Details

#action_idObject (readonly)

Returns the value of attribute action_id



3
4
5
# File 'lib/ruby_ami/action.rb', line 3

def action_id
  @action_id
end

#headersObject (readonly)

Returns the value of attribute headers



3
4
5
# File 'lib/ruby_ami/action.rb', line 3

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/ruby_ami/action.rb', line 3

def name
  @name
end

#responseObject

Returns the value of attribute response



3
4
5
# File 'lib/ruby_ami/action.rb', line 3

def response
  @response
end

Instance Method Details

#<<(message) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_ami/action.rb', line 67

def <<(message)
  case message
  when Error
    self.response = message
    complete!
  when Event
    raise StandardError, 'This action should not trigger events. Maybe it is now a causal action? This is most likely a bug in RubyAMI' unless has_causal_events?
    response.events << message
    complete! if message.name.downcase == causal_event_terminator_name
  when Response
    self.response = message
    complete! unless has_causal_events?
  end
  self
end

#causal_event_terminator_nameString

Used to determine the event name for an action which has causal events.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby_ami/action.rb', line 44

def causal_event_terminator_name
  return unless has_causal_events?
  case name
  when "sippeers", "iaxpeers"
    "peerlistcomplete"
  when "konferencelist"
    "conferencelistcomplete"
  else
    name + "complete"
  end
end

#complete?Boolean



18
19
20
# File 'lib/ruby_ami/action.rb', line 18

def complete?
  @complete
end

#eql?(other) ⇒ Boolean Also known as: ==



83
84
85
# File 'lib/ruby_ami/action.rb', line 83

def eql?(other)
  to_s == other.to_s
end

#has_causal_events?String

When sending an action with “causal events” (i.e. events which must be collected to form a proper response), AMI should send a particular event which instructs us that no more events will be sent. This event is called the “causal event terminator”.

Note: you must supply both the name of the event and any headers because it's possible that some uses of an action (i.e. same name, different headers) have causal events while other uses don't.



34
35
36
# File 'lib/ruby_ami/action.rb', line 34

def has_causal_events?
  CAUSAL_EVENT_NAMES.include? name
end

#to_sObject

Converts this action into a protocol-valid String, ready to be sent over a socket.



59
60
61
62
63
64
65
# File 'lib/ruby_ami/action.rb', line 59

def to_s
  @textual_representation ||= (
      "Action: #{@name}\r\nActionID: #{@action_id}\r\n" +
      @headers.map { |(key,value)| "#{key}: #{value}" }.join("\r\n") +
      (@headers.any? ? "\r\n\r\n" : "\r\n")
  )
end