twilio 1.4.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/twilio/verb.rb CHANGED
@@ -1,186 +1,315 @@
1
1
  module Twilio
2
2
  # Twilio Verbs enable your application to respond to Twilio requests (to your app) with XML responses.
3
+ # There are 5 primary verbs (say, play, gather, record, dial) and 3 secondary (hangup, pause, redirect).
4
+ # Verbs can be chained and, in some cases, nested.
3
5
  #
4
- # In addition to the 5 verbs supported by Twilio (say, play, gather, record, dial),
5
- # this class also implements dynamic interfaces that allow you to combine useful
6
- # operations into a single call. See below methods for examples.
6
+ # If your response consists of a single verb, you can call a Verb class method:
7
+ #
8
+ # Twilio::Verb.say('The time is 9:35 PM.')
9
+ #
10
+ # But if you need to chain several verbs together, just wrap them in an instance block and call the 'response' attribute:
11
+ #
12
+ # verb = Twilio::Verb.new { |v|
13
+ # v.dial('415-123-4567')
14
+ # v.redirect('http://www.foo.com/nextInstructions')
15
+ # }
16
+ # verb.response
7
17
  class Verb
18
+
19
+ attr_reader :response
20
+
8
21
  class << self
9
- # The Say verb converts text to speech that is read back to the caller.
10
- # Say is useful for dynamic text that is difficult to prerecord.
11
- #
12
- # Examples:
13
- # Twilio::Verb.say('The time is 9:35 PM.')
14
- # Twilio::Verb.say_3_times('The time is 9:35 PM.')
15
- #
16
- # With numbers, 12345 will be spoken as "twelve thousand three hundred forty five" while
17
- # 1 2 3 4 5 will be spoken as "one two three four five."
18
- #
19
- # Twilio::Verb.say_4_times('Your PIN is 1234')
20
- # Twilio::Verb.say_4_times('Your PIN is 1 2 3 4')
21
- #
22
- # If you need a longer pause between each loop, use the pause form:
23
- #
24
- # Twilio::Verb.say_4_times_with_pause('Your PIN is 1 2 3 4')
25
- #
26
- # Options (see http://www.twilio.com/docs/api_reference/TwiML/say) are passed in as a hash:
27
- #
28
- # Twilio::Verb.say('The time is 9:35 PM.', :voice => 'woman')
29
- # Twilio::Verb.say('The time is 9:35 PM.', {:voice => 'woman', :language => 'es'})
30
- def say(*args, &block)
31
- options = {:voice => 'man', :language => 'en', :loop => 1}
32
- args.each do |arg|
33
- case arg
34
- when String
35
- options[:text_to_speak] = arg
36
- when Hash
37
- options.merge!(arg)
38
- else
39
- raise ArgumentError, 'say expects String or Hash argument'
40
- end
41
- end
42
-
43
- xml = Builder::XmlMarkup.new
44
- xml.instruct!
45
- xml.Response {
46
- if options[:pause]
47
- loop_with_pause(options[:loop], xml) do
48
- xml.Say(options[:text_to_speak], :voice => options[:voice], :language => options[:language])
49
- end
50
- else
51
- xml.Say(options[:text_to_speak], :voice => options[:voice], :language => options[:language], :loop => options[:loop])
52
- end
53
- }
22
+ def method_missing(method_id, *args) #:nodoc:
23
+ v = Verb.new
24
+ v.send(method_id, *args)
54
25
  end
26
+ end
27
+
28
+ def initialize(&block)
29
+ @xml = Builder::XmlMarkup.new
30
+ @xml.instruct!
55
31
 
