dwolla-ruby 1.1.0 → 2.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.
Files changed (53) hide show
  1. data/dwolla-ruby.gemspec +16 -21
  2. data/examples/accountInfo.rb +7 -7
  3. data/examples/balance.rb +5 -5
  4. data/examples/contacts.rb +4 -4
  5. data/examples/fundingSources.rb +4 -4
  6. data/examples/oauth.rb +6 -7
  7. data/examples/transactions.rb +32 -0
  8. data/lib/dwolla.rb +258 -37
  9. data/lib/dwolla/balance.rb +15 -0
  10. data/lib/dwolla/contacts.rb +22 -0
  11. data/lib/dwolla/errors/api_connection_error.rb +4 -0
  12. data/lib/dwolla/errors/api_error.rb +4 -0
  13. data/lib/dwolla/errors/authentication_error.rb +4 -0
  14. data/lib/dwolla/errors/dwolla_error.rb +20 -0
  15. data/lib/dwolla/errors/invalid_request_error.rb +10 -0
  16. data/lib/dwolla/errors/missing_parameter_error.rb +4 -0
  17. data/lib/dwolla/funding_sources.rb +65 -0
  18. data/lib/dwolla/json.rb +21 -0
  19. data/lib/dwolla/oauth.rb +43 -0
  20. data/lib/dwolla/offsite_gateway.rb +76 -0
  21. data/lib/dwolla/requests.rb +56 -0
  22. data/lib/dwolla/transactions.rb +60 -0
  23. data/lib/dwolla/users.rb +36 -0
  24. data/lib/dwolla/version.rb +1 -1
  25. metadata +37 -108
  26. data/Gemfile +0 -4
  27. data/examples/send.rb +0 -23
  28. data/lib/dwolla/client.rb +0 -56
  29. data/lib/dwolla/connection.rb +0 -47
  30. data/lib/dwolla/funding_source.rb +0 -18
  31. data/lib/dwolla/response/follow_redirects.rb +0 -44
  32. data/lib/dwolla/response/guard_server_error.rb +0 -32
  33. data/lib/dwolla/response/parse_json.rb +0 -27
  34. data/lib/dwolla/transaction.rb +0 -56
  35. data/lib/dwolla/user.rb +0 -121
  36. data/spec/dwolla/client_spec.rb +0 -51
  37. data/spec/dwolla/funding_source_spec.rb +0 -59
  38. data/spec/dwolla/response/follow_redirects_spec.rb +0 -38
  39. data/spec/dwolla/transaction_spec.rb +0 -212
  40. data/spec/dwolla/user_spec.rb +0 -292
  41. data/spec/dwolla_spec.rb +0 -19
  42. data/spec/fixtures/account_information.json +0 -13
  43. data/spec/fixtures/add_funding_source.json +0 -11
  44. data/spec/fixtures/balance.json +0 -5
  45. data/spec/fixtures/basic_information.json +0 -6
  46. data/spec/fixtures/contacts.json +0 -18
  47. data/spec/fixtures/error.json +0 -5
  48. data/spec/fixtures/register.json +0 -9
  49. data/spec/fixtures/request_transaction.json +0 -3
  50. data/spec/fixtures/send_transaction.json +0 -3
  51. data/spec/fixtures/sources.json +0 -13
  52. data/spec/spec_helper.rb +0 -10
  53. data/spec/support/helpers.rb +0 -29
