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
@@ -1,27 +0,0 @@
1
- module Dwolla
2
- module Response
3
- class ParseJson < Faraday::Response::Middleware
4
- def on_complete(env)
5
- if respond_to? :parse
6
- env[:body] = parse(env[:body]) unless [204,302,304,307].index env[:status]
7
- end
8
- end
9
-
10
- def parse(body)
11
- case body
12
- when ''
13
- nil
14
- else
15
- response_hash = ::MultiJson.load(body)
16
-
17
- raise Dwolla::RequestException, response_hash["Message"] if response_hash["Success"] == false
18
-
19
- response_hash["Response"] ||
20
- response_hash["SendResult"] ||
21
- response_hash["RequestResult"] ||
22
- response_hash
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,56 +0,0 @@
1
- module Dwolla
2
- class Transaction
3
- include Dwolla::Connection
4
- @test_mode = false
5
- def self.test_mode
6
- @test_mode
7
- end
8
- def self.test_mode=(m)
9
- @test_mode = m
10
- end
11
-
12
- ENDPOINTS = { :send => 'transactions/send',
13
- :request => 'transactions/request' }
14
- TEST_ENDPOINTS = { :send => 'testapi/send',
15
- :request => 'testapi/request' }
16
-
17
- attr_accessor :origin, :destination, :destination_type, :type, :amount, :pin, :id, :source, :source_type, :description, :funds_source, :assume_costs
18
-
19
- def initialize(attrs = {})
20
- attrs.each do |key, value|
21
- send("#{key}=".to_sym, value)
22
- end
23
- end
24
-
25
- def execute
26
- if self.class.test_mode
27
- end_point = TEST_ENDPOINTS[type]
28
- else
29
- end_point = ENDPOINTS[type]
30
- end
31
- self.id = post(end_point, to_payload)
32
- end
33
-
34
- private
35
-
36
- def auth_params
37
- { :oauth_token => origin.oauth_token }
38
- end
39
-
40
- def to_payload
41
- payload = {
42
- :amount => amount,
43
- :pin => pin
44
- }
45
- payload[:destinationId] = destination if destination
46
- payload[:destinationType] = destination_type if destination_type
47
- payload[:sourceId] = source if source
48
- payload[:sourceType] = source_type if source_type
49
- payload[:notes] = description if description
50
- payload[:fundsSource] = funds_source if funds_source
51
- payload[:assumeCosts] = assume_costs if assume_costs
52
-
53
- payload
54
- end
55
- end
56
- end
data/lib/dwolla/user.rb DELETED
@@ -1,121 +0,0 @@
1
- module Dwolla
2
- class User
3
- include Dwolla::Connection
4
-
5
- attr_accessor :id,
6
- :name,
7
- :latitude,
8
- :longitude,
9
- :city,
10
- :state,
11
- :type,
12
- :contact_type,
13
- :image,
14
- :oauth_token
15
-
16
- def initialize(attrs={})
17
- update_attributes(attrs)
18
- end
19
-
20
- def self.me(access_token)
21
- User.new(:oauth_token => access_token)
22
- end
23
-
24
- def fetch
25
- user_attributes = get('users')
26
- update_attributes(user_attributes)
27
- self
28
- end
29
-
30
- def update_attributes(attrs)
31
- attrs.each do |key, value|
32
- key_string = key.is_a?(String) ? key : key.to_s
33
- send("#{key_string.downcase}=".to_sym, value)
34
- end
35
- end
36
-
37
- def balance
38
- get('balance')
39
- end
40
-
41
- def funding_sources
42
- sources = get('fundingsources')
43
- sources.map{|s| FundingSource.from_json(s)}
44
- end
45
-
46
- def funding_source(funding_id)
47
- sources = get('fundingsources?fundingid=' + funding_id)
48
- sources.map{|s| FundingSource.from_json(s)}
49
- end
50
-
51
- def add_funding_source(funding_source_hash)
52
- params = auth_params.merge(funding_source_hash)
53
-
54
- returned_source_hash = post("fundingsources/", params)
55
- FundingSource.from_json(returned_source_hash)
56
- end
57
-
58
- def deposit_funds(funding_id, pin, amount)
59
- params = auth_params.merge(:pin => pin, :amount => amount, :funding_id => funding_id)
60
-
61
- returned_hash = post("fundingsources/#{funding_id}/deposit", params)
62
- returned_hash
63
- end
64
-
65
- def withdraw_funds(funding_id, pin, amount)
66
- params = auth_params.merge(:pin => pin, :amount => amount, :funding_id => funding_id)
67
-
68
- returned_hash = post("fundingsources/#{funding_id}/withdraw", params)
69
- returned_hash
70
- end
71
-
72
-
73
- def contacts(options = {})
74
- contacts_url = 'contacts'
75
- contacts = get(contacts_url, options)
76
-
77
- instances_from_contacts(contacts)
78
- end
79
-
80
- def send_money_to(destination, amount, pin, type='dwolla', description='', funds_source=nil, assume_costs=nil)
81
- transaction = Transaction.new(:origin => self,
82
- :destination => destination,
83
- :destination_type => type,
84
- :description => description,
85
- :type => :send,
86
- :amount => amount,
87
- :pin => pin,
88
- :funds_source => funds_source,
89
- :assume_costs => assume_costs)
90
-
91
- transaction.execute
92
- end
93
-
94
- def request_money_from(source, amount, pin, source_type='dwolla', description='')
95
- transaction = Transaction.new(:origin => self,
96
- :source => source,
97
- :source_type => source_type,
98
- :description => description,
99
- :type => :request,
100
- :amount => amount,
101
- :pin => pin)
102
- transaction.execute
103
- end
104
-
105
- private
106
-
107
- def instances_from_contacts(contacts)
108
- user_instances = []
109
- contacts.each do |contact|
110
- contact["Contact_Type"] = contact["Type"]
111
- contact.delete("Type")
112
- user_instances << User.new(contact)
113
- end
114
- user_instances
115
- end
116
-
117
- def auth_params
118
- { :oauth_token => self.oauth_token }
119
- end
120
- end
121
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Dwolla::Client do
4
- subject { Dwolla::Client.new('sample_client_id', 'sample_client_secret') }
5
- let(:query_params) { "client_id=sample_client_id&client_secret=sample_client_secret" }
6
-
7
- describe "getting user basic information" do
8
- before do
9
- stub_get('/users/812-111-1111', query_params).
10
- to_return(:body => fixture("basic_information.json"))
11
- end
12
-
13
- it 'should request the correct resource' do
14
- subject.user('812-111-1111')
15
- a_get('/users/812-111-1111', query_params).should have_been_made
16
- end
17
-
18
- it 'should return extended information of a given user' do
19
- user = subject.user('812-111-1111')
20
- user.should be_a Dwolla::User
21
- user.id.should == '812-111-1111'
22
- user.name.should == 'Test User'
23
- user.latitude.should == 41.584546
24
- user.longitude.should == -93.634167
25
- end
26
- end
27
-
28
- describe "registering a user" do
29
- it 'should register a user and return a user object' do
30
- stub_post('/register/').
31
- to_return(:body => fixture("register.json"))
32
-
33
- userhash = {"email" => "test@test.com",
34
- "password" => "Asdfasdf43434",
35
- "pin" => "4343",
36
- "firstName" => "Test",
37
- "lastName" => "User",
38
- "address" => "204 W. Test St.",
39
- "city" => "Lansing",
40
- "state" => "MI",
41
- "zip" => "48906",
42
- "phone" => "5175557621",
43
- "dateOfBirth" => "07-12-1983",
44
- "type" => "Personal",
45
- "acceptTerms" => "true"}
46
-
47
- user = subject.register_user(userhash)
48
- user.name.should eq 'Test User'
49
- end
50
- end
51
- end
@@ -1,59 +0,0 @@
1
- describe "Funding sources" do
2
- let(:oauth_token) { 'valid_token' }
3
-
4
- describe "Getting a list of funding sources" do
5
- before :each do
6
- user = Dwolla::User.me(oauth_token)
7
- stub_request(:get, "https://www.dwolla.com/oauth/rest/fundingsources?oauth_token=valid_token").
8
- with(:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Dwolla Ruby Wrapper'}).
9
- to_return(:body => fixture("sources.json"))
10
- @account = user.funding_sources.first
11
- end
12
- it "should be a FundingSource object" do
13
- @account.should be_kind_of(Dwolla::FundingSource)
14
- end
15
- it "should get the correct id" do
16
- @account.id.should == "mE06khgHy9K/Ii9n5fbUEg=="
17
- end
18
- it "should get the right name" do
19
- @account.name.should == "Checking - My Bank"
20
- end
21
- it "should get the right type" do
22
- @account.type.should == "Checking"
23
- end
24
- it "should get whether the account is verified" do
25
- @account.should be_verified
26
- end
27
- end
28
-
29
- describe "adding a funding source" do
30
- it "should create a funding source" do
31
- user = Dwolla::User.new(:oauth_token => '12345', :id => '1')
32
-
33
- stub_post('/fundingsources/').
34
- to_return(:body => fixture("add_funding_source.json"))
35
-
36
- sourcehash = {
37
- "account_number" => "4434343434",
38
- "routing_number" => "343434343434",
39
- "account_type" => "checking",
40
- "name" => "My Checking Account - Checking"
41
- }
42
-
43
- funding_source = user.add_funding_source(sourcehash)
44
- funding_source.name.should eq 'My Checking Account - Checking'
45
- end
46
- end
47
-
48
- # "oauth_token": "",
49
- # "funding_id": "",
50
- # "pin": "",
51
- # "amount": ""
52
-
53
- pending "Depositing funds to dwolla from funding source" do
54
- end
55
-
56
- pending "Withdrawing funds from dwolla to funding source" do
57
- end
58
-
59
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
- require 'forwardable'
3
-
4
- describe Dwolla::Response::FollowRedirects do
5
-
6
- before do
7
- @conn = Faraday.new do |b|
8
- b.use Dwolla::Response::FollowRedirects
9
- b.adapter :test do |stub|
10
- stub.get('/') { [301, {'Location' => '/found'}, ''] }
11
- stub.post('/create') { [302, {'Location' => '/'}, ''] }
12
- stub.get('/found') { [200, {'Content-Type' => 'text/plain'}, 'fin'] }
13
- stub.get('/loop') { [302, {'Location' => '/loop'}, ''] }
14
- stub.get('/temp') { [307, {'Location' => '/found'}, ''] }
15
- end
16
- end
17
- end
18
-
19
- extend Forwardable
20
- def_delegators :@conn, :get, :post
21
-
22
- it 'follow one redirect' do
23
- get('/').body.should == 'fin'
24
- end
25
-
26
- it 'follow twice redirect' do
27
- post('/create').body.should == 'fin'
28
- end
29
-
30
- it 'follows 307 redirects' do
31
- get('/temp').body.should == 'fin'
32
- end
33
-
34
- it 'has a redirect limit' do
35
- expect { get('/loop') }.to raise_error(Dwolla::Response::RedirectLimitReached)
36
- end
37
- end
38
-
@@ -1,212 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Dwolla::Transaction do
4
- describe "send transaction" do
5
- context "to a dwolla account" do
6
- before do
7
- @origin = double(:oauth_token => '1')
8
- @destination = '2'
9
- @destination_type = "dwolla"
10
- @payload = { :amount => 200,
11
- :pin => '1234',
12
- :destinationId => '2',
13
- :destinationType => 'dwolla',
14
- :notes => "Sending a transaction",
15
- :oauth_token => '1' }
16
-
17
- stub_post('/transactions/send').with(:body => MultiJson.dump(@payload)).to_return(
18
- :body => fixture('send_transaction.json'))
19
- end
20
- it "should request the correct resource" do
21
- transaction = Dwolla::Transaction.new(:origin => @origin,
22
- :destination => @destination,
23
- :destination_type => @destination_type,
24
- :description => "Sending a transaction",
25
- :type => :send,
26
- :amount => 200,
27
- :pin => '1234')
28
- transaction.execute
29
-
30
- a_post('/transactions/send').
31
- with(:body => MultiJson.dump(@payload)).should have_been_made
32
- end
33
-
34
- it "should fetch the id if transaction succesfull" do
35
- transaction = Dwolla::Transaction.new(:origin => @origin,
36
- :destination => @destination,
37
- :destination_type => @destination_type,
38
- :description => "Sending a transaction",
39
- :type => :send,
40
- :amount => 200,
41
- :pin => '1234')
42
-
43
- transaction.execute.should == 12345
44
- transaction.id.should == 12345
45
- end
46
- end
47
-
48
- context "to an email address" do
49
- before do
50
- @origin = double(:oauth_token => '1')
51
- @destination = "user@example.com"
52
- @destination_type = "email"
53
- @payload = { :amount => 200,
54
- :pin => '1234',
55
- :destinationId => 'user@example.com',
56
- :destinationType => 'email',
57
- :notes => "Sending a transaction",
58
- :oauth_token => '1' }
59
- stub_post('/transactions/send').with(:body => MultiJson.dump(@payload)).to_return(
60
- :body => fixture('send_transaction.json'))
61
- end
62
- it "should request the correct resource" do
63
- transaction = Dwolla::Transaction.new(:origin => @origin,
64
- :destination => @destination,
65
- :destination_type => @destination_type,
66
- :description => "Sending a transaction",
67
- :type => :send,
68
- :amount => 200,
69
- :pin => '1234')
70
-
71
- transaction.execute
72
-
73
- a_post('/transactions/send').
74
- with(:body => MultiJson.dump(@payload)).should have_been_made
75
- end
76
-
77
- it "should fetch the id if transaction succesfull" do
78
- transaction = Dwolla::Transaction.new(:origin => @origin,
79
- :destination => @destination,
80
- :destination_type => @destination_type,
81
- :description => "Sending a transaction",
82
- :type => :send,
83
- :amount => 200,
84
- :pin => '1234')
85
-
86
- transaction.execute.should == 12345
87
- transaction.id.should == 12345
88
- end
89
- end
90
-
91
- context "and assume costs" do
92
- before do
93
- @origin = double(:oauth_token => '1')
94
- @destination = "user@example.com"
95
- @destination_type = "email"
96
- @payload = { :amount => 200,
97
- :pin => '1234',
98
- :destinationId => 'user@example.com',
99
- :destinationType => 'email',
100
- :assumeCosts => true,
101
- :oauth_token => '1' }
102
- stub_post('/transactions/send').with(:body => MultiJson.dump(@payload)).to_return(
103
- :body => fixture('send_transaction.json'))
104
- end
105
-
106
- it "should request the correct resource" do
107
- transaction = Dwolla::Transaction.new(:origin => @origin,
108
- :destination => @destination,
109
- :destination_type => @destination_type,
110
- :type => :send,
111
- :amount => 200,
112
- :pin => '1234',
113
- :assume_costs => true )
114
-
115
- transaction.execute
116
-
117
- a_post('/transactions/send').
118
- with(:body => MultiJson.dump(@payload)).should have_been_made
119
- end
120
- end
121
-
122
- end
123
-
124
- describe "request transaction" do
125
- context "from a dwolla account" do
126
- before do
127
- @origin = double(:oauth_token => '1')
128
- @source = '2'
129
- @source_type = 'dwolla'
130
- @payload = { :amount => 200,
131
- :pin => '1234',
132
- :sourceId => '2',
133
- :sourceType => 'dwolla',
134
- :notes => "Sending a transaction",
135
- :oauth_token => '1' }
136
-
137
- stub_post('/transactions/request').with(:body => MultiJson.dump(@payload)).to_return(
138
- :body => fixture('request_transaction.json'))
139
- end
140
-
141
- it "should request the correct resource" do
142
- transaction = Dwolla::Transaction.new(:origin => @origin,
143
- :source => @source,
144
- :source_type => @source_type,
145
- :description => "Sending a transaction",
146
- :type => :request,
147
- :amount => 200,
148
- :pin => '1234')
149
- transaction.execute
150
-
151
- a_post('/transactions/request').
152
- with(:body => MultiJson.dump(@payload)).should have_been_made
153
- end
154
-
155
- it "should fetch the id if transaction succesfull" do
156
- transaction = Dwolla::Transaction.new(:origin => @origin,
157
- :source => @source,
158
- :source_type => @source_type,
159
- :description => "Sending a transaction",
160
- :type => :request,
161
- :amount => 200,
162
- :pin => '1234')
163
-
164
- transaction.execute.should == 12345
165
- transaction.id.should == 12345
166
- end
167
- end
168
- context "from an email address" do
169
- before do
170
- @origin = double(:oauth_token => '1')
171
- @source = 'user@example.com'
172
- @source_type = "email"
173
- @payload = { :amount => 200,
174
- :pin => '1234',
175
- :sourceId => 'user@example.com',
176
- :sourceType => 'email',
177
- :notes => "Sending a transaction",
178
- :oauth_token => '1' }
179
-
180
- stub_post('/transactions/request').with(:body => MultiJson.dump(@payload)).to_return(
181
- :body => fixture('request_transaction.json'))
182
- end
183
-
184
- it "should request the correct resource" do
185
- transaction = Dwolla::Transaction.new(:origin => @origin,
186
- :source => @source,
187
- :source_type => @source_type,
188
- :description => "Sending a transaction",
189
- :type => :request,
190
- :amount => 200,
191
- :pin => '1234')
192
- transaction.execute
193
-
194
- a_post('/transactions/request').
195
- with(:body => MultiJson.dump(@payload)).should have_been_made
196
- end
197
-
198
- it "should fetch the id if transaction succesfull" do
199
- transaction = Dwolla::Transaction.new(:origin => @origin,
200
- :source => @source,
201
- :source_type => @source_type,
202
- :description => "Sending a transaction",
203
- :type => :request,
204
- :amount => 200,
205
- :pin => '1234')
206
-
207
- transaction.execute.should == 12345
208
- transaction.id.should == 12345
209
- end
210
- end
211
- end
212
- end