56
- # The Play verb plays an audio URL back to the caller.
57
- # Examples:
58
- # Twilio::Verb.play('http://foo.com/cowbell.mp3')
59
- # Twilio::Verb.play_3_times('http://foo.com/cowbell.mp3')
60
- #
61
- # If you need a longer pause between each loop, use the pause form:
62
- #
63
- # Twilio::Verb.play_3_times_with_pause('http://foo.com/cowbell.mp3')
64
- #
65
- # Options (see http://www.twilio.com/docs/api_reference/TwiML/play) are passed in as a hash,
66
- # however, since the Play verb only supports 'loop' as the current option, you can instead use the
67
- # above form to keep things concise.
68
- def play(*args, &block)
69
- options = {:loop => 1}
70
- args.each do |arg|
71
- case arg
72
- when String
73
- options[:audio_url] = arg
74
- when Hash
75
- options.merge!(arg)
76
- else
77
- raise ArgumentError, 'play expects String or Hash argument'
78
- end
32
+ if block_given?
33
+ @chain = true
34
+ @response = @xml.Response { block.call(self) }
35
+ end
36
+ end
37
+
38
+ # The Say verb converts text to speech that is read back to the caller.
39
+ # Say is useful for dynamic text that is difficult to prerecord.
40
+ #
41
+ # Examples:
42
+ # Twilio::Verb.say('The time is 9:35 PM.')
43
+ # Twilio::Verb.say('The time is 9:35 PM.', :loop => 3)
44
+ #
45
+ # With numbers, 12345 will be spoken as "twelve thousand three hundred forty five" while
46
+ # 1 2 3 4 5 will be spoken as "one two three four five."
47
+ #
48
+ # Twilio::Verb.say('Your PIN is 1234', :loop => 4)
49
+ # Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4)
50
+ #
51
+ # If you need a longer pause between each loop, instead of explicitly calling the Pause
52
+ # verb within a block, you can set the convenient pause option:
53
+ #
54
+ # Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4, :pause => true)
55
+ #
56
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/say) are passed in as a hash:
57
+ #
58
+ # Twilio::Verb.say('The time is 9:35 PM.', :voice => 'woman')
59
+ # Twilio::Verb.say('The time is 9:35 PM.', {:voice => 'woman', :language => 'es'})
60
+ def say(*args)
61
+ options = {:voice => 'man', :language => 'en', :loop => 1}
62
+ args.each do |arg|
63
+ case arg
64
+ when String
65
+ options[:text_to_speak] = arg
66
+ when Hash
67
+ options.merge!(arg)
68
+ else
69
+ raise ArgumentError, 'say expects String or Hash argument'
79
70
  end
80
-
81
- xml = Builder::XmlMarkup.new
82
- xml.instruct!
83
- xml.Response {
84
- if options[:pause]
85
- loop_with_pause(options[:loop], xml) do
86
- xml.Play(options[:audio_url])
87
- end
88
- else
89
- xml.Play(options[:audio_url], :loop => options[:loop])
90
- end
91
- }
92
71
  end
93
72
 
94
- # The Gather verb collects digits entered by a caller into their telephone keypad.
95
- # When the caller is done entering data, Twilio submits that data to a provided URL,
96
- # as either a HTTP GET or POST request, just like a web browser submits data from an HTML form.
97
- #
98
- # Options (see http://www.twilio.com/docs/api_reference/TwiML/gather) are passed in as a hash
99
- #
100
- # Examples:
101
- # Twilio::Verb.gather
102
- # Twilio::Verb.gather(:action => 'http://foobar.com')
103
- # Twilio::Verb.gather(:finishOnKey => '*')
104
- # Twilio::Verb.gather(:action => 'http://foobar.com', :finishOnKey => '*')
105
- def gather(*args, &block)
106
- options = args.shift
107
- xml = Builder::XmlMarkup.new
108
- xml.instruct!
109
- xml.Response { xml.Gather(options) }
73
+ output {
74
+ if options[:pause]
75
+ loop_with_pause(options[:loop], @xml) do
76
+ @xml.Say(options[:text_to_speak], :voice => options[:voice], :language => options[:language])
77
+ end
78
+ else
79
+ @xml.Say(options[:text_to_speak], :voice => options[:voice], :language => options[:language], :loop => options[:loop])
80
+ end
81
+ }
82
+ end
83
+
84
+ # The Play verb plays an audio URL back to the caller.
85
+ # Examples:
86
+ # Twilio::Verb.play('http://foo.com/cowbell.mp3')
87
+ # Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3)
88
+ #
89
+ # If you need a longer pause between each loop, instead of explicitly calling the Pause
90
+ # verb within a block, you can set the convenient pause option:
91
+ #
92
+ # Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3, :pause => true)
93
+ #
94
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/play) are passed in as a hash,
95
+ # but only 'loop' is currently supported.
96
+ def play(*args)
97
+ options = {:loop => 1}
98
+ args.each do |arg|
99
+ case arg
100
+ when String
101
+ options[:audio_url] = arg
102
+ when Hash
103
+ options.merge!(arg)
104
+ else
105
+ raise ArgumentError, 'play expects String or Hash argument'
106
+ end
110
107
  end