@@ -0,0 +1,15 @@
1
+ module Dwolla
2
+ class Balance
3
+ def self.get
4
+ url = balance_url
5
+
6
+ Dwolla.request(:get, url)
7
+ end
8
+
9
+ private
10
+
11
+ def self.balance_url
12
+ return '/balance/'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ module Dwolla
2
+ class Contacts
3
+ def self.get(filters={})
4
+ url = contacts_url
5
+
6
+ Dwolla.request(:get, url, filters)
7
+ end
8
+
9
+
10
+ def self.nearby(filters={})
11
+ url = contacts_url + 'nearby'
12
+
13
+ Dwolla.request(:get, url, filters, {}, false)
14
+ end
15
+
16
+ private
17
+
18
+ def self.contacts_url
19
+ return '/contacts/'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module Dwolla
2
+ class APIConnectionError < DwollaError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Dwolla
2
+ class APIError < DwollaError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Dwolla
2
+ class AuthenticationError < DwollaError
3
+ end
4
+ end
@@ -0,0 +1,20 @@
1
+ module Dwolla
2
+ class DwollaError < StandardError
3
+ attr_reader :message
4
+ attr_reader :http_status
5
+ attr_reader :http_body
6
+ attr_reader :json_body
7
+
8
+ def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil)
9
+ @message = message
10
+ @http_status = http_status
11
+ @http_body = http_body
12
+ @json_body = json_body
13
+ end
14
+
15
+ def to_s
16
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
17
+ "#{status_string}#{@message}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module Dwolla
2
+ class InvalidRequestError < DwollaError
3
+ attr_accessor :param
4
+
5
+ def initialize(message, param, http_status=nil, http_body=nil, json_body=nil)
6
+ super(message, http_status, http_body, json_body)
7
+ @param = param
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ module Dwolla
2
+ class MissingParameterError < DwollaError
3
+ end
4
+ end
@@ -0,0 +1,65 @@
1
+ module Dwolla
2
+ class FundingSources
3
+ def self.get(id=nil)
4
+ url = funding_sources_url
5
+
6
+ url += id.to_s unless id.nil?
7
+
8
+ Dwolla.request(:get, url)
9
+ end
10
+
11
+ def self.withdraw(id=nil, params={})
12
+ raise MissingParameterError.new('No Funding Source ID Provided.') if id.nil?
13
+ raise MissingParameterError.new('No PIN Provided.') unless params[:pin]
14
+ raise MissingParameterError.new('No Amount Provided.') unless params[:amount]
15
+
16
+ url = funding_sources_url
17
+ url += id.to_s + '/withdraw'
18
+
19
+ Dwolla.request(:post, url, params)
20
+ end
21
+
22
+ def self.deposit(id=nil, params={})
23
+ raise MissingParameterError.new('No Funding Source ID Provided.') if id.nil?
24
+ raise MissingParameterError.new('No PIN Provided.') unless params[:pin]
25
+ raise MissingParameterError.new('No Amount Provided.') unless params[:amount]
26
+
27
+ url = funding_sources_url
28
+ url += id.to_s + '/deposit'
29
+
30
+ Dwolla.request(:post, url, params)
31
+ end
32
+
33
+ def self.add(params={})
34
+ raise MissingParameterError.new('No Account Number Provided.') unless params[:account_number]
35
+ raise MissingParameterError.new('No Routing Number (ABA) Provided.') unless params[:routing_number]
36
+ raise MissingParameterError.new('No Account Type Provided.') unless params[:account_type]
37
+ raise MissingParameterError.new('No Account Name Provided.') unless params[:name]
38
+
39
+ url = funding_sources_url
40
+
41
+ Dwolla.request(:post, url, params)
42
+ end
43
+
44
+ def self.verify(id=nil, params={})
45
+ raise MissingParameterError.new('No Funding Source ID Provided.') if id.nil?
46
+ raise MissingParameterError.new('No Deposit 1 Amount Provided.') unless params[:deposit1]
47
+ raise MissingParameterError.new('No Deposit 2 Amount Provided.') unless params[:deposit2]
48
+
49
+ url = funding_sources_url
50
+ url += id.to_s + '/verify'
51
+
52
+ Dwolla.request(:post, url, params)
53
+ end
54
+
55
+ class << self
56
+ alias_method :listing, :get
57
+ end
58
+
59
+ private
60
+
61
+ def self.funding_sources_url
62
+ return '/fundingsources/'
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,21 @@
1
+ module Dwolla
2
+ module JSON
3
+ if MultiJson.respond_to?(:dump)
4
+ def self.dump(*args)
5
+ MultiJson.dump(*args)
6
+ end
7
+
8
+ def self.load(*args)
9
+ MultiJson.load(*args)
10
+ end
11
+ else
12
+ def self.dump(*args)
13
+ MultiJson.encode(*args)
14
+ end
15
+
16
+ def self.load(*args)
17
+ MultiJson.decode(*args)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ module Dwolla
2
+ class OAuth
3
+ def self.get_auth_url(redirect_uri=nil, scope='send|transactions|balance|request|contacts|accountinfofull|funding')
4
+ raise AuthenticationError.new('No Api Key Provided.') unless Dwolla::api_key
5
+
6
+ params = {
7
+ :scope => scope,
8
+ :response_type => 'code',
9
+ :client_id => Dwolla::api_key
10
+ }
11
+
12
+ params['redirect_uri'] = redirect_uri unless redirect_uri.nil?
13
+
14
+ uri = Addressable::URI.new
15
+ uri.query_values = params
16
+
17
+ return auth_url + '?' + uri.query
18
+ end
19
+
20
+ def self.get_token(code=nil, redirect_uri=nil)
21
+ raise MissingParameterError.new('No Code Provided.') if code.nil?
22
+
23
+ params = {
24
+ :grant_type => 'authorization_code',
25
+ :code => code
26
+ }
27
+
28
+ params['redirect_uri'] = redirect_uri unless redirect_uri.nil?
29
+
30
+ Dwolla.request(:get, token_url, params, {}, false, false, true)['access_token']
31
+ end
32
+
33
+ private
34
+
35
+ def self.auth_url
36
+ return 'https://www.dwolla.com/oauth/v2/authenticate'
37
+ end
38
+
39
+ def self.token_url
40
+ return 'https://www.dwolla.com/oauth/v2/token'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,76 @@
1
+ module Dwolla
2
+ class OffsiteGateway
3
+ @products = []
4
+ @discount = 0
5
+ @tax = 0
6
+ @shipping = 0
7
+ @destination_id = nil
8
+ @notes = nil
9
+ @facilitatorAmount = nil
10
+
11
+ def self.clear_session
12
+ @products = []
13
+ end
14
+
15
+ def self.add_product(name=nil, description=nil, price=nil, quantity=1)
16
+ @products.push {
17
+ :name => name,
18
+ :description => description,
19
+ :price => price,
20
+ :quantity => quantity
21
+ }
22
+ end
23
+
24
+ def self.discount=(discount)
25
+ @discount = discount
26
+ end
27
+
28
+ def self.tax=(tax)
29
+ @tax = tax
30
+ end
31
+
32
+ def self.shipping=(shipping)
33
+ @shipping = shipping
34
+ end
35
+
36
+ def self.get_checkout_url(params={})
37
+ params = {
38
+ :key => Dwolla::api_key,
39
+ :secret => Dwolla::api_secret,
40
+ :purchaseOrder => {
41
+ :destinationId => @destinationId,
42
+ :orderItems => @products,
43
+ :discount => @discount,
44
+ :shipping => @shipping,
45
+ :tax => @tax,
46
+ :total => @total
47
+ }
48
+ }
49
+
50
+ resp = Dwolla.request(:post, request_url, params, {}, false, false)
51
+
52
+ return checkout_url + resp['CheckoutId']
53
+ end
54
+
55
+ private
56
+
57
+ def self.request_url
58
+ return 'https://www.dwolla.com/payment/request'
59
+ end
60
+
61
+ def self.checkout_url
62
+ return 'https://www.dwolla.com/payment/checkout/'
63
+ end
64
+
65
+ def self.calculate_total
66
+ @total = 0
67
+
68
+ @products.each { |product|
69
+ @total += product['price'] * product['quantity']
70
+ }
71
+
72
+ return @total
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,56 @@
1
+ module Dwolla
2
+ class Requests
3
+ def self.get(id=nil, filters={})
4
+ url = requests_url
5
+
6
+ if id.is_a?(Hash)
7
+ filters = id
8
+ id = nil
9
+ else
10
+ filters = {}
11
+ end
12
+
13
+ url += id.to_s unless id.nil?
14
+
15
+ Dwolla.request(:get, url, filters)
16
+ end
17
+
18
+ def self.delete(id=nil)
19
+ raise MissingParameterError.new('No Request ID Provided.') if id.nil?
20
+
21
+ url = requests_url + id.to_s + '/cancel'
22
+
23
+ Dwolla.request(:post, url)
24
+ end
25
+
26
+ def self.create(params={})
27
+ raise MissingParameterError.new('No Source ID Provided.') unless params[:sourceId]
28
+ raise MissingParameterError.new('No Amount Provided.') unless params[:amount]
29
+
30
+ url = requests_url
31
+
32
+ Dwolla.request(:post, url, params)
33
+ end
34
+
35
+ def self.fulfill(id=nil, params={})
36
+ raise MissingParameterError.new('No Request ID Provided.') if id.nil?
37
+ raise MissingParameterError.new('No PIN Provided.') unless params[:pin]
38
+
39
+ url = requests_url + id.to_s + '/fulfill'
40
+
41
+ Dwolla.request(:post, url, params)
42
+ end
43
+
44
+ class << self
45
+ alias_method :pending, :get
46
+ alias_method :cancel, :delete
47
+ alias_method :request, :create
48
+ end
49
+
50
+ private
51
+
52
+ def self.requests_url
53
+ return '/requests/'
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,60 @@
1
+ module Dwolla
2
+ class Transactions
3
+ def self.get(id=nil, filters={})
4
+ url = transactions_url
5
+
6
+ if id.is_a?(Hash)
7
+ filters = id
8
+ id = nil
9
+ else
10
+ filters = {}
11
+ end
12
+
13
+ url += id.to_s unless id.nil?
14
+
15
+ Dwolla.request(:get, url, filters)
16
+ end
17
+
18
+ def self.stats(filters={})
19
+ url = transactions_url + 'stats'
20
+
21
+ Dwolla.request(:get, url, filters)
22
+ end
23
+
24
+ def self.create(params={})
25
+ raise MissingParameterError.new('No PIN Provided.') unless params[:pin]
26
+ raise MissingParameterError.new('No Destination ID Provided.') unless params[:destinationId]
27
+ raise MissingParameterError.new('No Amount Provided.') unless params[:amount]
28
+
29
+ url = transactions_url + 'send'
30
+
31
+ Dwolla.request(:post, url, params)
32
+ end
33
+
34
+ def self.guest_send(params={})
35
+ raise MissingParameterError.new('No Destination ID Provided.') unless params[:destinationId]
36
+ raise MissingParameterError.new('No Amount Provided.') unless params[:amount]
37
+ raise MissingParameterError.new('No First Name Provided.') unless params[:firstName]
38
+ raise MissingParameterError.new('No Last Name Provided.') unless params[:lastName]
39
+ raise MissingParameterError.new('No Email Address Provided.') unless params[:emailAddress]
40
+ raise MissingParameterError.new('No Routing Number (ABA) Provided.') unless params[:routingNumber]
41
+ raise MissingParameterError.new('No Account Number Provided.') unless params[:accountNumber]
42
+ raise MissingParameterError.new('No Account Type Provided.') unless params[:accountType]
43
+
44
+ url = transactions_url + 'guestsend'
45
+
46
+ Dwolla.request(:post, url, params, {}, false)
47
+ end
48
+
49
+ class << self
50
+ alias_method :listing, :get
51
+ alias_method :send, :create
52
+ end
53
+
54
+ private
55
+
56
+ def self.transactions_url
57
+ return '/transactions/'
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,36 @@
1
+ module Dwolla
2
+ class Users
3
+ def self.get(id=nil)
4
+ url = users_url
5
+
6
+ unless id.nil?
7
+ url += id.to_s
8
+ @oauth = false
9
+ else
10
+ @oauth = true
11
+ end
12
+
13
+ Dwolla.request(:get, url, {}, {}, @oauth)
14
+ end
15
+
16
+ def self.me
17
+ # I'm not using the 'alias_method' fn
18
+ # because the .me method should not
19
+ # honor any parameters (i.e. User IDs)
20
+ # passed to it
21
+ self.get
22
+ end
23
+
24
+ def self.nearby(filters={})
25
+ url = users_url + 'nearby'
26
+
27
+ Dwolla.request(:get, url, filters, {}, false)
28
+ end
29
+
30
+ private
31
+
32
+ def self.users_url
33
+ return '/users/'
34
+ end
35
+ end
36
+ end