librevox 0.2.1 → 0.3

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.
@@ -1,5 +1,5 @@
1
- require 'spec/helper'
2
- require 'spec/librevox/listener'
1
+ require './spec/helper'
2
+ require './spec/librevox/listener'
3
3
 
4
4
  require 'librevox/listener/inbound'
5
5
 
@@ -1,11 +1,11 @@
1
- require 'spec/helper'
2
- require 'spec/librevox/listener'
1
+ require './spec/helper'
2
+ require './spec/librevox/listener'
3
3
 
4
4
  require 'librevox/listener/outbound'
5
5
 
6
6
  module Librevox::Applications
7
- def sample_app(name, *args, &b)
8
- execute_app name, args.join(" "), &b
7
+ def sample_app name, *args
8
+ application name, args.join(" ")
9
9
  end
10
10
  end
11
11
 
@@ -15,22 +15,28 @@ class OutboundTestListener < Librevox::Listener::Outbound
15
15
  end
16
16
  end
17
17
 
18
- def receive_event_and_linger_replies
19
- @listener.receive_data("Content-Type: command/reply\nReply-Text: +OK Events Enabled\n\n")
20
- @listener.receive_data("Content-Type: command/reply\nReply-Text: +OK will linger\n\n")
18
+ def event_and_linger_replies
19
+ command_reply "Reply-Text" => "+OK Events Enabled"
20
+ command_reply "Reply-Text" => "+OK will linger"
21
21
  end
22
22
 
23
23
  describe "Outbound listener" do
24
+ extend Librevox::Test::Matchers
25
+
24
26
  before do
25
27
  @listener = OutboundTestListener.new(nil)
26
- @listener.receive_data("Content-Type: command/reply\nCaller-Caller-ID-Number: 8675309\n\n")
27
- receive_event_and_linger_replies
28
+ command_reply(
29
+ "Caller-Caller-Id-Number" => "8675309",
30
+ "Unique-ID" => "1234",
31
+ "variable_some_var" => "some value"
32
+ )
33
+ event_and_linger_replies
28
34
  end
29
35
 
30
36
  should "connect to freeswitch and subscribe to events" do
31
- @listener.outgoing_data.shift.should.equal "connect\n\n"
32
- @listener.outgoing_data.shift.should.equal "myevents\n\n"
33
- @listener.outgoing_data.shift.should.equal "linger\n\n"
37
+ @listener.should send_command "connect"
38
+ @listener.should send_command "myevents"
39
+ @listener.should send_command "linger"
34
40
  end
35
41
 
36
42
  should "establish a session" do
@@ -38,111 +44,139 @@ describe "Outbound listener" do
38
44
  end
39
45
 
40
46
  should "call session callback after establishing new session" do
41
- @listener.read_data.should.equal "session was initiated"
47
+ @listener.outgoing_data.pop.should == "session was initiated"
42
48
  end
43
49
 
44
- should "make channel variables available through session" do
50
+ should "make headers available through session" do
45
51
  @listener.session[:caller_caller_id_number].should.equal "8675309"
46
52
  end
47
53
 
54
+ should "make channel variables available through #variable" do
55
+ @listener.variable(:some_var).should == "some value"
56
+ end
57
+
48
58
  behaves_like "events"
49
59
  behaves_like "api commands"
50
-
51
- should "register app" do
52
- @listener.respond_to?(:sample_app).should.be.true?
53
- end
54
60
  end
55
61
 
56
62
  class OutboundListenerWithNestedApps < Librevox::Listener::Outbound
57
63
  def session_initiated
58
- sample_app "foo" do
59
- sample_app "bar"
60
- end
64
+ sample_app "foo"
65
+ sample_app "bar"
61
66
  end
62
67
  end
63
68
 
64
69
  describe "Outbound listener with apps" do
70
+ extend Librevox::Test::Matchers
71
+
65
72
  before do
66
73
  @listener = OutboundListenerWithNestedApps.new(nil)
67
74
 
68
- # Establish session and get rid of connect-string
69
- @listener.receive_data("Content-Type: command/reply\nEstablish-Session: OK\n\n")
70
- receive_event_and_linger_replies
75
+ command_reply "Establish-Session" => "OK",
76
+ "Unique-ID" => "1234"
77
+ event_and_linger_replies
71
78
  3.times {@listener.outgoing_data.shift}
72
79
  end
73
80
 
74
81
  should "only send one app at a time" do
75
- @listener.read_data.should == "sendmsg\ncall-command: execute\nexecute-app-name: foo\n\n"
76
- @listener.read_data.should == nil
82
+ @listener.should send_application "foo"
83
+ @listener.should send_nothing
84
+
85
+ command_reply "Reply-Text" => "+OK"
86
+ @listener.should update_session
87
+ channel_data
77
88
 
