twilio-ruby 3.5.1 → 3.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,8 +25,8 @@ require 'rubygems' # not necessary with ruby 1.9 but included for completeness
25
25
  require 'twilio-ruby'
26
26
 
27
27
  # put your own credentials here
28
- account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
29
- auth_token = '62ea81de3a5b414154eb263595357c69'
28
+ account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
29
+ auth_token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
30
30
 
31
31
  # set up a client to talk to the Twilio REST API
32
32
  @client = Twilio::REST::Client.new account_sid, auth_token
@@ -2,7 +2,7 @@
2
2
 
3
3
  @account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
4
4
  @auth_token = '62ea81de3a5b414154eb263595357c69'
5
- # set up a client, without any http requests
5
+ # set up a client
6
6
  @client = Twilio::REST::Client.new(@account_sid, @auth_token)
7
7
 
8
8
  ################ ACCOUNTS ################
@@ -13,50 +13,49 @@
13
13
  # list your (sub)accounts
14
14
  @client.accounts.list
15
15
 
16
- # grab an account instance resource if you know the sid (no http request)
16
+ # grab an account instance resource if you know the sid
17
17
  @account = @client.accounts.get(@account_sid)
18
18
  # http round trip happens here
19
19
  puts @account.friendly_name
20
20
 
21
- # update an account's friendly name (only one http request, for the POST)
21
+ # update an account's friendly name
22
22
  @client.accounts.get(@account_sid).update(:friendly_name => 'A Fabulous Friendly Name')
23
23
 
24
24
  ################ CALLS ################
25
25
 
26
- # print a list of calls (all parameters optional, single http request)
26
+ # print a list of calls (all parameters optional)
27
27
  @account.calls.list({:page => 0, :page_size => 1000, :start_time => '2010-09-01'}).each do |call|
28
28
  puts call.sid
29
29
  end
30
30
 
31
- # get a particular call and list its recording urls (one http request for #list)
31
+ # get a particular call and list its recording urls
32
32
  @account.calls.get('CAXXXXXXX').recordings.list.each do {|r| puts r.wav}
33
33
 
34
- # make a new outgoing call (this is the same type of object we get
35
- # from calls.get, except this has attributes since we made an http request)
34
+ # make a new outgoing call. returns a call object just like calls.get
36
35
  @call = @account.calls.create({:from => '+14159341234', :to => '+18004567890', :url => 'http://myapp.com/call-handler'})
37
36
 
38
- # cancel the call if not already in progress (single http request)
37
+ # cancel the call if not already in progress
39
38
  @account.calls.get(@call.sid).update({:status => 'canceled'})
40
- # or equivalently (single http request)
39
+ # or equivalently
41
40
  @call.update({:status => 'canceled'})
42
41
  # or simply
43
42
  @call.cancel
44
43
 
45
- # redirect and then terminate a call (each one http request)
44
+ # redirect and then terminate a call
46
45
  @account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:url => 'http://myapp.com/call-redirect'})
47
- @account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:status => 'completed'}) # formerly @call.hangup
46
+ @account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:status => 'completed'})
48
47
  # or, use the aliases...
49
48
  @call.redirect_to('http://myapp.com/call-redirect')
50
49
  @call.hangup
51
50
 
52
51
  ################ SMS MESSAGES ################
53
52
 
54
- # print a list of sms messages (one http request)
53
+ # print a list of sms messages
55
54
  @account.sms.messages.list({:date_sent => '2010-09-01'}).each do |sms|
56
55
  puts sms.body
57
56
  end
58
57
 
59
- # print a particular sms message (one http request)
58
+ # print a particular sms message
60
59
  puts @account.sms.messages.get('SMXXXXXXXX').body
61
60
 
62
61
  # send an sms
@@ -67,22 +66,22 @@ puts @account.sms.messages.get('SMXXXXXXXX').body
67
66
  # get a list of supported country codes
68
67
  @account.available_phone_numbers.list
69
68
 
70
- # print some available numbers (only one http request)
69
+ # print some available numbers
71
70
  @numbers = @account.available_phone_numbers.get('US').local.list({:contains => 'AWESOME'})
72
71
  @numbers.each {|num| puts num.phone_number}
73
72
 
74
- # buy the first one (one http request)
73
+ # buy the first one
75
74
  @account.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number)
76
75
 
