dwolla-ruby 2.9.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -8
  3. data/.travis.yml +11 -6
  4. data/Gemfile +1 -1
  5. data/README.md +225 -222
  6. data/Rakefile +8 -8
  7. data/dwolla-ruby.gemspec +27 -27
  8. data/examples/balance.rb +15 -15
  9. data/examples/contacts.rb +32 -32
  10. data/examples/fundingSources.rb +39 -39
  11. data/examples/oauth.rb +59 -59
  12. data/examples/offsiteGateway.rb +31 -31
  13. data/examples/transactions.rb +72 -72
  14. data/examples/users.rb +30 -30
  15. data/gemfiles/json.gemfile +2 -2
  16. data/lib/dwolla.rb +319 -327
  17. data/lib/dwolla/accounts.rb +27 -27
  18. data/lib/dwolla/balance.rb +15 -15
  19. data/lib/dwolla/contacts.rb +30 -30
  20. data/lib/dwolla/errors/api_connection_error.rb +3 -3
  21. data/lib/dwolla/errors/api_error.rb +3 -3
  22. data/lib/dwolla/errors/authentication_error.rb +3 -3
  23. data/lib/dwolla/errors/dwolla_error.rb +19 -19
  24. data/lib/dwolla/errors/invalid_request_error.rb +10 -10
  25. data/lib/dwolla/errors/missing_parameter_error.rb +3 -3
  26. data/lib/dwolla/exceptions.rb +4 -4
  27. data/lib/dwolla/funding_sources.rb +65 -65
  28. data/lib/dwolla/json.rb +20 -20
  29. data/lib/dwolla/masspay.rb +52 -52
  30. data/lib/dwolla/oauth.rb +84 -84
  31. data/lib/dwolla/offsite_gateway.rb +152 -152
  32. data/lib/dwolla/requests.rb +56 -56
  33. data/lib/dwolla/transactions.rb +108 -108
  34. data/lib/dwolla/users.rb +39 -39
  35. data/lib/dwolla/version.rb +3 -3
  36. data/test/test_accounts.rb +18 -18
  37. data/test/test_balance.rb +9 -9
  38. data/test/test_contacts.rb +19 -19
  39. data/test/test_dwolla.rb +62 -0
  40. data/test/test_funding_sources.rb +64 -64
  41. data/test/test_masspay.rb +47 -47
  42. data/test/test_oauth.rb +36 -36
  43. data/test/test_offsite_gateway.rb +57 -57
  44. data/test/test_requests.rb +29 -29
  45. data/test/test_transactions.rb +117 -117
  46. data/test/test_users.rb +33 -33
  47. metadata +5 -3
@@ -1,31 +1,31 @@
1
- # Include the Dwolla gem
2
- require 'rubygems'
3
- require 'pp'
4
- require 'dwolla'
5
-
6
- # Include any required keys
7
- require '_keys.rb'
8
-
9
- # Instantiate a new Dwolla User client
10
- # And, seed a previously generated access token
11
- Dwolla::token = @token
12
- Dwolla::api_key = @api_key
13
- Dwolla::api_secret = @api_secret
14
-
15
- # EXAMPLE 1:
16
- # Fetch account information for the
17
- # account associated with the provided
18
- # OAuth token
19
- pp Dwolla::Users.get
20
-
21
-
22
- # EXAMPLE 2:
23
- # Fetch basic account information
24
- # for a given Dwolla ID
25
- pp Dwolla::Users.get('812-626-8794')
26
-
27
-
28
- # EXAMPLE 3:
29
- # Fetch basic account information
30
- # for a given Email address
1
+ # Include the Dwolla gem
2
+ require 'rubygems'
3
+ require 'pp'
4
+ require 'dwolla'
5
+
6
+ # Include any required keys
7
+ require '_keys.rb'
8
+
9
+ # Instantiate a new Dwolla User client
10
+ # And, seed a previously generated access token
11
+ Dwolla::token = @token
12
+ Dwolla::api_key = @api_key
13
+ Dwolla::api_secret = @api_secret
14
+
15
+ # EXAMPLE 1:
16
+ # Fetch account information for the
17
+ # account associated with the provided
18
+ # OAuth token
19
+ pp Dwolla::Users.get
20
+
21
+
22
+ # EXAMPLE 2:
23
+ # Fetch basic account information
24
+ # for a given Dwolla ID
25
+ pp Dwolla::Users.get('812-626-8794')
26
+
27
+
28
+ # EXAMPLE 3:
29
+ # Fetch basic account information
30
+ # for a given Email address
31
31
  pp Dwolla::Users.get('michael@dwolla.com')