78
- @listener.receive_data("Content-Type: command/reply\nReply-Text: +OK\n\n")
79
- @listener.read_data.should == "sendmsg\ncall-command: execute\nexecute-app-name: bar\n\n"
80
- @listener.read_data.should == nil
89
+ @listener.should send_application "bar"
90
+ @listener.should send_nothing
81
91
  end
82
92
 
83
93
  should "not be driven forward by events" do
84
- @listener.read_data # sample_app "foo"
85
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 45\n\nEvent-Name: CHANNEL_EXECUTE\nSession-Var: Some\n\n")
86
- @listener.read_data.should == nil
94
+ @listener.should send_application "foo"
95
+
96
+ command_reply :body => {
97
+ "Event-Name" => "CHANNEL_EXECUTE",
98
+ "Session-Var" => "Some"
99
+ }
100
+
101
+ @listener.should send_nothing
87
102
  end
88
103
 
89
104
  should "not be driven forward by api responses" do
90
- @listener.read_data # sample_app "foo"
91
- @listener.receive_data("Content-Type: api/response\nContent-Length: 3\n\nFoo")
92
- @listener.read_data.should == nil
105
+ @listener.should send_application "foo"
106
+
107
+ api_response :body => "Foo"
108
+
109
+ @listener.should send_nothing
93
110
  end
94
111
 
95
112
  should "not be driven forward by disconnect notifications" do
96
- @listener.read_data # sample_app "foo"
97
- @listener.receive_data("Content-Type: text/disconnect-notice\nContent-Length: 9\n\nLingering")
98
- @listener.read_data.should == nil
113
+ @listener.should send_application "foo"
114
+
115
+ response "Content-Type" => "text/disconnect-notice",
116
+ :body => "Lingering"
117
+
118
+ @listener.should send_nothing
99
119
  end
100
120
  end
101
121
 
102
122
  module Librevox::Applications
103
- def reader_app(&b)
104
- execute_app 'reader_app', [], {:read_var => 'a_reader_var'}, &b
123
+ def reader_app
124
+ application 'reader_app', "", {:variable => 'app_var'}
105
125
  end
106
126
  end
107
127
 
108
128
  class OutboundListenerWithReader < Librevox::Listener::Outbound
109
129
  def session_initiated
110
- reader_app do |data|
111
- send_data "read this: #{data}"
112
- end
130
+ data = reader_app
131
+ application "send", data
113
132
  end
114
133
  end
115
134
 
116
135
  describe "Outbound listener with app reading data" do
136
+ extend Librevox::Test::Matchers
137
+
117
138
  before do
118
139
  @listener = OutboundListenerWithReader.new(nil)
119
140
 
120
- # Establish session and get rid of connect-string
121
- @listener.receive_data("Content-Type: command/reply\nSession-Var: First\nUnique-ID: 1234\n\n")
122
- receive_event_and_linger_replies
141
+ command_reply "Session-Var" => "First",
142
+ "Unique-ID" => "1234"
143
+ event_and_linger_replies
123
144
  3.times {@listener.outgoing_data.shift}
145
+
146
+ @listener.should send_application "reader_app"
124
147
  end
125
148
 
126
149
  should "not send anything while missing response" do
127
- @listener.read_data # the command executing reader_app
128
- @listener.read_data.should == nil
150
+ @listener.should send_nothing
129
151
  end
130
152
 
131
153
  should "send uuid_dump to get channel var, after getting response" do
132
- @listener.receive_data("Content-Type: command/reply\nReply-Text: +OK\n\n")
133
- @listener.read_data.should == "api uuid_dump 1234\n\n"
154
+ command_reply "Reply-Text" => "+OK"
155
+ @listener.should update_session 1234
134
156
  end
135
157
 
136
158
  should "update session with new data" do
137
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 3\n\n+OK\n\n")
138
- @listener.receive_data("Content-Type: api/response\nContent-Length: 44\n\nEvent-Name: CHANNEL_DATA\nSession-Var: Second\n\n")
159
+ command_reply :body => "+OK"
160
+
161
+ @listener.should update_session 1234
162
+ api_response :body => {
163
+ "Event-Name" => "CHANNEL_DATA",
164
+ "Session-Var" => "Second"
165
+ }
166
+
139
167
  @listener.session[:session_var].should == "Second"
140
168
  end
141
169
 
142
- should "pass value of channel variable to block" do
143
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 3\n\n+OK\n\n")
144
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 59\n\nEvent-Name: CHANNEL_DATA\nvariable_a-reader-var: some value\n\n")
145
- @listener.read_data.should == "read this: some value"
170
+ should "return value of channel variable" do
171
+ command_reply :body => "+OK"
172
+
173
+ @listener.should update_session 1234
174
+ api_response :body => {
175
+ "Event-Name" => "CHANNEL_DATA",
176
+ "variable_app_var" => "Second"
177
+ }
178
+
179
+ @listener.should send_application "send", "Second"
146
180
  end
