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

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



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

- (Object) action_id (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

- (Object) headers (readonly)

Returns the value of attribute headers



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

def headers
  @headers
end

- (Object) name (readonly)

Returns the value of attribute name



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

def name
  @name
end

- (Object) response

Returns the value of attribute response



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

def response
  @response
end

Instance Method Details

- (Object) <<(message)



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

- (String) causal_event_terminator_name

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

- (Boolean) complete?



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

def complete?
  @complete
end

- (Boolean) eql?(other) Also known as: ==



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

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

- (String) has_causal_events?

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

- (Object) to_s

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