punchblock 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +4 -0
- data/lib/punchblock.rb +15 -13
- data/lib/punchblock/client.rb +1 -0
- data/lib/punchblock/component.rb +1 -1
- data/lib/punchblock/connection/asterisk.rb +2 -2
- data/lib/punchblock/connection/xmpp.rb +8 -9
- data/lib/punchblock/core_ext/ruby.rb +24 -0
- data/lib/punchblock/has_headers.rb +4 -0
- data/lib/punchblock/translator/asterisk.rb +40 -9
- data/lib/punchblock/translator/asterisk/call.rb +107 -3
- data/lib/punchblock/translator/asterisk/component.rb +9 -3
- data/lib/punchblock/translator/asterisk/component/asterisk.rb +14 -0
- data/lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb +84 -0
- data/lib/punchblock/translator/asterisk/component/asterisk/ami_action.rb +96 -0
- data/lib/punchblock/version.rb +1 -1
- data/spec/punchblock/connection/xmpp_spec.rb +3 -1
- data/spec/punchblock/translator/asterisk/call_spec.rb +206 -0
- data/spec/punchblock/translator/asterisk/component/asterisk/agi_command_spec.rb +143 -0
- data/spec/punchblock/translator/asterisk/component/asterisk/ami_action_spec.rb +159 -0
- data/spec/punchblock/translator/asterisk_spec.rb +90 -10
- metadata +46 -41
- data/lib/punchblock/translator/asterisk/ami_action.rb +0 -86
- data/spec/punchblock/translator/asterisk/ami_action_spec.rb +0 -149
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
module Punchblock
|
|
2
|
-
module Translator
|
|
3
|
-
class Asterisk
|
|
4
|
-
class AMIAction < Component
|
|
5
|
-
attr_reader :action
|
|
6
|
-
|
|
7
|
-
def initialize(component_node, ami_client)
|
|
8
|
-
@component_node, @ami_client = component_node, ami_client
|
|
9
|
-
@action = create_action
|
|
10
|
-
@id = @action.action_id
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def execute
|
|
14
|
-
send_action
|
|
15
|
-
send_ref
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
|
|
20
|
-
def create_action
|
|
21
|
-
headers = {}
|
|
22
|
-
@component_node.params_hash.each_pair do |key, value|
|
|
23
|
-
headers[key.to_s.capitalize] = value
|
|
24
|
-
end
|
|
25
|
-
RubyAMI::Action.new @component_node.name, headers do |response|
|
|
26
|
-
handle_response response
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def send_action
|
|
31
|
-
@ami_client.send_action @action
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def send_ref
|
|
35
|
-
@component_node.response = Ref.new :id => @action.action_id
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def handle_response(response)
|
|
39
|
-
case response
|
|
40
|
-
when RubyAMI::Error
|
|
41
|
-
send_event error_event(response)
|
|
42
|
-
when RubyAMI::Response
|
|
43
|
-
send_events
|
|
44
|
-
send_event complete_event(response)
|
|
45
|
-
end
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def error_event(response)
|
|
49
|
-
Punchblock::Event::Complete.new.tap do |c|
|
|
50
|
-
c.reason = Punchblock::Event::Complete::Error.new :details => response.message
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def complete_event(response)
|
|
55
|
-
headers = response.headers
|
|
56
|
-
headers.merge! @extra_complete_attributes if @extra_complete_attributes
|
|
57
|
-
headers.delete 'ActionID'
|
|
58
|
-
Punchblock::Event::Complete.new.tap do |c|
|
|
59
|
-
c.reason = Punchblock::Component::Asterisk::AMI::Action::Complete::Success.new :message => headers.delete('Message'), :attributes => headers
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def send_events
|
|
64
|
-
return unless @action.has_causal_events?
|
|
65
|
-
@action.events.each do |e|
|
|
66
|
-
if e.name.downcase == @action.causal_event_terminator_name
|
|
67
|
-
@extra_complete_attributes = e.headers
|
|
68
|
-
else
|
|
69
|
-
send_event pb_event_from_ami_event(e)
|
|
70
|
-
end
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def pb_event_from_ami_event(ami_event)
|
|
75
|
-
headers = ami_event.headers
|
|
76
|
-
headers.delete 'ActionID'
|
|
77
|
-
Event::Asterisk::AMI::Event.new :name => ami_event.name, :attributes => headers
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
def send_event(event)
|
|
81
|
-
@component_node.add_event event
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
end
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
module Punchblock
|
|
4
|
-
module Translator
|
|
5
|
-
class Asterisk
|
|
6
|
-
describe AMIAction do
|
|
7
|
-
let(:mock_ami_client) { mock 'RubyAMI::Client' }
|
|
8
|
-
|
|
9
|
-
let :command do
|
|
10
|
-
Punchblock::Component::Asterisk::AMI::Action.new :name => 'ExtensionStatus', :params => { :context => 'default', :exten => 'idonno' }
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
subject { AMIAction.new command, mock_ami_client }
|
|
14
|
-
|
|
15
|
-
let :expected_action do
|
|
16
|
-
RubyAMI::Action.new 'ExtensionStatus', 'Context' => 'default', 'Exten' => 'idonno'
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
context 'initial execution' do
|
|
20
|
-
let(:component_id) { UUIDTools::UUID.random_create }
|
|
21
|
-
|
|
22
|
-
let :expected_response do
|
|
23
|
-
Ref.new :id => component_id
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
before { UUIDTools::UUID.stubs :random_create => component_id }
|
|
27
|
-
|
|
28
|
-
it 'should send the appropriate RubyAMI::Action and send the component node a ref with the action ID' do
|
|
29
|
-
mock_ami_client.expects(:send_action).once.with(expected_action).returns(expected_action)
|
|
30
|
-
command.expects(:response=).once.with(expected_response)
|
|
31
|
-
subject.execute
|
|
32
|
-
end
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
context 'when the AMI action completes' do
|
|
36
|
-
before do
|
|
37
|
-
command.request!
|
|
38
|
-
command.execute!
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
let :response do
|
|
42
|
-
RubyAMI::Response.new.tap do |r|
|
|
43
|
-
r['ActionID'] = "552a9d9f-46d7-45d8-a257-06fe95f48d99"
|
|
44
|
-
r['Message'] = 'Channel status will follow'
|
|
45
|
-
r["Exten"] = "idonno"
|
|
46
|
-
r["Context"] = "default"
|
|
47
|
-
r["Hint"] = ""
|
|
48
|
-
r["Status"] = "-1"
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
let :expected_complete_reason do
|
|
53
|
-
Punchblock::Component::Asterisk::AMI::Action::Complete::Success.new :message => 'Channel status will follow', :attributes => {:exten => "idonno", :context => "default", :hint => "", :status => "-1"}
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
context 'for a non-causal action' do
|
|
57
|
-
it 'should send a complete event to the component node' do
|
|
58
|
-
subject.action.response = response
|
|
59
|
-
|
|
60
|
-
command.should be_complete
|
|
61
|
-
|
|
62
|
-
command.complete_event.resource(0.5).reason.should == expected_complete_reason
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
context 'for a causal action' do
|
|
67
|
-
let :command do
|
|
68
|
-
Punchblock::Component::Asterisk::AMI::Action.new :name => 'CoreShowChannels'
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
let :expected_action do
|
|
72
|
-
RubyAMI::Action.new 'CoreShowChannels'
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
let :event do
|
|
76
|
-
RubyAMI::Event.new('CoreShowChannel').tap do |e|
|
|
77
|
-
e['ActionID'] = "552a9d9f-46d7-45d8-a257-06fe95f48d99"
|
|
78
|
-
e['Channel'] = 'SIP/127.0.0.1-00000013'
|
|
79
|
-
e['UniqueID'] = '1287686437.19'
|
|
80
|
-
e['Context'] = 'adhearsion'
|
|
81
|
-
e['Extension'] = '23432'
|
|
82
|
-
e['Priority'] = '2'
|
|
83
|
-
e['ChannelState'] = '6'
|
|
84
|
-
e['ChannelStateDesc'] = 'Up'
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
let :terminating_event do
|
|
89
|
-
RubyAMI::Event.new('CoreShowChannelsComplete').tap do |e|
|
|
90
|
-
e['EventList'] = 'Complete'
|
|
91
|
-
e['ListItems'] = '3'
|
|
92
|
-
e['ActionID'] = 'umtLtvSg-RN5n-GEay-Z786-YdiaSLNXkcYN'
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
let :event_node do
|
|
97
|
-
Punchblock::Event::Asterisk::AMI::Event.new :name => 'CoreShowChannel', :attributes => {
|
|
98
|
-
:channel => 'SIP/127.0.0.1-00000013',
|
|
99
|
-
:uniqueid => '1287686437.19',
|
|
100
|
-
:context => 'adhearsion',
|
|
101
|
-
:extension => '23432',
|
|
102
|
-
:priority => '2',
|
|
103
|
-
:channelstate => '6',
|
|
104
|
-
:channelstatedesc => 'Up'
|
|
105
|
-
}
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
let :expected_complete_reason do
|
|
109
|
-
Punchblock::Component::Asterisk::AMI::Action::Complete::Success.new :message => 'Channel status will follow', :attributes => {:exten => "idonno", :context => "default", :hint => "", :status => "-1", :eventlist => 'Complete', :listitems => '3'}
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
it 'should send events to the component node' do
|
|
113
|
-
command.register_event_handler Punchblock::Event::Asterisk::AMI::Event do |event|
|
|
114
|
-
@event = event
|
|
115
|
-
end
|
|
116
|
-
subject.action << event
|
|
117
|
-
subject.action << response
|
|
118
|
-
subject.action << terminating_event
|
|
119
|
-
@event.should == event_node
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
it 'should send a complete event to the component node' do
|
|
123
|
-
subject.action << response
|
|
124
|
-
subject.action << terminating_event
|
|
125
|
-
|
|
126
|
-
command.complete_event.resource(0.5).reason.should == expected_complete_reason
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
context 'with an error' do
|
|
131
|
-
let :error do
|
|
132
|
-
RubyAMI::Error.new.tap { |e| e.message = 'Action failed' }
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
let :expected_complete_reason do
|
|
136
|
-
Punchblock::Event::Complete::Error.new :details => 'Action failed'
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
it 'should send a complete event to the component node' do
|
|
140
|
-
subject.action << error
|
|
141
|
-
|
|
142
|
-
command.complete_event.resource(0.5).reason.should == expected_complete_reason
|
|
143
|
-
end
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
end
|