147
181
  end
148
182
 
@@ -150,99 +184,110 @@ class OutboundListenerWithNonNestedApps < Librevox::Listener::Outbound
150
184
  attr_reader :queue
151
185
  def session_initiated
152
186
  sample_app "foo"
153
- reader_app do |data|
154
- send_data "the end: #{data}"
155
- end
187
+ data = reader_app
188
+ application "send", "the end: #{data}"
156
189
  end
157
190
  end
158
191
 
159
192
  describe "Outbound listener with non-nested apps" do
193
+ extend Librevox::Test::Matchers
194
+
160
195
  before do
161
196
  @listener = OutboundListenerWithNonNestedApps.new(nil)
162
197
 
163
- # Establish session and get rid of connect-string
164
- @listener.receive_data("Content-Type: command/reply\nSession-Var: First\nUnique-ID: 1234\n\n")
165
- receive_event_and_linger_replies
198
+ command_reply "Session-Var" => "First",
199
+ "Unique-ID" => "1234"
200
+ event_and_linger_replies
166
201
  3.times {@listener.outgoing_data.shift}
167
202
  end
168
203
 
169
- should "wait for response before calling next proc" do
170
- # response to sample_app
171
- @listener.read_data.should.not.match /the end/
172
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 3\n\n+OK\n\n")
204
+ should "wait for response before calling next app" do
205
+ @listener.should send_application "foo"
206
+ command_reply :body => "+OK"
207
+ @listener.should update_session
208
+ channel_data "Unique-ID" => "1234"
173
209
 
174
- # response to reader_app
175
- @listener.read_data.should.not.match /the end/
176
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 3\n\n+OK\n\n")
210
+ @listener.should send_application "reader_app"
211
+ command_reply :body => "+OK"
177
212
 
178
- # response to uuid_dump caused by reader_app
179
- @listener.read_data.should.not.match /the end/
180
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 59\n\nEvent-Name: CHANNEL_DATA\nvariable_a-reader-var: some value\n\n")
213
+ @listener.should update_session
214
+ api_response :body => {
215
+ "Event-Name" => "CHANNEL_DATA",
216
+ "variable_app_var" => "Second"
217
+ }
181
218
 
182
- @listener.read_data.should == "the end: some value"
219
+ @listener.should send_application "send", "the end: Second"
183
220
  end
184
221
  end
185
222
 
186
223
  module Librevox::Commands
187
- def sample_cmd(cmd, *args, &b)
188
- execute_cmd cmd, *args, &b
224
+ def sample_cmd cmd, *args
225
+ command cmd, *args
189
226
  end
190
227
  end
191
228
 
192
229
  class OutboundListenerWithAppsAndApi < Librevox::Listener::Outbound
193
230
  def session_initiated
194
- sample_app "foo" do
195
- api :sample_cmd, "bar" do
196
- sample_app "baz"
197
- end
198
- end
231
+ sample_app "foo"
232
+ api.sample_cmd "bar"
233
+ sample_app "baz"
199
234
  end
200
235
  end
201
236
 
202
237
  describe "Outbound listener with both apps and api calls" do
238
+ extend Librevox::Test::Matchers
239
+
203
240
  before do
204
241
  @listener = OutboundListenerWithAppsAndApi.new(nil)
205
242
 
206
- # Establish session and get rid of connect-string
207
- @listener.receive_data("Content-Type: command/reply\nSession-Var: First\nUnique-ID: 1234\n\n")
208
- receive_event_and_linger_replies
243
+ command_reply "Session-Var" => "First",
244
+ "Unique-ID" => "1234"
245
+ event_and_linger_replies
209
246
  3.times {@listener.outgoing_data.shift}
210
247
  end
211
248
 
212
- should "wait for response before calling next proc" do
213
- @listener.read_data.should == "sendmsg\ncall-command: execute\nexecute-app-name: foo\n\n"
214
- @listener.receive_data("Content-Type: command/reply\nContent-Length: 3\n\n+OK\n\n")
249
+ should "wait for response before calling next app/cmd" do
250
+ @listener.should send_application "foo"
251
+ command_reply :body => "+OK"
252
+ @listener.should update_session
253
+ channel_data
215
254
 
216
- @listener.read_data.should == "api bar\n\n"
217
- @listener.receive_data("Content-Type: api/response\nContent-Length: 3\n\n+OK\n\n")
255
+ @listener.should send_command "api bar"
256
+ api_response :body => "+OK"
218
257
 
219
- @listener.read_data.should == "sendmsg\ncall-command: execute\nexecute-app-name: baz\n\n"
258
+ @listener.should send_application "baz"
220
259
  end