111
-
112
- # The Record verb records the caller's voice and returns a URL that links to a file
113
- # containing the audio recording.
114
- #
115
- # Options (see http://www.twilio.com/docs/api_reference/TwiML/record) are passed in as a hash
116
- #
117
- # Examples:
118
- # Twilio::Verb.record
119
- # Twilio::Verb.record(:action => 'http://foobar.com')
120
- # Twilio::Verb.record(:finishOnKey => '*')
121
- # Twilio::Verb.record(:transcribe => true, :transcribeCallback => '/handle_transcribe')
122
- def record(*args, &block)
123
- options = args.shift
124
- xml = Builder::XmlMarkup.new
125
- xml.instruct!
126
- xml.Response { xml.Record(options) }
108
+
109
+ output {
110
+ if options[:pause]
111
+ loop_with_pause(options[:loop], @xml) do
112
+ @xml.Play(options[:audio_url])
113
+ end
114
+ else
115
+ @xml.Play(options[:audio_url], :loop => options[:loop])
116
+ end
117
+ }
118
+ end
119
+
120
+ # The Gather verb collects digits entered by a caller into their telephone keypad.
121
+ # When the caller is done entering data, Twilio submits that data to a provided URL,
122
+ # as either a HTTP GET or POST request, just like a web browser submits data from an HTML form.
123
+ #
124
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/gather) are passed in as a hash
125
+ #
126
+ # Examples:
127
+ # Twilio::Verb.gather
128
+ # Twilio::Verb.gather(:action => 'http://foobar.com')
129
+ # Twilio::Verb.gather(:finishOnKey => '*')
130
+ # Twilio::Verb.gather(:action => 'http://foobar.com', :finishOnKey => '*')
131
+ #
132
+ # Gather also lets you nest the Play, Say, and Pause verbs:
133
+ #
134
+ # verb = Twilio::Verb.new { |v|
135
+ # v.gather(:action => '/process_gather', :method => 'GET) {
136
+ # v.say('Please enter your account number followed by the pound sign')
137
+ # }
138
+ # v.say("We didn't receive any input. Goodbye!")
139
+ # }
140
+ # verb.response # represents the final xml output
141
+ def gather(*args, &block)
142
+ options = args.shift || {}
143
+ output {
144
+ if block_given?
145
+ @xml.Gather(options) { block.call }
146
+ else
147
+ @xml.Gather(options)
148
+ end
149
+ }
150
+ end
151
+
152
+ #play, say, pause
153
+
154
+ # The Record verb records the caller's voice and returns a URL that links to a file
155
+ # containing the audio recording.
156
+ #
157
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/record) are passed in as a hash
158
+ #
159
+ # Examples:
160
+ # Twilio::Verb.record
161
+ # Twilio::Verb.record(:action => 'http://foobar.com')
162
+ # Twilio::Verb.record(:finishOnKey => '*')
163
+ # Twilio::Verb.record(:transcribe => true, :transcribeCallback => '/handle_transcribe')
164
+ def record(*args)
165
+ options = args.shift
166
+ output { @xml.Record(options) }
167
+ end
168
+
169
+ # The Dial verb connects the current caller to an another phone. If the called party picks up,
170
+ # the two parties are connected and can communicate until one hangs up. If the called party does
171
+ # not pick up, if a busy signal is received, or the number doesn't exist, the dial verb will finish.
172
+ #
173
+ # If an action verb is provided, Twilio will submit the outcome of the call attempt to the action URL.
174
+ # If no action is provided, Dial will fall through to the next verb in the document.
175
+ #
176
+ # Note: this is different than the behavior of Record and Gather. Dial does not submit back to the
177
+ # current document URL if no action is provided.
178
+ #
179
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/dial) are passed in as a hash
180
+ #
181
+ # Examples:
182
+ # Twilio::Verb.dial('415-123-4567')
183
+ # Twilio::Verb.dial('415-123-4567', :action => 'http://foobar.com')
184
+ # Twilio::Verb.dial('415-123-4567', {:timeout => 10, :callerId => '858-987-6543'})
185
+ #
186
+ # Twilio also supports an alternate form in which a Number object is nested inside Dial:
187
+ #
188
+ # verb = Twilio::Verb.new { |v|
189
+ # v.dial {
190
+ # v.number('415-123-4567')
191
+ # v.number('415-123-4568')
192
+ # v.number('415-123-4569')
193
+ # }
194
+ # }
195
+ # verb.response # represents the final xml output
196
+ def dial(*args, &block)
197
+ number_to_dial = ''
198
+ options = {}
199
+ args.each do |arg|
200
+ case arg
201
+ when String
202
+ number_to_dial = arg
203
+ when Hash
204
+ options.merge!(arg)
205
+ else
206
+ raise ArgumentError, 'dial expects String or Hash argument'
207
+ end
127
208
  end
