Class: RubyAMI::Action
Defined Under Namespace
Classes: UnsupportedActionName
Constant Summary collapse
- CAUSAL_EVENT_NAMES =
%w[queuestatus sippeers iaxpeers parkedcalls dahdishowchannels coreshowchannels dbget status agents konferencelist confbridgelist confbridgelistrooms]
Instance Attribute Summary collapse
-
#action_id ⇒ Object
readonly
Returns the value of attribute action_id.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #<<(message) ⇒ Object
-
#causal_event_terminator_name ⇒ String
Used to determine the event name for an action which has causal events.
- #eql?(other) ⇒ Boolean (also: #==)
- #events ⇒ Object
-
#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.
-
#initialize(name, headers = {}, &block) ⇒ Action
constructor
A new instance of Action.
- #replies_with_action_id? ⇒ Boolean
-
#response(timeout = nil) ⇒ Object
If the response has simply not been received yet from Asterisk, the calling Thread will block until it comes in.
- #response=(other) ⇒ Object
- #sync_timeout ⇒ Object
-
#to_s ⇒ Object
Converts this action into a protocol-valid String, ready to be sent over a socket.
Constructor Details
#initialize(name, headers = {}, &block) ⇒ Action
Returns a new instance of Action.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_ami/action.rb', line 10 def initialize(name, headers = {}, &block) @name = name.to_s.downcase.freeze @headers = headers.freeze @action_id = RubyAMI.new_uuid @response = FutureResource.new @response_callback = block @state = :new @events = [] @event_lock = Mutex.new end |
Instance Attribute Details
#action_id ⇒ Object (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 |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
3 4 5 |
# File 'lib/ruby_ami/action.rb', line 3 def headers @headers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/ruby_ami/action.rb', line 3 def name @name end |
#state ⇒ Object
Returns the value of attribute state.
5 6 7 |
# File 'lib/ruby_ami/action.rb', line 5 def state @state end |
Instance Method Details
#<<(message) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ruby_ami/action.rb', line 93 def <<() case when Error self.response = 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? @event_lock.synchronize do @events << end self.response = @pending_response if .name.downcase == causal_event_terminator_name when Response if has_causal_events? @pending_response = else self.response = end end end |
#causal_event_terminator_name ⇒ String
Used to determine the event name for an action which has causal events.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_ami/action.rb', line 51 def causal_event_terminator_name return unless has_causal_events? case name when "sippeers", "iaxpeers" "peerlistcomplete" when "dbget" "dbgetresponse" when "konferencelist" "conferencelistcomplete" else name + "complete" end end |
#eql?(other) ⇒ Boolean Also known as: ==
118 119 120 |
# File 'lib/ruby_ami/action.rb', line 118 def eql?(other) to_s == other.to_s end |
#events ⇒ Object
112 113 114 115 116 |
# File 'lib/ruby_ami/action.rb', line 112 def events @event_lock.synchronize do @events.dup end 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.
41 42 43 |
# File 'lib/ruby_ami/action.rb', line 41 def has_causal_events? CAUSAL_EVENT_NAMES.include? name end |
#replies_with_action_id? ⇒ Boolean
25 26 27 |
# File 'lib/ruby_ami/action.rb', line 25 def replies_with_action_id? !UnsupportedActionName::UNSUPPORTED_ACTION_NAMES.include? name end |
#response(timeout = nil) ⇒ Object
If the response has simply not been received yet from Asterisk, the calling Thread will block until it comes in. Once the response comes in, subsequent calls immediately return a reference to the ManagerInterfaceResponse object.
81 82 83 84 85 |
# File 'lib/ruby_ami/action.rb', line 81 def response(timeout = nil) @response.resource(timeout).tap do |resp| raise resp if resp.is_a? Exception end end |
#response=(other) ⇒ Object
87 88 89 90 91 |
# File 'lib/ruby_ami/action.rb', line 87 def response=(other) @state = :complete @response.resource = other @response_callback.call other if @response_callback end |
#sync_timeout ⇒ Object
123 124 125 |
# File 'lib/ruby_ami/action.rb', line 123 def sync_timeout name.downcase == 'originate' && !headers[:async] ? 60 : 10 end |
#to_s ⇒ Object
Converts this action into a protocol-valid String, ready to be sent over a socket.
68 69 70 71 72 73 74 |
# File 'lib/ruby_ami/action.rb', line 68 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 |