tropo-webapi-ruby 0.1.5

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/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'tropo-webapi-ruby'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
@@ -0,0 +1,444 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Tropo" do
4
+
5
+ # Ask action tests (and alias Prompt)
6
+ it "should generate a complete 'ask' JSON document" do
7
+ response = Tropo::Generator.ask({ :name => 'foo',
8
+ :bargein => 'true',
9
+ :timeout => 30,
10
+ :require => 'true' })
11
+ JSON.parse(response).should == { "tropo" => [{ "ask" => { "name" => "foo", "bargein" => "true", "timeout" => 30, "require" => "true" } }] }
12
+ end
13
+
14
+ it "should generate an 'ask' JSON document when a block is passed" do
15
+ response = Tropo::Generator.ask({ :name => 'foo',
16
+ :bargein => 'true',
17
+ :timeout => 30,
18
+ :require => 'true' }) do
19
+ say :value => 'Please say your account number'
20
+ choices :value => '[5 DIGITS]'
21
+ end
22
+ JSON.parse(response).should == {"tropo"=>[{"ask"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "bargein"=>"true", "timeout"=>30, "require"=>"true", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
23
+ end
24
+
25
+ # There is currently a feature request to support an on within an ask
26
+ #
27
+ # it "should generate an 'ask' JSON document when a block is passed with an 'on' action" do
28
+ # response = Tropo::Generator.ask({ :name => 'foo',
29
+ # :bargein => 'true',
30
+ # :timeout => 30,
31
+ # :require => 'true' }) do
32
+ # say :value => 'Please say your account number'
33
+ # choices :value => '[5 DIGITS]'
34
+ # on :event => 'success', :next => '/result.json'
35
+ # end
36
+ # JSON.parse(response).should == {"tropo"=>[{"ask"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "bargein"=>"true", "timeout"=>30, "require"=>"true", "on"=>[{"event"=>"success", "next"=>"/result.json"}], "choices"=>{"value"=>"[5 DIGITS]"}}}]}
37
+ # end
38
+
39
+ it "should generate an error if an 'ask' is passed without a 'name' parameter" do
40
+ begin
41
+ response = Tropo::Generator.ask({ :foo => 'bar' })
42
+ rescue => err
43
+ err.to_s.should == "A 'name' must be provided to a 'ask' action"
44
+ end
45
+ end
46
+
47
+ # Prompt
48
+ it "should generate a complete 'prompt' JSON document" do
49
+ response = Tropo::Generator.prompt({ :name => 'foo',
50
+ :bargein => 'true',
51
+ :timeout => 30,
52
+ :require => 'true' })
53
+ JSON.parse(response).should == { "tropo" => [{ "ask" => { "name" => "foo", "bargein" => "true", "timeout" => 30, "require" => "true" } }] }
54
+ end
55
+
56
+ it "should generate an 'prompt' JSON document when a block is passed" do
57
+ response = Tropo::Generator.prompt({ :name => 'foo',
58
+ :bargein => 'true',
59
+ :timeout => 30,
60
+ :require => 'true' }) do
61
+ say :value => 'Please say your account number'
62
+ choices :value => '[5 DIGITS]'
63
+ end
64
+ JSON.parse(response).should == {"tropo"=>[{"ask"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "bargein"=>"true", "timeout"=>30, "require"=>"true", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
65
+ end
66
+
67
+ it "should generate an error if an 'prompt' is passed without a 'name' parameter" do
68
+ begin
69
+ response = Tropo::Generator.prompt({ :foo => 'bar' })
70
+ rescue => err
71
+ err.to_s.should == "A 'name' must be provided to a 'ask' action"
72
+ end
73
+ end
74
+
75
+ # Choices tests
76
+ it "should generate a standard 'choices' JSON document" do
77
+ response = Tropo::Generator.choices({ :value => '[5 DIGITS]' })
78
+ JSON.parse(response).should == { 'tropo' => [{ 'choices' => { 'value' => '[5 DIGITS]' } }] }
79
+ end
80
+
81
+ it "should raise an error if a 'choices' passes an unspported mode" do
82
+ begin
83
+ response = Tropo::Generator.choices({ :value => '[5 DIGITS]', :mode => 'frootloops' })
84
+ rescue => err
85
+ err.to_s.should == "If mode is provided, only 'dtmf', 'speech' or 'any' is supported"
86
+ end
87
+ end
88
+
89
+ it "should generate a standard 'choices' JSON document with a mode" do
90
+ response = Tropo::Generator.choices({ :value => '[5 DIGITS]', :mode => 'dtmf' })
91
+ JSON.parse(response).should == { 'tropo' => [{ 'choices' => { 'value' => '[5 DIGITS]', 'mode' => 'dtmf' } }] }
92
+ end
93
+
94
+ # Conference action tests
95
+ it "should generate a complete 'conference' JSON document" do
96
+ response = Tropo::Generator.conference({ :name => 'foo',
97
+ :id => '1234',
98
+ :mute => false,
99
+ :send_tones => false,
100
+ :exit_tone => '#' })
101
+ JSON.parse(response).should == {"tropo"=>[{"conference"=>{"name"=>"foo", "mute"=>false, "sendTones"=>false, "id"=>"1234", "exitTone"=>"#"}}]}
102
+ end
103
+
104
+ it "should generate a complete 'conference' JSON document when a block is passed" do
105
+ response = Tropo::Generator.conference({ :name => 'foo',
106
+ :id => '1234',
107
+ :mute => false,
108
+ :send_tones => false,
109
+ :exit_tone => '#' }) do
110
+ on(:event => 'join') { say :value => 'Welcome to the conference' }
111
+ on(:event => 'leave') { say :value => 'Someone has left the conference' }
112
+ end
113
+ JSON.parse(response).should == {"tropo"=>[{"conference"=>{"name"=>"foo", "mute"=>false, "id"=>"1234", "exitTone"=>"#", "sendTones"=>false, "on"=>[{"say"=>[{"value"=>"Welcome to the conference"}], "event"=>"join"}, {"say"=>[{"value"=>"Someone has left the conference"}], "event"=>"leave"}]}}]}
114
+ end
115
+
116
+ it "should generate an error if an 'conference' is passed without a 'name' parameter" do
117
+ begin
118
+ response = Tropo::Generator.conference({ :foo => 'bar' })
119
+ rescue => err
120
+ err.to_s.should == "A 'name' must be provided to a 'conference' action"
121
+ end
122
+ end
123
+
124
+ it "should generate an error if an 'conference' is passed without an 'id' parameter" do
125
+ begin
126
+ response = Tropo::Generator.conference({ :name => 'bar' })
127
+ rescue => err
128
+ err.to_s.should == "A 'id' must be provided to a 'conference' action"
129
+ end
130
+ end
131
+
132
+ # Hangup action tests and Disconnect alias
133
+ it "should generate a JSON document with a 'hangup' action" do
134
+ response = Tropo::Generator.hangup
135
+ JSON.parse(response).should == {"tropo"=>[{"hangup"=>nil}]}
136
+ end
137
+
138
+ it "should generate a JSON document with a 'disconnect' action" do
139
+ response = Tropo::Generator.disconnect
140
+ JSON.parse(response).should == {"tropo"=>[{"hangup"=>nil}]}
141
+ end
142
+
143
+ it "should generate a standard 'on' JSON document" do
144
+ response = Tropo::Generator.on({ :event => 'hangup', :next => 'myresource' })
145
+ JSON.parse(response).should == { "tropo" => [{ "on" =>{ "event" => "hangup", "next" => "myresource" } }] }
146
+ end
147
+
148
+ # On tests
149
+ it "should generate a an error of an 'on' document does not pass an event param" do
150
+ begin
151
+ response = Tropo::Generator.on({ :foo => 'bar' })
152
+ rescue => err
153
+ err.to_s.should == "A 'event' must be provided to a 'on' action"
154
+ end
155
+ end
156
+
157
+ it "should generate a an error of an 'on' document does not pass an event param" do
158
+ begin
159
+ response = Tropo::Generator.on({ :event => 'bar' })
160
+ rescue => err
161
+ err.to_s.should == "A 'next' resource must be provided"
162
+ end
163
+ end
164
+
165
+ # Record action tests
166
+ it "should generate a complete 'record' JSON document" do
167
+ response = Tropo::Generator.record({ :name => 'foo',
168
+ :url => 'http://sendme.com/tropo',
169
+ :beep => true,
170
+ :send_tones => false,
171
+ :exit_tone => '#' })
172
+ JSON.parse(response).should == {"tropo"=>[{"record"=>{"name"=>"foo", "beep"=>true, "url"=>"http://sendme.com/tropo", "exitTone"=>"#", "sendTones"=>false}}]}
173
+ end
174
+
175
+ it "should generate a complete 'record' JSON document when a block is passed" do
176
+ response = Tropo::Generator.record({ :name => 'foo',
177
+ :url => 'http://sendme.com/tropo',
178
+ :beep => true,
179
+ :send_tones => false,
180
+ :exit_tone => '#' }) do
181
+ say :value => 'Please say your account number'
182
+ choices :value => '[5 DIGITS]'
183
+ end
184
+ JSON.parse(response).should == {"tropo"=>[{"record"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "sendTones"=>false, "exitTone"=>"#", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
185
+ end
186
+
187
+ it "should generate an error if an 'record' is passed without a 'name' parameter" do
188
+ begin
189
+ response = Tropo::Generator.record({ :foo => 'bar' })
190
+ rescue => err
191
+ err.to_s.should == "A 'name' must be provided to a 'record' action"
192
+ end
193
+ end
194
+
195
+ it "should generate an error if an 'record' is passed without an 'url' parameter" do
196
+ begin
197
+ response = Tropo::Generator.record({ :name => 'bar' })
198
+ rescue => err
199
+ err.to_s.should == "A 'url' must be provided to a 'record' action"
200
+ end
201
+ end
202
+
203
+ it "should generate an error if an 'record' is passed without an invalid 'url' parameter" do
204
+ begin
205
+ response = Tropo::Generator.record({ :name => 'bar',
206
+ :url => 'foobar' })
207
+ rescue => err
208
+ err.to_s.should == "The 'url' paramater must be a valid URL"
209
+ end
210
+ end
211
+
212
+ it "should accept a valid email address when a 'record' action is called" do
213
+ response = Tropo::Generator.record({ :name => 'bar',
214
+ :url => 'foo@bar.com' })
215
+ response.should == "{\"tropo\":[{\"record\":{\"url\":\"foo@bar.com\",\"name\":\"bar\"}}]}"
216
+ end
217
+
218
+ # Redirect action tests
219
+ it "should generate a JSON document with a 'redirect' action" do
220
+ response = Tropo::Generator.redirect({ :to => 'sip:1234', :from => '4155551212' })
221
+ JSON.parse(response).should == {"tropo"=>[{"redirect"=>{"from"=>"4155551212", "to"=>"sip:1234"}}]}
222
+ end
223
+
224
+ it "should generate an error if a 'redirect' action is included in a block" do
225
+ begin
226
+ response = Tropo::Generator.conference(:name => 'foobar', :id => 1234) do
227
+ redirect(:to => 'sip:1234', :from => '4155551212')
228
+ end
229
+ rescue => err
230
+ err.to_s.should == 'Redirect should only be used alone and before the session is answered, use transfer instead'
231
+ end
232
+ end
233
+
234
+ it "should generate an error when no 'to' is passed to a 'redirect' action" do
235
+ begin
236
+ response = Tropo::Generator.redirect
237
+ rescue => err
238
+ err.to_s.should == "A 'to' must be provided to a 'redirect' action"
239
+ end
240
+ end
241
+
242
+ # Reject action tests
243
+ it "should generate a JSON document with a 'reject' action" do
244
+ response = Tropo::Generator.reject
245
+ JSON.parse(response).should == {"tropo"=>[{"reject"=>nil}]}
246
+ end
247
+
248
+ # Say action tests
249
+ it "should generate a standard 'say' JSON document when a stiring is passed" do
250
+ JSON.parse(Tropo::Generator.say('1234')).should == { "tropo" => [{ "say" => [{ "value" => "1234" }] }] }
251
+ end
252
+
253
+ it "should generate an error if I try to pass an integer to a 'say' action" do
254
+ begin
255
+ Tropo::Generator.say(1234)
256
+ rescue => err
257
+ err.to_s.should == "An invalid paramater type Fixnum has been passed"
258
+ end
259
+ end
260
+
261
+ it "should generate a standard 'say' JSON document" do
262
+ JSON.parse(Tropo::Generator.say({ :value => '1234' })).should == { "tropo" => [{ "say" => [{ "value" => "1234" }] }] }
263
+ end
264
+
265
+ it "should generate a 'say' JSON document when an array of values is passed" do
266
+ response = Tropo::Generator.say([{ :value => '1234' }, { :value => 'abcd', :event => 'nomatch:1' }])
267
+ JSON.parse(response).should == { "tropo" => [{ "say" => [{ "value" => "1234" }, { "value" => "abcd", "event"=>"nomatch:1" }] }] }
268
+ end
269
+
270
+ it "should generate an error if no 'value' key is passed to a 'say' request" do
271
+ begin
272
+ response = Tropo::Generator.say({ :name => 'foo' })
273
+ rescue => err
274
+ err.to_s.should == "A 'value' must be provided to a 'say' action"
275
+ end
276
+ end
277
+
278
+ it "should generate a JSON document with a 'say' and an 'on'" do
279
+ result = Tropo::Generator.new do
280
+ say :value => 'blah'
281
+ on :event => 'error', :next => 'error.json'
282
+ end
283
+ JSON.parse(result.response).should == {"tropo"=>[{"say"=>[{"value"=>"blah"}]}, {"on"=>{"event"=>"error", "next"=>"error.json"}}]}
284
+ end
285
+
286
+ # Start & Stop Recording actions tests
287
+ it "should generate a JSON document with a 'start_recording' action" do
288
+ response = Tropo::Generator.start_recording(:name => 'recording', :url => 'http://postrecording.com/tropo')
289
+ JSON.parse(response).should == {"tropo"=>[{"startRecording"=>{"name"=>"recording", "url"=>"http://postrecording.com/tropo"}}]}
290
+ end
291
+
292
+ it "should generate a JSON document with a 'stoprecording' action" do
293
+ response = Tropo::Generator.stop_recording
294
+ JSON.parse(response).should == {"tropo"=>[{"stopRecording"=>nil}]}
295
+ end
296
+
297
+ # Transfer action tests
298
+ it "should generate a JSON document with a 'transfer' action" do
299
+ response = Tropo::Generator.transfer(:to => 'tel:+14157044517')
300
+ JSON.parse(response).should == {"tropo"=>[{"transfer"=>{"to"=>"tel:+14157044517"}}]}
301
+ end
302
+
303
+ # Transfer action tests
304
+ it "should generate a JSON document with a 'transfer' action with an 'on' and 'choices' actions" do
305
+ response = Tropo::Generator.transfer(:to => 'tel:+14157044517') do
306
+ on :event => 'unbounded', :next => '/error.json'
307
+ choices :value => '[5 DIGITS]'
308
+ end
309
+ JSON.parse(response).should == {"tropo"=>[{"transfer"=>{"to"=>"tel:+14157044517", "choices"=>{"value"=>"[5 DIGITS]"}, "on"=>[{"event"=>"unbounded", "next"=>"/error.json"}]}}]}
310
+ end
311
+
312
+ it "should generate an error if no 'to' key is passed to a 'transfer' request" do
313
+ begin
314
+ response = Tropo::Generator.transfer
315
+ rescue => err
316
+ err.to_s.should == "A 'to' must be provided to a 'transfer' action"
317
+ end
318
+ end
319
+
320
+ # General tests
321
+ it "should generate a JSON document when a block is passed" do
322
+ result = Tropo::Generator.new do
323
+ say [{ :value => '1234' }, { :value => 'abcd', :event => "nomatch:1" }]
324
+ say [{ :value => '0987' }, { :value => 'zyxw', :event => "nomatch:2" }]
325
+ end
326
+ JSON.parse(result.response).should == {"tropo"=>[{"say"=>[{"value"=>"1234"}, {"value"=>"abcd", "event"=>"nomatch:1"}]}, {"say"=>[{"value"=>"0987"}, {"value"=>"zyxw", "event"=>"nomatch:2"}]}]}
327
+ end
328
+
329
+ it "should build a Ruby hash object when a session arrives in JSON" do
330
+ json_session = "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
331
+ hash = Tropo::Generator.parse(json_session)
332
+ hash[:session][:timestamp] == Time.parse('2010-01-19T18:27:46.852-05:00')
333
+ end
334
+
335
+ it "should build a Ruby hash object when a result arrives in JSON" do
336
+ json_result = "{\"result\":{\"sessionId\":\"sessionId\",\"callState\":\"ANSWERED\",\"sessionDuration\":10,\"sequence\":1,\"complete\":true,\"error\":\"error\",\"properties\":[{\"key\":\"foo\",\"value\":\"bar\"},{\"key\":\"charlie\",\"value\":\"foxtrot\"}],\"actions\":{\"name\":\"pin\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"}}}"
337
+ Tropo::Generator.parse(json_result).should == {:result=>{:session_id=>"sessionId", :properties=>{:foo=>{:value=>"bar"}, :charlie=>{:value=>"foxtrot"}}, :complete=>true, :call_state=>"ANSWERED", :actions=>{:pin=>{:disposition=>"SUCCESS", :utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100}}, :session_duration=>10, :error=>"error", :sequence=>1}}
338
+ end
339
+
340
+ it "should build a ruby hash object when a realworld JSON string arrives" do
341
+ json_result = "{\"result\":{\"sessionId\":\"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20\",\"callState\":\"ANSWERED\",\"sessionDuration\":2,\"sequence\":1,\"complete\":true,\"error\":null,\"actions\":[{\"name\":\"zip\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"},{\"name\":\"days\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"1\",\"utterance\":\"1\"}]}}"
342
+ Tropo::Generator.parse(json_result).should == {:result=>{:call_state=>"ANSWERED", :complete=>true, :actions=>{:zip=>{:disposition=>"SUCCESS", :utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100}, :days=>{:disposition=>"SUCCESS", :utterance=>"1", :attempts=>1, :interpretation=>"1", :confidence=>100}}, :session_duration=>2, :sequence=>1, :session_id=>"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20", :error=>nil}}
343
+ end
344
+
345
+ it "should see an object delcared outside of a block" do
346
+ @@session = 'foobar'
347
+ result = Tropo::Generator.new do
348
+ @@new_session = @@session
349
+ say :value => 'blah'
350
+ on :event => 'error', :next => 'error.json'
351
+ end
352
+ @@new_session.should == 'foobar'
353
+ end
354
+
355
+ it "should see an object passed into the block" do
356
+ session = 'foobar'
357
+ result = Tropo::Generator.new(session) do
358
+ session.should == 'foobar'
359
+ say :value => 'blah'
360
+ on :event => 'error', :next => 'error.json'
361
+ end
362
+ end
363
+
364
+ it "should allow you to create a Tropo::Generator object and build up a JSON request with two says" do
365
+ tropo = Tropo::Generator.new
366
+ tropo.say('foo')
367
+ tropo.say('bar')
368
+ tropo.response.should == "{\"tropo\":[{\"say\":[{\"value\":\"foo\"}]},{\"say\":[{\"value\":\"bar\"}]}]}"
369
+ end
370
+
371
+ it "should allow you to create a Tropo::Generator object and build up a JSON request with: a say, an on and a record" do
372
+ tropo = Tropo::Generator.new
373
+ tropo.say 'Welcome to the app'
374
+ tropo.on :event => 'hangup', :next => '/hangup.json'
375
+ tropo.record({ :name => 'foo',
376
+ :url => 'http://sendme.com/tropo',
377
+ :beep => true,
378
+ :send_tones => false,
379
+ :exit_tone => '#' }) do
380
+ say :value => 'Please say your account number'
381
+ choices :value => '[5 DIGITS]'
382
+ end
383
+ JSON.parse(tropo.response).should == {"tropo"=>[{"say"=>[{"value"=>"Welcome to the app"}]}, {"on"=>{"event"=>"hangup", "next"=>"/hangup.json"}}, {"record"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "sendTones"=>false, "exitTone"=>"#", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
384
+ end
385
+
386
+ it "should allow you to reset the object to a fresh response after building a response first" do
387
+ tropo = Tropo::Generator.new
388
+ tropo.say 'Welcome to the app'
389
+ tropo.on :event => 'hangup', :next => '/hangup.json'
390
+ tropo.record({ :name => 'foo',
391
+ :url => 'http://sendme.com/tropo',
392
+ :beep => true,
393
+ :send_tones => false,
394
+ :exit_tone => '#' }) do
395
+ say :value => 'Please say your account number'
396
+ choices :value => '[5 DIGITS]'
397
+ end
398
+ JSON.parse(tropo.response).should == {"tropo"=>[{"say"=>[{"value"=>"Welcome to the app"}]}, {"on"=>{"event"=>"hangup", "next"=>"/hangup.json"}}, {"record"=>{"name"=>"foo", "say"=>[{"value"=>"Please say your account number"}], "beep"=>true, "url"=>"http://sendme.com/tropo", "sendTones"=>false, "exitTone"=>"#", "choices"=>{"value"=>"[5 DIGITS]"}}}]}
399
+ tropo.reset
400
+ tropo.response.should == "{\"tropo\":[]}"
401
+ end
402
+
403
+ it "should build a Ruby hash object when a session arrives in JSON with a proper Ruby Time object" do
404
+ json_session = "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
405
+ hash = Tropo::Generator.parse(json_session)
406
+ hash[:session][:timestamp].should == Time.parse("2010-01-19T23:18:48.562Z")
407
+ end
408
+
409
+ it "should build a Ruby hash object when a result arrives in JSON with one action returned in an array" do
410
+ json_result = "{\"result\":{\"sessionId\":\"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20\",\"callState\":\"ANSWERED\",\"sessionDuration\":2,\"sequence\":1,\"complete\":true,\"error\":null,\"actions\":{\"name\":\"zip\",\"attempts\":1,\"disposition\":\"SUCCESS\",\"confidence\":100,\"interpretation\":\"12345\",\"utterance\":\"1 2 3 4 5\"}}}"
411
+ hash = Tropo::Generator.parse(json_result)
412
+ hash.should == {:result=>{:call_state=>"ANSWERED", :complete=>true, :actions=>{:zip=>{:utterance=>"1 2 3 4 5", :attempts=>1, :interpretation=>"12345", :confidence=>100, :disposition=>"SUCCESS"}}, :session_id=>"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20", :session_duration=>2, :error=>nil, :sequence=>1}}
413
+ end
414
+
415
+ it "should generate valid JSON when a startRecording is used" do
416
+ t = Tropo::Generator.new
417
+ t.on :event => 'error', :next => '/error.json' # For fatal programming errors. Log some details so we can fix it
418
+ t.on :event => 'hangup', :next => '/hangup.json' # When a user hangs or call is done. We will want to log some details.
419
+ t.on :event => 'continue', :next => '/next.json'
420
+ t.say "Hello"
421
+ t.start_recording(:name => 'recording', :url => "http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar")
422
+ # [From this point, until stop_recording(), we will record what the caller *and* the IVR say]
423
+ t.say "You are now on the record."
424
+ # Prompt the user to incriminate themselve on-the-record
425
+ t.say "Go ahead, sing-along."
426
+ t.say "http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"
427
+ JSON.parse(t.response).should == {"tropo"=>[{"on"=>{"event"=>"error", "next"=>"/error.json"}}, {"on"=>{"event"=>"hangup", "next"=>"/hangup.json"}}, {"on"=>{"event"=>"continue", "next"=>"/next.json"}}, {"say"=>[{"value"=>"Hello"}]}, {"startRecording"=>{"name"=>"recording", "url"=>"http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar"}}, {"say"=>[{"value"=>"You are now on the record."}]}, {"say"=>[{"value"=>"Go ahead, sing-along."}]}, {"say"=>[{"value"=>"http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"}]}]}
428
+ end
429
+
430
+ it "should generate a voice_session true if a JSON session is received that is a channel of 'VOICE'" do
431
+ tropo = Tropo::Generator.new
432
+ tropo.parse "{\"session\":{\"id\":\"0-13c4-4b563da3-7aecefda-46af-1d10bdd0\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:00.854Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"9991427589\",\"name\":\"unknown\",\"channel\":\"VOICE\",\"network\":\"PSTN\"},\"from\":{\"id\":\"jsgoecke\",\"name\":\"unknown\",\"channel\":\"VOICE\",\"network\":\"PSTN\"}}}"
433
+ tropo.voice_session.should == true
434
+ tropo.text_session.should == false
435
+ end
436
+
437
+ it "should generate a text_session true if a JSON session is received that is a channel of 'TEXT'" do
438
+ tropo = Tropo::Generator.new
439
+ tropo.parse "{\"session\":{\"id\":\"dih06n\",\"accountId\":\"33932\",\"timestamp\":\"2010-01-19T23:18:48.562Z\",\"userType\":\"HUMAN\",\"to\":{\"id\":\"tropomessaging@bot.im\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"},\"from\":{\"id\":\"john_doe@gmail.com\",\"name\":\"unknown\",\"channel\":\"TEXT\",\"network\":\"JABBER\"}}}"
440
+ tropo.voice_session.should == false
441
+ tropo.text_session.should == true
442
+ end
443
+
444
+ end