128
209
 
129
- # The Dial verb connects the current caller to an another phone. If the called party picks up,
130
- # the two parties are connected and can communicate until one hangs up. If the called party does
131
- # not pick up, if a busy signal is received, or the number doesn't exist, the dial verb will finish.
132
- #
133
- # If an action verb is provided, Twilio will submit the outcome of the call attempt to the action URL.
134
- # If no action is provided, Dial will fall through to the next verb in the document.
135
- #
136
- # Note: this is different than the behavior of Record and Gather. Dial does not submit back to the
137
- # current document URL if no action is provided.
138
- #
139
- # Options (see http://www.twilio.com/docs/api_reference/TwiML/dial) are passed in as a hash
140
- #
141
- # Examples:
142
- # Twilio::Verb.dial('415-123-4567')
143
- # Twilio::Verb.dial('415-123-4567', :action => 'http://foobar.com')
144
- # Twilio::Verb.dial('415-123-4567', {:timeout => 10, :callerId => '858-987-6543'})
145
- def dial(*args, &block)
146
- number_to_dial = ''
147
- options = {}
148
- args.each do |arg|
149
- case arg
150
- when String
151
- number_to_dial = arg
152
- when Hash
153
- options.merge!(arg)
154
- else
155
- raise ArgumentError, 'dial expects String or Hash argument'
156
- end
210
+ output {
211
+ if block_given?
212
+ @xml.Dial(options) { block.call }
213
+ else
214
+ @xml.Dial(number_to_dial, options)
157
215
  end
216
+ }
217
+ end
158
218
 
159
- xml = Builder::XmlMarkup.new
160
- xml.instruct!
161
- xml.Response { xml.Dial(number_to_dial, options) }
162
- end
163
-
164
- def method_missing(method_id, *args) #:nodoc:
165
- if match = /(say|play|gather|record|dial)(_(\d+)_)times(_with_pause$*)/.match(method_id.to_s)
166
- verb = match.captures.first
167
- how_many_times = match.captures[1]
168
- pause = match.captures[2] == '_with_pause'
169
- self.send(verb, args.first, { :loop => Integer(how_many_times), :pause => pause})
219
+ # The Pause (secondary) verb waits silently for a number of seconds.
220
+ # It is normally chained with other verbs.
221
+ #
222
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/pause) are passed in as a hash
223
+ #
224
+ # Examples:
225
+ # verb = Twilio::Verb.new { |v|
226
+ # v.say('greetings')
227
+ # v.pause(:length => 2)
228
+ # v.say('have a nice day')
229
+ # }
230
+ # verb.response
231
+ def pause(*args)
232
+ options = args.shift
233
+ output { @xml.Pause(options) }
234
+ end
235
+
236
+ # The Redirect (secondary) verb transfers control to a different URL.
237
+ # It is normally chained with other verbs.
238
+ #
239
+ # Options (see http://www.twilio.com/docs/api_reference/TwiML/redirect) are passed in as a hash
240
+ #
241
+ # Examples:
242
+ # verb = Twilio::Verb.new { |v|
243
+ # v.dial('415-123-4567')
244
+ # v.redirect('http://www.foo.com/nextInstructions')
245
+ # }
246
+ # verb.response
247
+ def redirect(*args)
248
+ redirect_to_url = ''
249
+ options = {}
250
+ args.each do |arg|
251
+ case arg
252
+ when String
253
+ redirect_to_url = arg
254
+ when Hash
255
+ options.merge!(arg)
170
256
  else
