Permalink
Browse files

- update documentation

- refactoring
- update to test helper
  • Loading branch information...
1 parent 2a7f8ae commit c2bc83f4f0ba7ef9df4cefa6cb3b8aa6058a056c Hemant Bhanoo committed Apr 11, 2010
Showing with 60 additions and 12 deletions.
  1. +0 −1 Manifest.txt
  2. +1 −1 README.txt
  3. +2 −0 lib/trails/test_helper.rb
  4. +57 −10 lib/trails/twilio/account.rb
View
@@ -1,4 +1,3 @@
-.autotest
History.txt
Manifest.txt
README.txt
View
@@ -14,7 +14,7 @@ Support for SMS, twiml MimeType alias, functional test helpers.
class ApplicationController < ActionController::Base
..
- include Twilio::CallHandling
+ include Trails::Twilio::CallHandling
..
end
@@ -104,9 +104,11 @@ def modify_params_with_twilio_opts( params, as_twilio_opts )
caller = as_twilio_opts[:caller] || '4155551212'
called = as_twilio_opts[:called] || '6155556161'
from = as_twilio_opts[:from] || '6665554321'
+ to = as_twilio_opts[:to] || '3334445678'
params['Caller'] = caller
params['Called'] = called
params['From'] = from
+ params['To'] = to
params['SmsMessageSid'] = 'DummyMessageSid' if( as_twilio_opts[:sms] )
end
@@ -52,8 +52,7 @@ def call( number, handler_url, opts = {} )
'Method' => opts['Method'] || opts[:method] || 'GET',
'Timeout' => opts['Timeout'] || opts[:timeout] || 15
}
- api_version = opts[:api_version] || '2008-08-01'
- make_request( File.join( base_uri, 'Calls' ), 'POST', params )
+ request( 'Calls', 'POST', params )
end
MAX_SMS_LENGTH = 160
@@ -72,38 +71,86 @@ def send_sms( number, body, opts = {} )
'Body' => body,
'Method' => opts[:method] || 'POST'
}
- url = File.join( base_uri, 'SMS/Messages' )
- make_request(url, 'POST', params )
+ request( 'SMS/Messages', 'POST', params )
end
+ # Sample Response:
+ # [{"SmsFallbackUrl"=>nil, "SmsUrl"=>nil, "PhoneNumber"=>"4253954994", "AccountSid"=>"AC6c25b3d8b4f0a2a4e49e4936398a2180", "Capabilities"=>{"SMS"=>"false", "Voice"=>"false"}, "Method"=>"POST", "Sid"=>"PNc939a026d5a332d22c23ca94a161ce29", "DateUpdated"=>"Sat, 20 Mar 2010 17:52:33 -0700", "DateCreated"=>"Sat, 20 Mar 2010 17:52:33 -0700", "Url"=>nil, "FriendlyName"=>"(425) 395-4994", "VoiceFallbackUrl"=>nil, "SmsFallbackMethod"=>"POST", "VoiceCallerIdLookup"=>"false", "SmsMethod"=>"POST", "VoiceFallbackMethod"=>"POST"}]
def incoming_numbers( reset = false )
if( @incoming_numbers.nil? || reset )
response =
- make_request( File.join( base_uri, 'IncomingPhoneNumbers' ), 'GET' )
+ request( 'IncomingPhoneNumbers', 'GET' )
if( 200 == response.code.to_i )
- @raw_incoming_numbers = Hpricot( response.body )
+ @raw_incoming_numbers = Hash.from_xml( response.body )
else
raise "got response code #{response.code} and body #{response.body}"
end
- @incoming_numbers = @raw_incoming_numbers.search( '//phonenumber').
- collect{|j| j.inner_html}
+ @incoming_numbers = [@raw_incoming_numbers['TwilioResponse']['IncomingPhoneNumbers']['IncomingPhoneNumber']].flatten # returns an array even when it's a single entry
end
return @incoming_numbers
end
def outgoing_numbers( reset = false )
if( @outgoing_numbers.nil? || reset )
response =
- make_request( "/2008-08-01/Accounts/#{@sid}/OutgoingCallerIds",
- 'GET' )
+ request( 'OutgoingCallerIds', 'GET' )
@outgoing_numbers_raw = Hpricot( response.body ) if( 200 == response.code.to_i )
@outgoing_numbers = @outgoing_numbers_raw.search( '//phonenumber').
collect{|j| j.inner_html}
end
return @outgoing_numbers
end
+ # options: [:area_code, :friendly_name, :url, :sms_url]
+ # sameple return:
+ # {"SmsFallbackUrl"=>nil,
+ # "SmsUrl"=>nil,
+ # "PhoneNumber"=>"4253954994",
+ # "AccountSid"=>"AC6c25b3d8b4f0a2a4e49e4936398a2180",
+ # "Capabilities"=>{"SMS"=>"false",
+ # "Voice"=>"false"},
+ # "Method"=>"POST",
+ # "Sid"=>"PNc939a026d5a332d22c23ca94a161ce29",
+ # "DateUpdated"=>"Sat,
+ # 20 Mar 2010 17:52:33 -0700",
+ # "DateCreated"=>"Sat,
+ # 20 Mar 2010 17:52:33 -0700",
+ # "Url"=>nil,
+ # "FriendlyName"=>"(425) 395-4994",
+ # "VoiceFallbackUrl"=>nil,
+ # "SmsFallbackMethod"=>"POST",
+ # "VoiceCallerIdLookup"=>"false",
+ # "SmsMethod"=>"POST",
+ # "VoiceFallbackMethod"=>"POST"}
+ def provision_number( options = {} )
+ params = {}
+ [:area_code, :friendly_name, :url, :sms_url].each do |key|
+ params[key.to_s.camelize] = options[key] if options.has_key?( key )
+ end
+
+ response = request( 'IncomingPhoneNumbers/Local', 'POST', params )
+ if( 201 == response.code.to_i )
+ raw_number_response = Hash.from_xml( response.body )
+ else
+ raise "while trying to acquire a new number, got response #{response.code} and body: #{response.body}"
+ end
+ raw_number_response["TwilioResponse"]["IncomingPhoneNumber"]
+ end
+
+ def release_number( sid )
+ request( File.join( 'IncomingPhoneNumbers', sid ), 'DELETE' )
+ end
+
+ # just specify the resource (e.g. 'Calls' ) and it will
+ # append it to the base uri ("/#{api_version}/Accounts/#{sid}/")
+ # and then call twilio.
+ def request( resource, method = 'GET', params = {})
+ url = File.join( base_uri, resource )
+ make_request( url, method, params )
+ end
+
+
protected

0 comments on commit c2bc83f

Please sign in to comment.