Class: Trails::Twilio::Account
- Inherits:
-
Object
- Object
- Trails::Twilio::Account
- Defined in:
- lib/trails/twilio/account.rb
Constant Summary collapse
- MAX_SMS_LENGTH =
160
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
-
#call(number, handler_url, opts = {}) ⇒ Object
Make outgoing calls: Required: - number - handler_url.
-
#incoming_numbers(reset = false) ⇒ Object
Sample Response: [“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”}].
-
#initialize(opts = {}) ⇒ Account
constructor
A new instance of Account.
- #outgoing_numbers(reset = false) ⇒ Object
-
#provision_number(options = {}) ⇒ Object
options: [:area_code, :friendly_name, :url, :sms_url] sameple return: “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”}.
- #release_number(sid) ⇒ Object
-
#request(resource, method = 'GET', params = {}) ⇒ Object
just specify the resource (e.g. ‘Calls’ ) and it will append it to the base uri (“/#api_version/Accounts/#sid/”) and then call twilio.
-
#send_sms(number, body, opts = {}) ⇒ Object
Required: - number: to - body: text.
- #verify_caller(request) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Account
Returns a new instance of Account.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/trails/twilio/account.rb', line 5 def initialize( opts = {} ) _logger = opts[:logger] || ActiveRecord::Base.logger rescue Logger.new( STDERR ) if( !opts.blank? ) _logger.warn "overriding default opts #{self.class.config.inspect} with #{opts.inspect}" else opts = self.class.config[self.class.config.keys.first] end @config = opts.dup @sid = @config[:sid] || raise( "no sid specified on #{self}" ) @token = @config[:token] @logger = _logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/trails/twilio/account.rb', line 4 def config @config end |
Class Method Details
.from_request(request) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/trails/twilio/account.rb', line 22 def self.from_request( request ) sid = sid_from_request( request ) unless( config.has_key?( sid ) ) logger.warn{ "unknown twilio account #{sid}. Request params: #{request.inspect}" } raise Trails::Exception::UnknownAccount.new( sid ) end account = new( config[sid].dup ) raise Trails::Exception::InvalidSignature unless account.verify_caller( request ) account end |
.sid_from_request(request) ⇒ Object
18 19 20 |
# File 'lib/trails/twilio/account.rb', line 18 def self.sid_from_request( request ) ( :development == Rails.env.to_sym ) ? request.params['AccountSid'] : request.env["HTTP_X_TWILIO_ACCOUNTSID"] end |
Instance Method Details
#call(number, handler_url, opts = {}) ⇒ Object
Make outgoing calls: Required:
-
number
-
handler_url
Options:
-
:caller
-
:method
-
:timeout
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/trails/twilio/account.rb', line 47 def call( number, handler_url, opts = {} ) params = { 'Caller' => opts['Caller'] || opts[:caller], 'Called' => number, 'Url' => handler_url, 'Method' => opts['Method'] || opts[:method] || 'GET', 'Timeout' => opts['Timeout'] || opts[:timeout] || 15 } if if_machine = opts[:if_machine] params['IfMachine'] = if_machine.to_s.capitalize end request( 'Calls', 'POST', params ) end |
#incoming_numbers(reset = false) ⇒ Object
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"}]
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/trails/twilio/account.rb', line 84 def incoming_numbers( reset = false ) if( @incoming_numbers.nil? || reset ) response = request( 'IncomingPhoneNumbers', 'GET' ) if( 200 == response.code.to_i ) @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['TwilioResponse']['IncomingPhoneNumbers']['IncomingPhoneNumber']].flatten # returns an array even when it's a single entry end return @incoming_numbers end |
#outgoing_numbers(reset = false) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/trails/twilio/account.rb', line 99 def outgoing_numbers( reset = false ) if( @outgoing_numbers.nil? || reset ) response = 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 |
#provision_number(options = {}) ⇒ Object
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"}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/trails/twilio/account.rb', line 131 def provision_number( = {} ) params = {} [:area_code, :friendly_name, :url, :sms_url].each do |key| params[key.to_s.camelize] = [key] if .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 |
#release_number(sid) ⇒ Object
146 147 148 |
# File 'lib/trails/twilio/account.rb', line 146 def release_number( sid ) request( File.join( 'IncomingPhoneNumbers', sid ), 'DELETE' ) end |
#request(resource, method = 'GET', params = {}) ⇒ Object
just specify the resource (e.g. ‘Calls’ ) and it will append it to the base uri (“/#api_version/Accounts/#sid/”) and then call twilio.
153 154 155 156 |
# File 'lib/trails/twilio/account.rb', line 153 def request( resource, method = 'GET', params = {}) url = File.join( base_uri, resource ) make_request( url, method, params ) end |
#send_sms(number, body, opts = {}) ⇒ Object
Required:
-
number: to
-
body: text
Options:
-
:from: number
-
:method: GET/POST
72 73 74 75 76 77 78 79 80 |
# File 'lib/trails/twilio/account.rb', line 72 def send_sms( number, body, opts = {} ) params = { 'From' => opts[:from] || @config[:default_number], 'To' => number, 'Body' => body, 'Method' => opts[:method] || 'POST' } request( 'SMS/Messages', 'POST', params ) end |
#verify_caller(request) ⇒ Object
33 34 35 36 |
# File 'lib/trails/twilio/account.rb', line 33 def verify_caller( request ) # TODO: check caller credentials here. :) return true end |