@@ -1,3 +1,3 @@
1
- source "https://rubygems.org"
2
- gemspec :path => File.join(File.dirname(__FILE__), "..")
1
+ source "https://rubygems.org"
2
+ gemspec :path => File.join(File.dirname(__FILE__), "..")
3
3
  gem "json"
@@ -1,327 +1,319 @@
1
- # Dwolla Ruby API Wrapper
2
- # Heavily based off Stripe's Ruby Gem
3
- # API spec at https://developers.dwolla.com
4
- require 'openssl'
5
- require 'rest_client'
6
- require 'multi_json'
7
- require 'addressable/uri'
8
-
9
- # Version
10
- require_relative 'dwolla/version'
11
-
12
- # Resources
13
- require_relative 'dwolla/json'
14
- require_relative 'dwolla/transactions'
15
- require_relative 'dwolla/requests'
16
- require_relative 'dwolla/contacts'
17
- require_relative 'dwolla/users'
18
- require_relative 'dwolla/balance'
19
- require_relative 'dwolla/funding_sources'
20
- require_relative 'dwolla/oauth'
21
- require_relative 'dwolla/offsite_gateway'
22
- require_relative 'dwolla/accounts'
23
- require_relative 'dwolla/masspay'
24
-
25
- # Errors
26
- require_relative 'dwolla/errors/dwolla_error'
27
- require_relative 'dwolla/errors/api_connection_error'
28
- require_relative 'dwolla/errors/api_error'
29
- require_relative 'dwolla/errors/missing_parameter_error'
30
- require_relative 'dwolla/errors/authentication_error'
31
- require_relative 'dwolla/errors/invalid_request_error'
32
-
33
- module Dwolla
34
- @@api_key = nil
35
- @@api_secret = nil
36
- @@token = nil
37
- @@api_base = '/oauth/rest'
38
- @@verify_ssl_certs = true
39
- @@api_version = nil
40
- @@debug = false
41
- @@sandbox = false
42
- @@scope = 'send|transactions|balance|request|contacts|accountinfofull|funding|scheduled'
43
-
44
- def self.api_key=(api_key)
45
- @@api_key = api_key
46
- end
47
-
48
- def self.api_key
49
- @@api_key
50
- end
51
-
52
- def self.api_secret=(api_secret)
53
- @@api_secret = api_secret
54
- end
55
-
56
- def self.api_secret
57
- @@api_secret
58
- end
59
-
60
- def self.sandbox=(sandbox)
61
- @@sandbox = sandbox
62
- end
63
-
64
- def self.sandbox
65
- @@sandbox
66
- end
67
-
68
- def self.debug
69
- @@debug
70
- end
71
-
72
- def self.debug=(debug)
73
- @@debug = debug
74
- end
75
-
76
- def self.api_version=(api_version)
77
- @@api_version = api_version
78
- end
79
-
80
- def self.api_version
81
- @@api_version
82
- end
83
-
84
- def self.verify_ssl_certs=(verify_ssl_certs)
85
- @@verify_ssl_certs = verify_ssl_certs
86
- end
87
-
88
- def self.verify_ssl_certs
89
- @@verify_ssl_certs
90
- end
91
-
92
- def self.token=(token)
93
- @@token = token
94
- end
95
-
96
- def self.token
97
- @@token
98
- end
99
-
100
- def self.scope=(scope)
101
- @@scope = scope
102
- end
103
-
104
- def self.scope
105
- @@scope
106
- end
107
-
108
- def self.hostname
109
- if not @@sandbox
110
- return 'https://www.dwolla.com'
111
- else
112
- return 'https://uat.dwolla.com'
113
- end
114
- end
115
-
116
- def self.endpoint_url(endpoint)
117
- self.hostname + @@api_base + endpoint
118
- end
119
-
120
- def self.request(method, url, params={}, headers={}, oauth=true, parse_response=true, custom_url=false)
121
- # if oauth is nil, assume default [true]
122
- oauth = true if oauth.nil?
123
-
124
- # figure out which auth to use
125
- if oauth and not params[:oauth_token]
126
- if not oauth.is_a?(TrueClass) # was token passed in the oauth param?
127
- params = {
128
- :oauth_token => oauth
129
- }.merge(params)
130
- else
131
- raise AuthenticationError.new('No OAuth Token Provided.') unless token
132
- params = {
133
- :oauth_token => token
134
- }.merge(params)
135
- end
136
- elsif oauth and params[:oauth_token]
137
- raise AuthenticationError.new('No OAuth Token Provided.') unless params[:oauth_token]
138
- else not oauth
139
- raise AuthenticationError.new('No App Key & Secret Provided.') unless (api_key && api_secret)
140
- params = {
141
- :client_id => api_key,
142
- :client_secret => api_secret
143
- }.merge(params)
144
- end
145
-
146
- if !verify_ssl_certs
147
- $stderr.puts "WARNING: Running without SSL cert verification."
148
- else
149
- ssl_opts = {
150
- :use_ssl => true
151
- }
152
- end
153
-
154
- uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
155
- lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
156
- ua = {
157
- :bindings_version => Dwolla::VERSION,
158
- :lang => 'ruby',
159
- :lang_version => lang_version,
160
- :platform => RUBY_PLATFORM,
161
- :publisher => 'dwolla',
162
- :uname => uname
163
- }
164
-
165
- url = self.endpoint_url(url) unless custom_url
166
-
167
- case method.to_s.downcase.to_sym
168
- when :get || :delete
169
- # Make params into GET/DELETE parameters
170
- if params && params.count > 0
171
- uri = Addressable::URI.new
172
- uri.query_values = params
173
- url += '?' + uri.query
174
- end
175
- payload = nil
176
- else
177
- payload = JSON.dump(params)
178
- end
179
-
180
- begin
181
- headers = { :x_dwolla_client_user_agent => Dwolla::JSON.dump(ua) }.merge(headers)
182
- rescue => e
183
- headers = {
184
- :x_dwolla_client_raw_user_agent => ua.inspect,
185
- :error => "#{e} (#{e.class})"
186
- }.merge(headers)
187
- end
188
-
189
- headers = {
190
- :user_agent => "Dwolla Ruby API Wrapper/#{Dwolla::VERSION}",
191
- :content_type => 'application/json'
192
- }.merge(headers)
193
-
194
- if self.api_version
195
- headers[:dwolla_version] = self.api_version
196
- end
197
-
198
- opts = {
199
- :method => method,
200
- :url => url,
201
- :headers => headers,
202
- :open_timeout => 30,
203
- :payload => payload,
204
- :timeout => 80
205
- }.merge(ssl_opts)
206
-
207
- if self.debug
208
- if self.sandbox
209
- puts "[DWOLLA SANDBOX MODE OPERATION]"
210
- end
211
-
212
- puts "Firing request with options and headers:"
213
- puts opts
214
- puts headers
215
- end
216
-
217
- begin
218
- response = execute_request(opts)
219
- rescue SocketError => e
220
- self.handle_restclient_error(e)
221
- rescue NoMethodError => e
222
- # Work around RestClient bug
223
- if e.message =~ /\WRequestFailed\W/
224
- e = APIConnectionError.new('Unexpected HTTP response code')
225
- self.handle_restclient_error(e)
226
- else
227
- raise
228
- end
229
- rescue RestClient::ExceptionWithResponse => e
230
- if rcode = e.http_code and rbody = e.http_body
231
- self.handle_api_error(rcode, rbody)
232
- else
233
- self.handle_restclient_error(e)
234
- end
235
- rescue RestClient::Exception, Errno::ECONNREFUSED => e
236
- self.handle_restclient_error(e)
237
- end
238
-
239
- rbody = response.body
240
- rcode = response.code
241
-
242
- if self.debug
243
- puts "Raw response headers received:"
244
- puts headers
245
- puts "Raw response body received:"
246
- puts rbody
247
- end
248
-
249
- resp = self.extract_json(rbody, rcode)
250
-
251
- if parse_response
252
- return self.parse_response(resp)
253
- else
254
- return resp
255
- end
256
- end
257
-
258
- private
259
-
260
- def self.execute_request(opts)
261
- RestClient::Request.execute(opts)
262
- end
263
-
264
- def self.extract_json(rbody, rcode)
265
- begin
266
- resp = Dwolla::JSON.load(rbody)
267
- rescue MultiJson::DecodeError
268
- raise APIError.new("There was an error parsing Dwolla's API response: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
269
- end
270
-
271
- return resp
272
- end
273
-
274
- def self.parse_response(resp)
275
- raise APIConnectionError.new("Network issue / unserializable response. Please try again.") unless resp.is_a?(Hash)
276
- raise APIError.new(resp['Message']) unless resp.has_key?('Success') and resp['Success'] == true
277
-
278
- return resp['Response']
279
- end
280
-
281
- def self.handle_api_error(rcode, rbody)
282
- begin
283
- error_obj = Dwolla::JSON.load(rbody)
284
- error = error_obj[:error] or raise DwollaError.new # escape from parsing
285
- rescue MultiJson::DecodeError, DwollaError
286
- raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
287
- end
288
-
289
- case rcode
290
- when 400, 404 then
291
- raise invalid_request_error(error, rcode, rbody, error_obj)
292
- when 401
293
- raise authentication_error(error, rcode, rbody, error_obj)
294
- else
295
- raise api_error(error, rcode, rbody, error_obj)
296
- end
297
- end
298
-
299
- def self.invalid_request_error(error, rcode, rbody, error_obj)
300
- InvalidRequestError.new(error[:message], error[:param], rcode, rbody, error_obj)
301
- end
302
-
303
- def self.authentication_error(error, rcode, rbody, error_obj)
304
- AuthenticationError.new(error[:message], rcode, rbody, error_obj)
305
- end
306
-
307
- def self.api_error(error, rcode, rbody, error_obj)
308
- APIError.new(error[:message], rcode, rbody, error_obj)
309
- end
310
-
311
- def self.handle_restclient_error(e)
312
- case e
313
- when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
314
- message = "Could not connect to Dwolla (#{@@api_base}). Please check your internet connection and try again. If this problem persists, you should check Dwolla's service status at https://twitter.com/Dwolla, or let us know at support@Dwolla.com."
315
- when RestClient::SSLCertificateNotVerified
316
- message = "Could not verify Dwolla's SSL certificate. If this problem persists, let us know at support@dwolla.com."
317
- when SocketError
318
- message = "Unexpected error communicating when trying to connect to Dwolla. If this problem persists, let us know at support@dwolla.com."
319
- else
320
- message = "Unexpected error communicating with Dwolla. If this problem persists, let us know at support@dwolla.com."
321
- end
322
-
323
- message += "\n\n(Network error: #{e.message})"
324
-
325
- raise APIConnectionError.new(message)
326
- end
327
- end
1
+ # Dwolla Ruby API Wrapper
2
+ # Heavily based off Stripe's Ruby Gem
3
+ # API spec at https://developers.dwolla.com
4
+ require 'openssl'
5
+ require 'rest_client'
6
+ require 'multi_json'
7
+ require 'addressable/uri'
8
+
9
+ # Version
10
+ require_relative 'dwolla/version'
11
+
12
+ # Resources
13
+ require_relative 'dwolla/json'
14
+ require_relative 'dwolla/transactions'
15
+ require_relative 'dwolla/requests'
16
+ require_relative 'dwolla/contacts'
17
+ require_relative 'dwolla/users'
18
+ require_relative 'dwolla/balance'
19
+ require_relative 'dwolla/funding_sources'
20
+ require_relative 'dwolla/oauth'
21
+ require_relative 'dwolla/offsite_gateway'
22
+ require_relative 'dwolla/accounts'
23
+ require_relative 'dwolla/masspay'
24
+
25
+ # Errors
26
+ require_relative 'dwolla/errors/dwolla_error'
27
+ require_relative 'dwolla/errors/api_connection_error'
28
+ require_relative 'dwolla/errors/api_error'
29
+ require_relative 'dwolla/errors/missing_parameter_error'
30
+ require_relative 'dwolla/errors/authentication_error'
31
+ require_relative 'dwolla/errors/invalid_request_error'
32
+
33
+ module Dwolla
34
+ @@api_key = nil
35
+ @@api_secret = nil
36
+ @@token = nil
37
+ @@api_base = '/oauth/rest'
38
+ @@verify_ssl_certs = true
39
+ @@api_version = nil
40
+ @@debug = false
41
+ @@sandbox = false
42
+ @@scope = 'send|transactions|balance|request|contacts|accountinfofull|funding|scheduled'
43
+
44
+ def self.api_key=(api_key)
45
+ @@api_key = api_key
46
+ end
47
+
48
+ def self.api_key
49
+ @@api_key
50
+ end
51
+
52
+ def self.api_secret=(api_secret)
53
+ @@api_secret = api_secret
54
+ end
55
+
56
+ def self.api_secret
57
+ @@api_secret
58
+ end
59
+
60
+ def self.sandbox=(sandbox)
61
+ @@sandbox = sandbox
62
+ end
63
+
64
+ def self.sandbox
65
+ @@sandbox
66
+ end
67
+
68
+ def self.debug
69
+ @@debug
70
+ end
71
+
72
+ def self.debug=(debug)
73
+ @@debug = debug
74
+ end
75
+
76
+ def self.api_version=(api_version)
77
+ @@api_version = api_version
78
+ end
79
+
80
+ def self.api_version
81
+ @@api_version
82
+ end
83
+
84
+ def self.verify_ssl_certs=(verify_ssl_certs)
85
+ @@verify_ssl_certs = verify_ssl_certs
86
+ end
87
+
88
+ def self.verify_ssl_certs
89
+ @@verify_ssl_certs
90
+ end
91
+
92
+ def self.token=(token)
93
+ @@token = token
94
+ end
95
+
96
+ def self.token
97
+ @@token
98
+ end
99
+
100
+ def self.scope=(scope)
101
+ @@scope = scope
102
+ end
103
+
104
+ def self.scope
105
+ @@scope
106
+ end
107
+
108
+ def self.hostname
109
+ if not @@sandbox
110
+ return 'https://www.dwolla.com'
111
+ else
112
+ return 'https://sandbox.dwolla.com'
113
+ end
114
+ end
115
+
116
+ def self.endpoint_url(endpoint)
117
+ self.hostname + @@api_base + endpoint
118
+ end
119
+
120
+ def self.request(method, url, params={}, headers={}, oauth=true, parse_response=true, custom_url=false)
121
+ self.extract_authorization(params, headers, oauth)
122
+
123
+ if !verify_ssl_certs
124
+ $stderr.puts "WARNING: Running without SSL cert verification."
125
+ else
126
+ ssl_opts = {
127
+ :use_ssl => true
128
+ }
129
+ end
130
+
131
+ uname = (@@uname ||= RUBY_PLATFORM =~ /linux|darwin/i ? `uname -a 2>/dev/null`.strip : nil)
132
+ lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
133
+ ua = {
134
+ :bindings_version => Dwolla::VERSION,
135
+ :lang => 'ruby',
136
+ :lang_version => lang_version,
137
+ :platform => RUBY_PLATFORM,
138
+ :publisher => 'dwolla',
139
+ :uname => uname
140
+ }
141
+
142
+ url = self.endpoint_url(url) unless custom_url
143
+
144
+ case method.to_s.downcase.to_sym
145
+ when :get || :delete
146
+ # Make params into GET/DELETE parameters
147
+ if params && params.count > 0
148
+ uri = Addressable::URI.new
149
+ uri.query_values = params
150
+ url += '?' + uri.query
151
+ end
152
+ payload = nil
153
+ else
154
+ payload = JSON.dump(params)
155
+ end
156
+
157
+ begin
158
+ headers = { :x_dwolla_client_user_agent => Dwolla::JSON.dump(ua) }.merge(headers)
159
+ rescue => e
160
+ headers = {
161
+ :x_dwolla_client_raw_user_agent => ua.inspect,
162
+ :error => "#{e} (#{e.class})"
163
+ }.merge(headers)
164
+ end
165
+
166
+ headers = {
167
+ :user_agent => "Dwolla Ruby API Wrapper/#{Dwolla::VERSION}",
168
+ :content_type => 'application/json'
169
+ }.merge(headers)
170
+
171
+ if self.api_version
172
+ headers[:dwolla_version] = self.api_version
173
+ end
174
+
175
+ opts = {
176
+ :method => method,
177
+ :url => url,
178
+ :headers => headers,
179
+ :open_timeout => 30,
180
+ :payload => payload,
181
+ :timeout => 80
182
+ }.merge(ssl_opts)
183
+
184
+ if self.debug
185
+ if self.sandbox
186
+ puts "[DWOLLA SANDBOX MODE OPERATION]"
187
+ end
188
+
189
+ puts "Firing request with options and headers:"
190
+ puts opts
191
+ puts headers
192
+ end
193
+
194
+ begin
195
+ response = execute_request(opts)
196
+ rescue SocketError => e
197
+ self.handle_restclient_error(e)
198
+ rescue NoMethodError => e
199
+ # Work around RestClient bug
200
+ if e.message =~ /\WRequestFailed\W/
201
+ e = APIConnectionError.new('Unexpected HTTP response code')
202
+ self.handle_restclient_error(e)
203
+ else
204
+ raise
205
+ end
206
+ rescue RestClient::ExceptionWithResponse => e
207
+ if rcode = e.http_code and rbody = e.http_body
208
+ self.handle_api_error(rcode, rbody)
209
+ else
210
+ self.handle_restclient_error(e)
211
+ end
212
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
213
+ self.handle_restclient_error(e)
214
+ end
215
+
216
+ rbody = response.body
217
+ rcode = response.code
218
+
219
+ if self.debug
220
+ puts "Raw response headers received:"
221
+ puts headers
222
+ puts "Raw response body received:"
223
+ puts rbody
224
+ end
225
+
226
+ resp = self.extract_json(rbody, rcode)
227
+
228
+ if parse_response
229
+ return self.parse_response(resp)
230
+ else
231
+ return resp
232
+ end
233
+ end
234
+
235
+ private
236
+
237
+ def self.execute_request(opts)
238
+ RestClient::Request.execute(opts)
239
+ end
240
+
241
+ def self.extract_json(rbody, rcode)
242
+ begin
243
+ resp = Dwolla::JSON.load(rbody)
244
+ rescue MultiJson::DecodeError
245
+ raise APIError.new("There was an error parsing Dwolla's API response: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
246
+ end
247
+
248
+ return resp
249
+ end
250
+
251
+ def self.parse_response(resp)
252
+ raise APIConnectionError.new("Network issue / unserializable response. Please try again.") unless resp.is_a?(Hash)
253
+ raise APIError.new(resp['Message']) unless resp.has_key?('Success') and resp['Success'] == true
254
+
255
+ return resp['Response']
256
+ end
257
+
258
+ def self.handle_api_error(rcode, rbody)
259
+ begin
260
+ error_obj = Dwolla::JSON.load(rbody)
261
+ error = error_obj[:error] or raise DwollaError.new # escape from parsing
262
+ rescue MultiJson::DecodeError, DwollaError
263
+ raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})", rcode, rbody)
264
+ end
265
+
266
+ case rcode
267
+ when 400, 404 then
268
+ raise invalid_request_error(error, rcode, rbody, error_obj)
269
+ when 401
270
+ raise authentication_error(error, rcode, rbody, error_obj)
271
+ else
272
+ raise api_error(error, rcode, rbody, error_obj)
273
+ end
274
+ end
275
+
276
+ def self.invalid_request_error(error, rcode, rbody, error_obj)
277
+ InvalidRequestError.new(error[:message], error[:param], rcode, rbody, error_obj)
278
+ end
279
+
280
+ def self.authentication_error(error, rcode, rbody, error_obj)
281
+ AuthenticationError.new(error[:message], rcode, rbody, error_obj)
282
+ end
283
+
284
+ def self.api_error(error, rcode, rbody, error_obj)
285
+ APIError.new(error[:message], rcode, rbody, error_obj)
286
+ end
287
+
288
+ def self.extract_authorization(params={}, headers={}, oauthToken=true)
289
+ paramsToken = params.delete(:oauth_token)
290
+ if oauthToken.is_a?(FalseClass)
291
+ raise AuthenticationError.new('No App Key & Secret Provided.') unless (api_key && api_secret)
292
+ params[:client_id] = api_key
293
+ params[:client_secret] = api_secret
294
+ else
295
+ providedAuthorization = oauthToken.is_a?(TrueClass) ? token : oauthToken
296
+ t = paramsToken || providedAuthorization
297
+ raise AuthenticationError.new('No OAuth Token Provided.') unless t
298
+ headers[:authorization] = "Bearer #{t}"
299
+ end
300
+
301
+ end
302
+
303
+ def self.handle_restclient_error(e)
304
+ case e
305
+ when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
306
+ message = "Could not connect to Dwolla (#{@@api_base}). Please check your internet connection and try again. If this problem persists, you should check Dwolla's service status at https://twitter.com/Dwolla, or let us know at support@Dwolla.com."
307
+ when RestClient::SSLCertificateNotVerified
308
+ message = "Could not verify Dwolla's SSL certificate. If this problem persists, let us know at support@dwolla.com."
309
+ when SocketError
310
+ message = "Unexpected error communicating when trying to connect to Dwolla. If this problem persists, let us know at support@dwolla.com."
311
+ else
312
+ message = "Unexpected error communicating with Dwolla. If this problem persists, let us know at support@dwolla.com."
313
+ end
314
+
315
+ message += "\n\n(Network error: #{e.message})"
316
+
317
+ raise APIConnectionError.new(message)
318
+ end
319
+ end