Browse files

more examples

  • Loading branch information...
1 parent 3f65d5c commit 14ce8152b1b8ce5d4b755c4612d17155fc50c604 @andrewmbenton andrewmbenton committed Mar 24, 2012
Showing with 41 additions and 18 deletions.
  1. +17 −18 { → examples}/examples.rb
  2. +24 −0 examples/print-call-log.rb
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?

0 comments on commit 14ce815

Please sign in to comment.