77
- # update an existing phone number's voice url (one http request)
76
+ # update an existing phone number's voice url
78
77
  @account.incoming_phone_numbers.get('PNdba508c5616a7f5e141789f44f022cc3').update({:voice_url => 'http://myapp.com/voice'})
79
78
 
80
79
  ################ CONFERENCES ################
81
80
 
82
- # get a particular conference's participants object and stash it (should be zero http requests)
81
+ # get a particular conference's participants object and stash it
83
82
  @participants = @account.conferences.get('CFbbe46ff1274e283f7e3ac1df0072ab39').participants
84
83
 
85
- # list participants (http request here)
84
+ # list participants
86
85
  @participants.list.each do {|p| puts p.sid}
87
86
 
88
87
  # update a conference participant
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'twilio-ruby'
3
+
4
+ # print a list of all phone calls, what phone number each was to/from, and how
5
+ # much each one cost.
6
+
7
+ # put your Twilio credentials here. you can find your AccountSid and AuthToken
8
+ # at the top of your account dashboard page located at:
9
+ # https://www.twilio.com/user/account
10
+ account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
11
+ auth_token = '62ea81de3a5b414154eb263595357c69'
12
+
13
+ # set up a client
14
+ client = Twilio::REST::Client.new(account_sid, auth_token)
15
+
16
+ calls = client.account.calls.list
17
+
18
+ begin
19
+ calls.each do |call|
20
+ price = call.price || '0.00' # since apparently prices can be nil...
21
+ puts call.sid + "\t" + call.from + "\t" + call.to + "\t" + price
22
+ end
23
+ calls = calls.next_page
24
+ end while not calls.empty?
@@ -152,8 +152,10 @@ module Twilio
152
152
  method_class = Net::HTTP.const_get method.to_s.capitalize
153
153
  define_method method do |uri, *args|
154
154
  params = twilify args[0]; params = {} if params.empty?
155
- uri = "#{uri}.json" # create a local copy of the uri to manipulate
156
- uri << "?#{url_encode(params)}" if method == :get && !params.empty?
155
+ unless args[1]
156
+ uri = "#{uri}.json" # create a local copy of the uri to manipulate
157
+ uri << "?#{url_encode(params)}" if method == :get && !params.empty?
158
+ end
157
159
  request = method_class.new uri, HTTP_HEADERS
158
160
  request.basic_auth @account_sid, @auth_token
159
161
  request.form_data = params if [:post, :put].include? method
@@ -161,42 +163,6 @@ module Twilio
161
163
  end
162
164
  end
163
165
 
164
- ##
165
- # Mimic the old (deprecated) interface. Make an HTTP request to Twilio
166
- # using the given +method+ and +uri+. If the +method+ is <tt>'GET'</tt>
167
- # then +params+ are appended to the +uri+ as urlencoded query parameters.
168
- # If the +method+ is <tt>'POST'</tt> or <tt>'PUT'</tt> then +params+ are
169
- # passed as an application/x-www-form-urlencoded string in the request
170
- # body.
171
- #
172
- # Returns the raw Net::HTTP::Response object.
173
- def request(uri, method='POST', params={}) # :nodoc:
174
- raise ArgumentError, 'Invalid path parameter' if uri.empty?
175
-
176
- uri = "/#{uri}" unless uri.start_with? '/'
177
-
178
- case method.upcase
179
- when 'GET'
180
- uri << "?#{url_encode(params)}" if params
181
- req = Net::HTTP::Get.new uri
182
- when 'DELETE'
183
- req = Net::HTTP::Delete.new uri
184
- when 'PUT'
185
- req = Net::HTTP::Put.new uri
186
- req.form_data = params
187
- when 'POST'
188
- req = Net::HTTP::Post.new uri
189
- req.form_data = params
190
- else
191
- raise NotImplementedError, "HTTP #{method} not implemented"
192
- end
193
-
194
- req['User-Agent'] = 'twilio-ruby/deprecated'
195
- req.basic_auth @account_sid, @auth_token
196
- @last_request = req
197
- @last_response = @connection.request req
198
- end
199
-
200
166
  private
201
167
 
202
168
  ##
@@ -241,7 +207,7 @@ module Twilio
241
207
  # inspection later.
242
208
  def connect_and_send(request) # :doc:
243
209
  @last_request = request
