Class: TwilioRest::Account
- Inherits:
-
Object
- Object
- TwilioRest::Account
- Defined in:
- lib/trails/test_helper.rb,
lib/twiliorest.rb
Overview
open up the TwilioRest::Account class so that we can keep track of faked requests
Class Method Summary collapse
Instance Method Summary collapse
- #_build_get_uri(uri, params) ⇒ Object
- #_fetch(url, params, method = nil) ⇒ Object
- #_urlencode(params) ⇒ Object
-
#initialize(id, token) ⇒ Account
constructor
initialize a twilio account object.
-
#request(path, method = nil, vars = {}) ⇒ Object
sends a request and gets a response from the Twilio REST API.
- #request_with_fake(url, method, params) ⇒ Object
Constructor Details
#initialize(id, token) ⇒ Account
initialize a twilio account object
id: Twilio account SID/ID token: Twilio account token
returns a Twilio account object
42 43 44 45 |
# File 'lib/twiliorest.rb', line 42 def initialize(id, token) @id = id @token = token end |
Class Method Details
.faked_requests ⇒ Object
137 138 139 |
# File 'lib/trails/test_helper.rb', line 137 def self.faked_requests return @@fake_requests end |
Instance Method Details
#_build_get_uri(uri, params) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/twiliorest.rb', line 52 def _build_get_uri(uri, params) if params && params.length > 0 if uri.include?('?') if uri[-1, 1] != '&' uri += '&' end uri += _urlencode(params) else uri += '?' + _urlencode(params) end end return uri end |
#_fetch(url, params, method = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/twiliorest.rb', line 66 def _fetch(url, params, method=nil) if method && method == 'GET' url = _build_get_uri(url, params) end uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if method && method == 'GET' req = Net::HTTP::Get.new(uri.request_uri) elsif method && method == 'DELETE' req = Net::HTTP::Delete.new(uri.request_uri) elsif method && method == 'PUT' req = Net::HTTP::Put.new(uri.request_uri) req.set_form_data(params) else req = Net::HTTP::Post.new(uri.request_uri) req.set_form_data(params) end req.basic_auth(@id, @token) return http.request(req) end |
#_urlencode(params) ⇒ Object
47 48 49 50 |
# File 'lib/twiliorest.rb', line 47 def _urlencode(params) params.to_a.collect! \ { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&") end |
#request(path, method = nil, vars = {}) ⇒ Object
sends a request and gets a response from the Twilio REST API
path: the URL (relative to the endpoint URL, after the /v1 url: the HTTP method to use, defaults to POST vars: for POST or PUT, a dict of data to send
returns Twilio response XML or raises an exception on error
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/twiliorest.rb', line 98 def request(path, method=nil, vars={}) if !path || path.length < 1 raise ArgumentError, 'Invalid path parameter' end if method && !['GET', 'POST', 'DELETE', 'PUT'].include?(method) raise NotImplementedError, 'HTTP %s not implemented' % method end if path[0, 1] == '/' uri = TWILIO_API_URL + path else uri = TWILIO_API_URL + '/' + path end return _fetch(uri, vars, method) end |
#request_with_fake(url, method, params) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/trails/test_helper.rb', line 140 def request_with_fake( url, method, params ) @@fake_requests.push( OpenStruct.new( :url => url, :method => method, :params => params ) ) fake_response = OpenStruct.new fake_response.body = 'Fake Body' fake_response end |