Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: twilio/twilio-ruby
...
head fork: twilio/twilio-ruby
  • 19 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
Commits on Feb 03, 2012
@andrewmbenton andrewmbenton bumping version number for development 088e161
@andrewmbenton andrewmbenton remove the old deprecated request interface 5261fa5
Commits on Feb 28, 2012
@andrewmbenton andrewmbenton setting up travis-ci 6fc1fd9
@andrewmbenton andrewmbenton Merge branch 'develop' of github.com:twilio/twilio-ruby into develop a159a21
@andrewmbenton andrewmbenton removing travis config since it's coming in from master f73e1d7
@andrewmbenton andrewmbenton Merge branch 'master' into develop 94bc857
@andrewmbenton andrewmbenton make bundler happy on rubies > 1.8.7 424ef92
@andrewmbenton andrewmbenton Merge branch 'master' into develop 8c153b4
@andrewmbenton andrewmbenton Merge branch 'master' into develop 0bf07ac
Commits on Mar 14, 2012
@rafael rafael Add more granurality to client errors.
* Add specific exception for invalid number and international sms unavailable errors.
* Raise these exceptions in the case the error code is 21401 or 21408 respectively.
68c3bd7
Commits on Mar 15, 2012
@rafael rafael Set code error in RequestError exceptions ed464aa
Commits on Mar 24, 2012
@andrewmbenton andrewmbenton Merge branch 'master' of git://github.com/rafael/twilio-ruby into raf…
…ael-master-exceptions
502b94c
@andrewmbenton andrewmbenton some refactoring for style 1f5d860
@andrewmbenton andrewmbenton refactoring to make reading simpler d79f684
@andrewmbenton andrewmbenton an initial (partially broken) implementation of paging via next/previ…
…ous page uris
6025cb0
@andrewmbenton andrewmbenton getting closer 05e00ab
@andrewmbenton andrewmbenton actually return the thing and not just an empty array every time 1d9f0f9
@andrewmbenton andrewmbenton chop off the end of the uri if we are in full_uri mode 3f65d5c
@andrewmbenton andrewmbenton more examples 14ce815
View
35 examples.rb → examples/examples.rb
@@ -2,7 +2,7 @@
@account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
@auth_token = '62ea81de3a5b414154eb263595357c69'
-# set up a client, without any http requests
+# set up a client
@client = Twilio::REST::Client.new(@account_sid, @auth_token)
################ ACCOUNTS ################
@@ -13,50 +13,49 @@
# list your (sub)accounts
@client.accounts.list
-# grab an account instance resource if you know the sid (no http request)
+# grab an account instance resource if you know the sid
@account = @client.accounts.get(@account_sid)
# http round trip happens here
puts @account.friendly_name
-# update an account's friendly name (only one http request, for the POST)
+# update an account's friendly name
@client.accounts.get(@account_sid).update(:friendly_name => 'A Fabulous Friendly Name')
################ CALLS ################
-# print a list of calls (all parameters optional, single http request)
+# print a list of calls (all parameters optional)
@account.calls.list({:page => 0, :page_size => 1000, :start_time => '2010-09-01'}).each do |call|
puts call.sid
end
-# get a particular call and list its recording urls (one http request for #list)
+# get a particular call and list its recording urls
@account.calls.get('CAXXXXXXX').recordings.list.each do {|r| puts r.wav}
-# make a new outgoing call (this is the same type of object we get
-# from calls.get, except this has attributes since we made an http request)
+# make a new outgoing call. returns a call object just like calls.get
@call = @account.calls.create({:from => '+14159341234', :to => '+18004567890', :url => 'http://myapp.com/call-handler'})
-# cancel the call if not already in progress (single http request)
+# cancel the call if not already in progress
@account.calls.get(@call.sid).update({:status => 'canceled'})
-# or equivalently (single http request)
+# or equivalently
@call.update({:status => 'canceled'})
# or simply
@call.cancel
-# redirect and then terminate a call (each one http request)
+# redirect and then terminate a call
@account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:url => 'http://myapp.com/call-redirect'})
-@account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:status => 'completed'}) # formerly @call.hangup
+@account.calls.get('CA386025c9bf5d6052a1d1ea42b4d16662').update({:status => 'completed'})
# or, use the aliases...
@call.redirect_to('http://myapp.com/call-redirect')
@call.hangup
################ SMS MESSAGES ################
-# print a list of sms messages (one http request)
+# print a list of sms messages
@account.sms.messages.list({:date_sent => '2010-09-01'}).each do |sms|
puts sms.body
end
-# print a particular sms message (one http request)
+# print a particular sms message
puts @account.sms.messages.get('SMXXXXXXXX').body
# send an sms
@@ -67,22 +66,22 @@
# get a list of supported country codes
@account.available_phone_numbers.list
-# print some available numbers (only one http request)
+# print some available numbers
@numbers = @account.available_phone_numbers.get('US').local.list({:contains => 'AWESOME'})
@numbers.each {|num| puts num.phone_number}
-# buy the first one (one http request)
+# buy the first one
@account.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number)
-# update an existing phone number's voice url (one http request)
+# update an existing phone number's voice url
@account.incoming_phone_numbers.get('PNdba508c5616a7f5e141789f44f022cc3').update({:voice_url => 'http://myapp.com/voice'})
################ CONFERENCES ################
-# get a particular conference's participants object and stash it (should be zero http requests)
+# get a particular conference's participants object and stash it
@participants = @account.conferences.get('CFbbe46ff1274e283f7e3ac1df0072ab39').participants
-# list participants (http request here)
+# list participants
@participants.list.each do {|p| puts p.sid}
# update a conference participant
View
24 examples/print-call-log.rb
@@ -0,0 +1,24 @@
+require 'rubygems'
+require 'twilio-ruby'
+
+# print a list of all phone calls, what phone number each was to/from, and how
+# much each one cost.
+
+# put your Twilio credentials here. you can find your AccountSid and AuthToken
+# at the top of your account dashboard page located at:
+# https://www.twilio.com/user/account
+account_sid = 'AC043dcf9844e04758bc3a36a84c29761'
+auth_token = '62ea81de3a5b414154eb263595357c69'
+
+# set up a client
+client = Twilio::REST::Client.new(account_sid, auth_token)
+
+calls = client.account.calls.list
+
+begin
+ calls.each do |call|
+ price = call.price || '0.00' # since apparently prices can be nil...
+ puts call.sid + "\t" + call.from + "\t" + call.to + "\t" + price
+ end
+ calls = calls.next_page
+end while not calls.empty?
View
53 lib/twilio-ruby/rest/client.rb
@@ -152,8 +152,10 @@ def inspect # :nodoc:
method_class = Net::HTTP.const_get method.to_s.capitalize
define_method method do |uri, *args|
params = twilify args[0]; params = {} if params.empty?
- uri = "#{uri}.json" # create a local copy of the uri to manipulate
- uri << "?#{url_encode(params)}" if method == :get && !params.empty?
+ unless args[1]
+ uri = "#{uri}.json" # create a local copy of the uri to manipulate
+ uri << "?#{url_encode(params)}" if method == :get && !params.empty?
+ end
request = method_class.new uri, HTTP_HEADERS
request.basic_auth @account_sid, @auth_token
request.form_data = params if [:post, :put].include? method
@@ -161,42 +163,6 @@ def inspect # :nodoc:
end
end
- ##
- # Mimic the old (deprecated) interface. Make an HTTP request to Twilio
- # using the given +method+ and +uri+. If the +method+ is <tt>'GET'</tt>
- # then +params+ are appended to the +uri+ as urlencoded query parameters.
- # If the +method+ is <tt>'POST'</tt> or <tt>'PUT'</tt> then +params+ are
- # passed as an application/x-www-form-urlencoded string in the request
- # body.
- #
- # Returns the raw Net::HTTP::Response object.
- def request(uri, method='POST', params={}) # :nodoc:
- raise ArgumentError, 'Invalid path parameter' if uri.empty?
-
- uri = "/#{uri}" unless uri.start_with? '/'
-
- case method.upcase
- when 'GET'
- uri << "?#{url_encode(params)}" if params
- req = Net::HTTP::Get.new uri
- when 'DELETE'
- req = Net::HTTP::Delete.new uri
- when 'PUT'
- req = Net::HTTP::Put.new uri
- req.form_data = params
- when 'POST'
- req = Net::HTTP::Post.new uri
- req.form_data = params
- else
- raise NotImplementedError, "HTTP #{method} not implemented"
- end
-
- req['User-Agent'] = 'twilio-ruby/deprecated'
- req.basic_auth @account_sid, @auth_token
- @last_request = req
- @last_response = @connection.request req
- end
-
private
##
@@ -241,7 +207,7 @@ def set_up_subresources # :doc:
# inspection later.
def connect_and_send(request) # :doc:
@last_request = request
- retries_remaining = @config[:retry_limit]
+ retries_left = @config[:retry_limit]
begin
response = @connection.request request
@last_response = response
@@ -250,15 +216,10 @@ def connect_and_send(request) # :doc:
raise Twilio::REST::ServerError, object['message']
end
rescue Exception
- if retries_remaining > 0
- retries_remaining -= 1
- retry
- else
- raise
- end
+ if retries_left > 0 then retries_left -= 1; retry else raise end
end
if response.kind_of? Net::HTTPClientError
- raise Twilio::REST::RequestError, object['message']
+ raise Twilio::REST::RequestError.new object['message'], object['code']
end
object
end
View
10 lib/twilio-ruby/rest/errors.rb
@@ -1,6 +1,14 @@
module Twilio
module REST
- class RequestError < StandardError; end
class ServerError < StandardError; end
+
+ class RequestError < StandardError
+ attr_reader :code
+
+ def initialize(message, code=nil);
+ super message
+ @code = code
+ end
+ end
end
end
View
17 lib/twilio-ruby/rest/list_resource.rb
@@ -24,18 +24,27 @@ def inspect # :nodoc:
#
# The optional +params+ hash allows you to filter the list returned. The
# filters for each list resource type are defined by Twilio.
- def list(params = {})
+ def list(params={}, full_uri=false)
raise "Can't get a resource list without a REST Client" unless @client
- response = @client.get @uri, params
+ response = @client.get @uri, params, full_uri
resources = response[@list_key]
+ uri = full_uri ? @uri.split('.')[0] : @uri
resource_list = resources.map do |resource|
- @instance_class.new "#{@uri}/#{resource[@instance_id_key]}", @client,
+ @instance_class.new "#{uri}/#{resource[@instance_id_key]}", @client,
resource
end
- # set the +total+ property on the array
+ # set the +total+ and +next_page+ properties on the array
+ client, list_class = @client, self.class
resource_list.instance_eval do
eigenclass = class << self; self; end
eigenclass.send :define_method, :total, &lambda {response['total']}
+ eigenclass.send :define_method, :next_page, &lambda {
+ if response['next_page_uri']
+ list_class.new(response['next_page_uri'], client).list({}, true)
+ else
+ []
+ end
+ }
end
resource_list
end
View
2  lib/twilio-ruby/version.rb
@@ -1,3 +1,3 @@
module Twilio
- VERSION = '3.5.1'
+ VERSION = '3.6.0'
end

No commit comments for this range

Something went wrong with that request. Please try again.