171
- raise NoMethodError.new("Method --- #{method_id} --- not found")
257
+ raise ArgumentError, 'dial expects String or Hash argument'
172
258
  end
173
259
  end
174
-
175
- private
176
260
 
177
- def loop_with_pause(loop_count, xml, &verb_action)
178
- last_iteration = loop_count-1
179
- loop_count.times do |i|
180
- yield verb_action
181
- xml.Pause unless i == last_iteration
182
- end
261
+ output { @xml.Redirect(redirect_to_url, options) }
262
+ end
263
+
264
+ # The Hangup (secondary) verb ends the call.
265
+ #
266
+ # Examples:
267
+ # If your response is only a hangup:
268
+ #
269
+ # Twilio::Verb.hangup
270
+ #
271
+ # If your response is chained:
272
+ #
273
+ # verb = Twilio::Verb.new { |v|
274
+ # v.say("The time is #{Time.now}")
275
+ # v.hangup
276
+ # }
277
+ # verb.response
278
+ def hangup
279
+ output { @xml.Hangup }
280
+ end
281
+
282
+ # The Number element specifies a phone number. The number element has two optional attributes: sendDigits and url.
283
+ # Number elements can only be nested in Dial verbs
284
+ def number(*args)
285
+ number_to_dial = ''
286
+ options = {}
287
+ args.each do |arg|
288
+ case arg
289
+ when String
290
+ number_to_dial = arg
291
+ when Hash
292
+ options.merge!(arg)
293
+ else
294
+ raise ArgumentError, 'dial expects String or Hash argument'
183
295
  end
296
+ end
297
+
298
+ output { @xml.Number(number_to_dial, options) }
184
299
  end
300
+
301
+ private
302
+
303
+ def output
304
+ @chain ? yield : @xml.Response { yield }
305
+ end
306
+
307
+ def loop_with_pause(loop_count, xml, &verb_action)
308
+ last_iteration = loop_count-1
309
+ loop_count.times do |i|
310
+ yield verb_action
311
+ xml.Pause unless i == last_iteration
312
+ end
313
+ end
185
314
  end
186
315
  end
@@ -13,6 +13,18 @@ say_hi_three_times:
13
13
  say_hi_three_times_with_pause:
14
14
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Say language="en" voice="man">hi</Say><Pause/><Say language="en" voice="man">hi</Say><Pause/><Say language="en" voice="man">hi</Say></Response>
15
15
 
16
+ say_hi_with_pause:
17
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Say loop="1" language="en" voice="man">hi</Say><Pause/></Response>
18
+
19
+ say_hi_with_pause_and_say_bye:
20
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Say loop="1" language="en" voice="man">hi</Say><Pause></Pause><Say loop="1" language="en" voice="man">bye</Say></Response>
21
+
22
+ say_hi_with_2_second_pause_and_say_bye:
23
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Say loop="1" language="en" voice="man">hi</Say><Pause length="2"/><Say loop="1" language="en" voice="man">bye</Say></Response>
24
+
25
+ say_hi_and_hangup:
26
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Say loop="1" language="en" voice="man">hi</Say><Hangup/></Response>
27
+
16
28
  play_mp3:
17
29
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Play loop="1">http://foo.com/cowbell.mp3</Play></Response>
18
30
 
@@ -23,7 +35,7 @@ play_mp3_two_times_with_pause:
23
35
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Play>http://foo.com/cowbell.mp3</Play><Pause/><Play>http://foo.com/cowbell.mp3</Play></Response>
24
36
 
25
37
  gather:
26
- response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather></Gather></Response>
38
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather/></Response>
27
39
 
