dwolla-ruby 2.9.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.travis.yml +11 -6
- data/Gemfile +1 -1
- data/README.md +225 -222
- data/Rakefile +8 -8
- data/dwolla-ruby.gemspec +27 -27
- data/examples/balance.rb +15 -15
- data/examples/contacts.rb +32 -32
- data/examples/fundingSources.rb +39 -39
- data/examples/oauth.rb +59 -59
- data/examples/offsiteGateway.rb +31 -31
- data/examples/transactions.rb +72 -72
- data/examples/users.rb +30 -30
- data/gemfiles/json.gemfile +2 -2
- data/lib/dwolla.rb +319 -327
- data/lib/dwolla/accounts.rb +27 -27
- data/lib/dwolla/balance.rb +15 -15
- data/lib/dwolla/contacts.rb +30 -30
- data/lib/dwolla/errors/api_connection_error.rb +3 -3
- data/lib/dwolla/errors/api_error.rb +3 -3
- data/lib/dwolla/errors/authentication_error.rb +3 -3
- data/lib/dwolla/errors/dwolla_error.rb +19 -19
- data/lib/dwolla/errors/invalid_request_error.rb +10 -10
- data/lib/dwolla/errors/missing_parameter_error.rb +3 -3
- data/lib/dwolla/exceptions.rb +4 -4
- data/lib/dwolla/funding_sources.rb +65 -65
- data/lib/dwolla/json.rb +20 -20
- data/lib/dwolla/masspay.rb +52 -52
- data/lib/dwolla/oauth.rb +84 -84
- data/lib/dwolla/offsite_gateway.rb +152 -152
- data/lib/dwolla/requests.rb +56 -56
- data/lib/dwolla/transactions.rb +108 -108
- data/lib/dwolla/users.rb +39 -39
- data/lib/dwolla/version.rb +3 -3
- data/test/test_accounts.rb +18 -18
- data/test/test_balance.rb +9 -9
- data/test/test_contacts.rb +19 -19
- data/test/test_dwolla.rb +62 -0
- data/test/test_funding_sources.rb +64 -64
- data/test/test_masspay.rb +47 -47
- data/test/test_oauth.rb +36 -36
- data/test/test_offsite_gateway.rb +57 -57
- data/test/test_requests.rb +29 -29
- data/test/test_transactions.rb +117 -117
- data/test/test_users.rb +33 -33
- metadata +5 -3
data/examples/users.rb
CHANGED
@@ -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')
|
data/gemfiles/json.gemfile
CHANGED
@@ -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"
|
data/lib/dwolla.rb
CHANGED
@@ -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://
|
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
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
:
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
end
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
end
|
302
|
-
|
303
|
-
def self.
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
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
|