221
260
  end
222
261
 
223
262
  class OutboundListenerWithUpdateSessionCallback < Librevox::Listener::Outbound
224
263
  def session_initiated
225
- update_session do
226
- send_data "yay, #{session[:session_var]}"
227
- end
264
+ update_session
265
+ application "send", "yay, #{session[:session_var]}"
228
266
  end
229
267
  end
230
268
 
231
269
  describe "Outbound listener with update session callback" do
270
+ extend Librevox::Test::Matchers
271
+
232
272
  before do
233
273
  @listener = OutboundListenerWithUpdateSessionCallback.new(nil)
234
- @listener.receive_data("Content-Type: command/reply\nSession-Var: First\nUnique-ID: 1234\n\n")
235
- receive_event_and_linger_replies
274
+ command_reply "Session-Var" => "First",
275
+ "Unique-ID" => "1234"
276
+ event_and_linger_replies
236
277
  3.times {@listener.outgoing_data.shift}
237
278
 
238
- @listener.receive_data("Content-Type: api/response\nContent-Length: 44\n\nEvent-Name: CHANNEL_DATA\nSession-Var: Second\n\n")
279
+ @listener.should update_session
280
+ api_response :body => {
281
+ "Event-Name" => "CHANNEL_DATA",
282
+ "Session-Var" => "Second"
283
+ }
239
284
  end
240
285
 
241
286
  should "execute callback" do
242
- @listener.read_data.should =~ /^yay,/
287
+ @listener.outgoing_data.shift.should =~ /yay,/
243
288
  end
244
289
 
245
290
  should "update session before calling callback" do
246
- @listener.read_data.should == "yay, Second"
291
+ @listener.should send_application "send", "yay, Second"
247
292
  end
248
293
  end
@@ -1,4 +1,4 @@
1
- require 'spec/helper'
1
+ require './spec/helper'
2
2
  require 'librevox/applications'
3
3
 
4
4
  module AppTest
@@ -6,12 +6,11 @@ module AppTest
6
6
 
7
7
  extend self
8
8
 
9
- def execute_app(name, args=[], params={}, &block)
9
+ def application name, args="", params={}
10
10
  {
11
11
  :name => name,
12
12
  :args => args,
13
- :params => params,
14
- :block => block
13
+ :params => params
15
14
  }
16
15
  end
17
16
  end
@@ -129,7 +128,7 @@ describe Librevox::Applications do
129
128
  app = AppTest.play_and_get_digits "please-enter", "wrong-try-again"
130
129
  app[:name].should == "play_and_get_digits"
131
130
  app[:args].should == "1 2 3 5000 # please-enter wrong-try-again read_digits_var \\d+"
132
- app[:params][:read_var].should == "read_digits_var"
131
+ app[:params][:variable].should == "read_digits_var"
133
132
  end
134
133
 
135
134
  should "take params" do
@@ -139,11 +138,11 @@ describe Librevox::Applications do
139
138
  :tries => 4,
140
139
  :terminators => "0",
141
140
  :timeout => 10000,
142
- :read_var => "other_var",
141
+ :variable => "other_var",
143
142
  :regexp => "[125]"
144
143
 
145
144
  app[:args].should == "2 3 4 10000 0 please-enter invalid-choice other_var [125]"
146
- app[:params][:read_var].should == "other_var"
145
+ app[:params][:variable].should == "other_var"
147
146
  end
148
147
  end
149
148
 
@@ -165,7 +164,7 @@ describe Librevox::Applications do
165
164
  app = AppTest.read "please-enter.wav"
166
165
  app[:name].should == "read"
167
166
  app[:args].should == "1 2 please-enter.wav read_digits_var 5000 #"
168
- app[:params][:read_var].should == "read_digits_var"
167
+ app[:params][:variable].should == "read_digits_var"
169
168
  end
170
169
 
171
170
  should "take params" do
@@ -174,10 +173,10 @@ describe Librevox::Applications do
174
173
  :max => 3,
175
174
  :terminators => "0",
176
175
  :timeout => 10000,
177
- :read_var => "other_var"
176
+ :variable => "other_var"
178
177
 
179
178
  app[:args].should == "2 3 please-enter.wav other_var 10000 0"
180
- app[:params][:read_var].should == "other_var"
179
+ app[:params][:variable].should == "other_var"
181
180
  end
182
181
  end
183
182
 
@@ -203,6 +202,12 @@ describe Librevox::Applications do
203
202
  end
204
203
  end
205
204
 
205
+ should "respond with code" do
206
+ app = AppTest.respond 403
207
+ app[:name].should == "respond"
208
+ app[:args].should == "403"
209
+ end
210
+
206
211
  should "set" do
207
212
  app = AppTest.set("foo", "bar")
208
213
  app[:name].should == "set"