28
40
  gather_with_action:
29
41
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather action="http://foobar.com"/></Response>
@@ -41,8 +53,14 @@ gather_with_num_digits:
41
53
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather numDigits="5"/></Response>
42
54
 
43
55
  gather_with_all_options_set:
44
- response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather finishOnKey="*" method="GET" action="http://foobar.com" numDigits="5" timeout="10"/></Response>
56
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather finishOnKey="*" action="http://foobar.com" method="GET" numDigits="5" timeout="10"/></Response>
57
+
58
+ gather_and_say_instructions:
59
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather><Say loop="1" language="en" voice="man">Please enter your account number followed by the pound sign</Say></Gather><Say loop="1" language="en" voice="man">We didn't receive any input. Goodbye!</Say></Response>
45
60
 
61
+ gather_with_timeout_and_say_instructions:
62
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Gather timeout="10"><Say loop="1" language="en" voice="man">Please enter your account number followed by the pound sign</Say></Gather><Say loop="1" language="en" voice="man">We didn't receive any input. Goodbye!</Say></Response>
63
+
46
64
  record:
47
65
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Record></Record></Response>
48
66
 
@@ -86,4 +104,16 @@ dial_with_caller_id:
86
104
  response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial callerId="858-987-6543">415-123-4567</Dial></Response>
87
105
 
88
106
  dial_with_timeout_and_caller_id:
89
- response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial callerId="858-987-6543" timeout="10">415-123-4567</Dial></Response>
107
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial callerId="858-987-6543" timeout="10">415-123-4567</Dial></Response>
108
+
109
+ dial_with_redirect:
110
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial>415-123-4567</Dial><Redirect>http://www.foo.com/nextInstructions</Redirect></Response>
111
+
112
+ dial_with_number_and_send_digits:
113
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial><Number sendDigits="wwww1928">415-123-4567</Number></Dial></Response>
114
+
115
+ dial_multiple_numbers:
116
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Dial><Number>415-123-4567</Number><Number>415-123-4568</Number><Number>415-123-4569</Number></Dial></Response>
117
+
118
+ hangup:
119
+ response: <?xml version="1.0" encoding="UTF-8"?><Response><Hangup/></Response>
@@ -3,21 +3,33 @@ require File.dirname(__FILE__) + '/../test_helper'
3
3
  class AccountTest < Test::Unit::TestCase #:nodoc: all
4
4
  context "An account" do
5
5
  setup do
6
- @connection = Twilio::Connection.new('mysid', 'mytoken')
7
- @account = Twilio::Account.new(@connection)
6
+ Twilio.connect('mysid', 'mytoken')
8
7
  end
9
8
 
10
9
  should "be retrievable" do
11
10
  fake_response = fixture(:account)
12
11
  FakeWeb.register_uri(:get, twilio_url, :string => fake_response)
13
- assert_equal @account.get, fake_response
12
+ assert_equal Twilio::Account.get, fake_response
14
13
  end
15
14
 
16
15
  should "be able to update name" do
17
16
  fake_response = fixture(:account_renamed)
18
17
  FakeWeb.register_uri(:put, twilio_url, :string => fake_response)
19
- response = @account.update_name('Bubba')
20
- assert_equal response, fake_response
18
+ assert_equal Twilio::Account.update_name('Bubba'), fake_response
21
19
  end
20
+
21
+ context "using deprecated API" do
22
+ setup do
23
+ @connection = Twilio::Connection.new('mysid', 'mytoken')
24
+ @account = Twilio::Account.new(@connection)
25
+ end
26
+
27
+ should "be retrievable" do
28
+ fake_response = fixture(:account)
29
+ FakeWeb.register_uri(:get, twilio_url, :string => fake_response)
30
+ assert_equal @account.get, fake_response
31
+ end
32
+ end
33
+
22
34
  end
23
35
  end
@@ -3,26 +3,25 @@ require File.dirname(__FILE__) + '/../test_helper'
3
3
  class CallTest < Test::Unit::TestCase #:nodoc: all
4
4
  context "A call" do
5
5
  setup do