244
- retries_remaining = @config[:retry_limit]
210
+ retries_left = @config[:retry_limit]
245
211
  begin
246
212
  response = @connection.request request
247
213
  @last_response = response
@@ -250,15 +216,10 @@ module Twilio
250
216
  raise Twilio::REST::ServerError, object['message']
251
217
  end
252
218
  rescue Exception
253
- if retries_remaining > 0
254
- retries_remaining -= 1
255
- retry
256
- else
257
- raise
258
- end
219
+ if retries_left > 0 then retries_left -= 1; retry else raise end
259
220
  end
260
221
  if response.kind_of? Net::HTTPClientError
261
- raise Twilio::REST::RequestError, object['message']
222
+ raise Twilio::REST::RequestError.new object['message'], object['code']
262
223
  end
263
224
  object
264
225
  end
@@ -1,6 +1,14 @@
1
1
  module Twilio
2
2
  module REST
3
- class RequestError < StandardError; end
4
3
  class ServerError < StandardError; end
4
+
5
+ class RequestError < StandardError
6
+ attr_reader :code
7
+
8
+ def initialize(message, code=nil);
9
+ super message
10
+ @code = code
11
+ end
12
+ end
5
13
  end
6
14
  end
@@ -24,18 +24,27 @@ module Twilio
24
24
  #
25
25
  # The optional +params+ hash allows you to filter the list returned. The
26
26
  # filters for each list resource type are defined by Twilio.
27
- def list(params = {})
27
+ def list(params={}, full_uri=false)
28
28
  raise "Can't get a resource list without a REST Client" unless @client
29
- response = @client.get @uri, params
29
+ response = @client.get @uri, params, full_uri
30
30
  resources = response[@list_key]
31
+ uri = full_uri ? @uri.split('.')[0] : @uri
31
32
  resource_list = resources.map do |resource|
32
- @instance_class.new "#{@uri}/#{resource[@instance_id_key]}", @client,
33
+ @instance_class.new "#{uri}/#{resource[@instance_id_key]}", @client,
33
34
  resource
34
35
  end
35
- # set the +total+ property on the array
36
+ # set the +total+ and +next_page+ properties on the array
37
+ client, list_class = @client, self.class
36
38
  resource_list.instance_eval do
37
39
  eigenclass = class << self; self; end
38
40
  eigenclass.send :define_method, :total, &lambda {response['total']}
41
+ eigenclass.send :define_method, :next_page, &lambda {
42
+ if response['next_page_uri']
43
+ list_class.new(response['next_page_uri'], client).list({}, true)
44
+ else
45
+ []
46
+ end
47
+ }
39
48
  end
40
49
  resource_list
41
50
  end
@@ -1,3 +1,3 @@
1
1
  module Twilio
2
- VERSION = '3.5.1'
2
+ VERSION = '3.6.0'
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'lib/twilio-ruby/version'
1
+ require './lib/twilio-ruby/version'
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'twilio-ruby'
@@ -24,6 +24,6 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'fakeweb', '~> 1.3.0'
25
25
  s.add_development_dependency 'rack', '~> 1.3.0'
26
26
 
27
- s.extra_rdoc_files = ['README.md', 'examples.rb', 'LICENSE']
27
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
28
28
  s.rdoc_options = ['--line-numbers', '--inline-source', '--title', 'twilio-ruby', '--main', 'README.md']
29
29
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twilio-ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 3
8
- - 5
9
- - 1
10
- version: 3.5.1
8
+ - 6
9
+ - 0
10
+ version: 3.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Benton
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-21 00:00:00 -08:00
18
+ date: 2012-03-24 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -138,16 +138,17 @@ extensions: []
138
138
 
139
139
  extra_rdoc_files:
140
140
  - README.md
141
- - examples.rb
142
141
  - LICENSE
143
142
  files:
144
143
  - .gitignore
144
+ - .travis.yml
145
145
  - Gemfile
146
146
  - LICENSE
147
147
  - README.md
148
148
  - Rakefile
149
149
  - conf/cacert.pem
150
- - examples.rb
150
+ - examples/examples.rb
151
+ - examples/print-call-log.rb
151
152
  - lib/twilio-ruby.rb
152
153
  - lib/twilio-ruby/rest/accounts.rb
153
154
  - lib/twilio-ruby/rest/applications.rb