6
- @connection = Twilio::Connection.new('mysid', 'mytoken')
7
- @call = Twilio::Call.new(@connection)
6
+ Twilio.connect('mysid', 'mytoken')
8
7
  end
9
8
 
10
9
  should "be retrievable as a list" do
11
10
  fake_response = fixture(:calls)
12
11
  FakeWeb.register_uri(:get, twilio_url('Calls'), :string => fake_response)
13
- assert_equal @call.list, fake_response
12
+ assert_equal Twilio::Call.list, fake_response
14
13
  end
15
14
 
16
15
  should "be retrievable individually" do
17
16
  fake_response = fixture(:call)
18
17
  FakeWeb.register_uri(:get, twilio_url('Calls/CA42ed11f93dc08b952027ffbc406d0868'), :string => fake_response)
19
- assert_equal @call.get('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
18
+ assert_equal Twilio::Call.get('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
20
19
  end
21
20
 
22
21
  should "be made" do
23
22
  fake_response = fixture(:call_new)
24
23
  FakeWeb.register_uri(:post, twilio_url('Calls'), :string => fake_response)
25
- response = @call.make('4158675309', '4155551212', 'http://test.local/call_handler')
24
+ response = Twilio::Call.make('4158675309', '4155551212', 'http://test.local/call_handler')
26
25
  assert_equal response, fake_response
27
26
  end
28
27
 
@@ -30,13 +29,13 @@ class CallTest < Test::Unit::TestCase #:nodoc: all
30
29
  should "returns a list of Call resources that were segments created in the same call" do
31
30
  fake_response = fixture(:calls)
32
31
  FakeWeb.register_uri(:get, twilio_url('Calls/CA42ed11f93dc08b952027ffbc406d0868/Segments'), :string => fake_response)
33
- assert_equal @call.segments('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
32
+ assert_equal Twilio::Call.segments('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
34
33
  end
35
34
 
36
35
  should "returns a single Call resource for the CallSid and CallSegmentSid provided" do
37
36
  fake_response = fixture(:calls)
38
37
  FakeWeb.register_uri(:get, twilio_url('Calls/CA42ed11f93dc08b952027ffbc406d0868/Segments/abc123'), :string => fake_response)
39
- assert_equal @call.segments('CA42ed11f93dc08b952027ffbc406d0868', 'abc123'), fake_response
38
+ assert_equal Twilio::Call.segments('CA42ed11f93dc08b952027ffbc406d0868', 'abc123'), fake_response
40
39
  end
41
40
  end
42
41
 
@@ -44,7 +43,7 @@ class CallTest < Test::Unit::TestCase #:nodoc: all
44
43
  should "returns a list of recordings that were generated during the call" do
45
44
  fake_response = fixture(:recordings)
46
45
  FakeWeb.register_uri(:get, twilio_url('Calls/CA42ed11f93dc08b952027ffbc406d0868/Recordings'), :string => fake_response)
47
- assert_equal @call.recordings('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
46
+ assert_equal Twilio::Call.recordings('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
48
47
  end
49
48
  end
50
49
 
@@ -52,8 +51,22 @@ class CallTest < Test::Unit::TestCase #:nodoc: all
52
51
  should "description" do
53
52
  fake_response = fixture(:notifications)
54
53
  FakeWeb.register_uri(:get, twilio_url('Calls/CA42ed11f93dc08b952027ffbc406d0868/Notifications'), :string => fake_response)
55
- assert_equal @call.notifications('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
54
+ assert_equal Twilio::Call.notifications('CA42ed11f93dc08b952027ffbc406d0868'), fake_response
56
55
  end
57
56
  end
57
+
58
+ context "using deprecated API" do
59
+ setup do
60
+ @connection = Twilio::Connection.new('mysid', 'mytoken')
61
+ @call = Twilio::Call.new(@connection)
62
+ end
63
+
64
+ should "be made" do
65
+ fake_response = fixture(:call_new)
66
+ FakeWeb.register_uri(:post, twilio_url('Calls'), :string => fake_response)
67
+ assert_equal @call.make('4158675309', '4155551212', 'http://test.local/call_handler'), fake_response
68
+ end
69
+ end
70
+
58
71
  end
59
72
  end