balanced 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/CONTRIBUTORS +4 -0
- data/Gemfile +3 -0
- data/Guardfile +7 -0
- data/README.md +31 -2
- data/balanced.gemspec +4 -1
- data/examples/examples.rb +18 -2
- data/lib/balanced.rb +16 -25
- data/lib/balanced/client.rb +16 -39
- data/lib/balanced/resources.rb +13 -241
- data/lib/balanced/resources/account.rb +134 -0
- data/lib/balanced/resources/api_key.rb +18 -0
- data/lib/balanced/resources/bank_account.rb +34 -0
- data/lib/balanced/resources/card.rb +34 -0
- data/lib/balanced/resources/credit.rb +20 -0
- data/lib/balanced/resources/debit.rb +42 -0
- data/lib/balanced/resources/hold.rb +42 -0
- data/lib/balanced/resources/marketplace.rb +77 -0
- data/lib/balanced/resources/merchant.rb +25 -0
- data/lib/balanced/resources/refund.rb +21 -0
- data/lib/balanced/resources/resource.rb +130 -0
- data/lib/balanced/resources/transaction.rb +18 -0
- data/lib/balanced/version.rb +1 -1
- data/spec/balanced/resources/account_spec.rb +422 -0
- data/spec/balanced/resources/api_key_spec.rb +55 -0
- data/spec/balanced/resources/marketplace_spec.rb +97 -0
- data/spec/balanced_spec.rb +73 -12
- data/spec/client_spec.rb +2 -2
- data/spec/spec_helper.rb +24 -0
- data/spec/utils_spec.rb +2 -2
- metadata +85 -7
- data/lib/balanced/base.rb +0 -129
- data/spec/api_key_spec.rb +0 -20
data/.gitignore
CHANGED
data/.rspec
ADDED
data/CONTRIBUTORS
ADDED
data/Gemfile
CHANGED
data/Guardfile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
ignore %r{^doc}
|
2
|
+
guard 'rspec', :version => 2 do
|
3
|
+
watch(%r{^spec/.+_spec\.rb$})
|
4
|
+
watch(%r{^lib/balanced/(.+)\.rb$}) { |m| "spec/balanced/#{m[1]}_spec.rb" }
|
5
|
+
watch(%r{^lib/balanced/resources/(.+)\.rb$}) { |m| "spec/balanced/resources/#{m[1]}_spec.rb" }
|
6
|
+
watch('spec/spec_helper.rb') { "spec" }
|
7
|
+
end
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Balanced
|
2
2
|
|
3
|
-
|
3
|
+
Online Marketplace Payments
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,8 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
See https://www.balancedpayments.com/docs/ruby for tutorials and documentation.
|
22
|
+
|
22
23
|
|
23
24
|
## Contributing
|
24
25
|
|
@@ -27,3 +28,31 @@ TODO: Write usage instructions here
|
|
27
28
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
29
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
30
|
5. Create new Pull Request
|
31
|
+
|
32
|
+
|
33
|
+
### Specs
|
34
|
+
|
35
|
+
The spec suite is a work in progress. Existing specs can either be run
|
36
|
+
using [guard](https://github.com/guard/guard) or rake.
|
37
|
+
|
38
|
+
Guard
|
39
|
+
|
40
|
+
$ bundle exec guard
|
41
|
+
|
42
|
+
Rake
|
43
|
+
|
44
|
+
$ rake spec
|
45
|
+
|
46
|
+
We use [VCR](https://www.relishapp.com/myronmarston/vcr/docs) to stub
|
47
|
+
out and save http interactions. Cassettes are not stored in the repo.
|
48
|
+
They are generated the first time the spec suite is run and stored in
|
49
|
+
spec/cassettes. To clear them and regenerate:
|
50
|
+
|
51
|
+
$ rm -rf spec/cassettes
|
52
|
+
|
53
|
+
|
54
|
+
### Building Documentation
|
55
|
+
|
56
|
+
Documentation is build using YARD - http://rubydoc.info/docs/yard
|
57
|
+
|
58
|
+
yard doc -e doc/balanced_plugin.rb -p doc/balanced_templates/ 'lib/**/resources.rb' --one-file
|
data/balanced.gemspec
CHANGED
@@ -14,7 +14,10 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.add_dependency("faraday", '~> 0.8')
|
15
15
|
gem.add_dependency("faraday_middleware", '~> 0.8.7')
|
16
16
|
|
17
|
-
gem.add_development_dependency("rspec", '~> 2.
|
17
|
+
gem.add_development_dependency("rspec", '~> 2.10')
|
18
|
+
gem.add_development_dependency("vcr", '~> 2.2.0')
|
19
|
+
gem.add_development_dependency("guard", '~> 1.1.1')
|
20
|
+
gem.add_development_dependency("guard-rspec", '~> 1.0.0')
|
18
21
|
|
19
22
|
gem.files = `git ls-files`.split($\)
|
20
23
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/examples/examples.rb
CHANGED
@@ -6,6 +6,16 @@ rescue NameError
|
|
6
6
|
raise "wtf"
|
7
7
|
end
|
8
8
|
|
9
|
+
host = ENV['BALANCED_HOST'] or nil
|
10
|
+
options = {}
|
11
|
+
if host then
|
12
|
+
options[:scheme] = 'http'
|
13
|
+
options[:host] = host
|
14
|
+
options[:port] = 5000
|
15
|
+
end
|
16
|
+
|
17
|
+
Balanced.configure(nil, options)
|
18
|
+
|
9
19
|
puts "create our new api key"
|
10
20
|
api_key = Balanced::ApiKey.new.save
|
11
21
|
puts "Our secret is: ", api_key.secret
|
@@ -99,6 +109,12 @@ puts "ok lets invalid a card"
|
|
99
109
|
card['is_valid'] = false
|
100
110
|
card.save
|
101
111
|
|
102
|
-
raise "This card is
|
112
|
+
raise "This card is INCORRECTLY VALID" if card.is_valid
|
113
|
+
|
114
|
+
puts "invalidating a bank account"
|
115
|
+
bank_account['is_valid'] = false
|
116
|
+
bank_account.save
|
117
|
+
|
118
|
+
raise "This card is INCORRECTLY VALID" if bank_account.is_valid
|
103
119
|
|
104
|
-
puts "and there you have it :)"
|
120
|
+
puts "and there you have it :)"
|
data/lib/balanced.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "balanced", "resources")
|
2
|
+
|
1
3
|
require 'uri'
|
2
4
|
require 'balanced/version' unless defined? Balanced::VERSION
|
3
5
|
require 'balanced/client'
|
4
6
|
require 'balanced/utils'
|
5
|
-
require 'balanced/base'
|
6
7
|
require 'balanced/resources'
|
7
8
|
|
8
9
|
module Balanced
|
@@ -17,34 +18,15 @@ module Balanced
|
|
17
18
|
|
18
19
|
class << self
|
19
20
|
|
20
|
-
attr_accessor
|
21
|
-
attr_accessor
|
21
|
+
attr_accessor :client
|
22
|
+
attr_accessor :config
|
22
23
|
|
23
24
|
def configure(api_key=nil, options={})
|
24
|
-
|
25
|
-
@config = options
|
26
|
-
@client = Balanced::Client.new(api_key, @config)
|
27
|
-
end
|
28
|
-
|
29
|
-
def get uri, params = {}
|
30
|
-
self.client.get uri, params
|
31
|
-
end
|
32
|
-
|
33
|
-
def post uri, data = {}
|
34
|
-
self.client.post uri, data
|
35
|
-
end
|
36
|
-
|
37
|
-
def put uri, data = {}
|
38
|
-
self.client.put uri, data
|
39
|
-
end
|
40
|
-
|
41
|
-
def delete uri
|
42
|
-
self.client.delete uri
|
25
|
+
@client = Balanced::Client.new(api_key, @config.merge(options))
|
43
26
|
end
|
44
27
|
|
45
28
|
def split_the_uri uri
|
46
|
-
|
47
|
-
parsed_uri.path.sub(/\/$/, '').split('/')
|
29
|
+
URI.parse(uri).path.sub(/\/$/, '').split('/')
|
48
30
|
end
|
49
31
|
|
50
32
|
def from_uri uri
|
@@ -72,9 +54,18 @@ module Balanced
|
|
72
54
|
end
|
73
55
|
true
|
74
56
|
end
|
57
|
+
|
58
|
+
def method_missing(method, *args, &block)
|
59
|
+
case method
|
60
|
+
when :get, :post, :put, :delete
|
61
|
+
self.client.send method, *args
|
62
|
+
else
|
63
|
+
super
|
64
|
+
end
|
65
|
+
end
|
75
66
|
end
|
76
67
|
|
77
68
|
# configure on import so we don't have to configure for creating
|
78
69
|
# an api key
|
79
70
|
configure
|
80
|
-
end
|
71
|
+
end
|
data/lib/balanced/client.rb
CHANGED
@@ -14,17 +14,15 @@ module Balanced
|
|
14
14
|
:logging_level => 'WARN',
|
15
15
|
}
|
16
16
|
|
17
|
-
attr :api_key, true
|
18
17
|
attr_reader :conn
|
19
|
-
attr_accessor :config
|
18
|
+
attr_accessor :api_key, :config
|
20
19
|
|
21
20
|
def initialize(api_key, options={})
|
22
21
|
@api_key = api_key.nil? ? api_key : api_key.strip
|
23
|
-
@config = DEFAULTS.merge
|
22
|
+
@config = DEFAULTS.merge options
|
24
23
|
build_conn
|
25
24
|
end
|
26
25
|
|
27
|
-
|
28
26
|
def build_conn
|
29
27
|
logger = Logger.new(STDOUT)
|
30
28
|
logger.level = Logger.const_get(DEFAULTS[:logging_level].to_s)
|
@@ -37,50 +35,29 @@ module Balanced
|
|
37
35
|
cxn.response :raise_error # raise exceptions on 40x, 50x responses
|
38
36
|
cxn.adapter Faraday.default_adapter
|
39
37
|
end
|
40
|
-
|
41
|
-
|
38
|
+
conn.path_prefix = '/'
|
39
|
+
conn.headers['User-Agent'] = "balanced-ruby/#{Balanced::VERSION}"
|
42
40
|
end
|
43
41
|
|
44
|
-
def inspect # :nodoc:
|
45
|
-
"<Balanced::Client @api_key
|
46
|
-
end
|
42
|
+
#def inspect # :nodoc:
|
43
|
+
#"<Balanced::Client @api_key=#{api_key}, @url=#{url}>"
|
44
|
+
#end
|
47
45
|
|
48
46
|
def url
|
49
|
-
|
50
|
-
:host => @config[:host],
|
51
|
-
:port => @config[:port],
|
52
|
-
)
|
53
|
-
# wow. yes, this is what you actually have to do.
|
54
|
-
_url.scheme = @config[:scheme]
|
55
|
-
_url
|
56
|
-
end
|
57
|
-
|
58
|
-
# wtf..
|
59
|
-
def get *args
|
60
|
-
op(:get, *args)
|
61
|
-
end
|
62
|
-
|
63
|
-
def post *args
|
64
|
-
op(:post, *args)
|
65
|
-
end
|
66
|
-
|
67
|
-
def put *args
|
68
|
-
op(:put, *args)
|
69
|
-
end
|
70
|
-
|
71
|
-
def delete *args
|
72
|
-
op(:delete, *args)
|
47
|
+
URI::HTTPS.build :host => config[:host], :port => config[:port]
|
73
48
|
end
|
74
49
|
|
75
50
|
private
|
76
51
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
52
|
+
def method_missing(method, *args, &block)
|
53
|
+
case method
|
54
|
+
when :get, :post, :put, :delete
|
55
|
+
conn.basic_auth(api_key, '') unless api_key.nil?
|
56
|
+
conn.send method, *args
|
57
|
+
else
|
58
|
+
super
|
80
59
|
end
|
81
|
-
@conn.send(method, *args)
|
82
60
|
end
|
83
61
|
|
84
62
|
end
|
85
|
-
|
86
|
-
end
|
63
|
+
end
|
data/lib/balanced/resources.rb
CHANGED
@@ -1,241 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
super attributes
|
15
|
-
end
|
16
|
-
|
17
|
-
def debit (amount=nil,
|
18
|
-
appears_on_statement_as=nil,
|
19
|
-
hold_uri=nil,
|
20
|
-
meta={},
|
21
|
-
description=nil,
|
22
|
-
source_uri=nil)
|
23
|
-
debit = Debit.new(
|
24
|
-
:uri => self.debits_uri,
|
25
|
-
:amount => amount,
|
26
|
-
:appears_on_statement_as => appears_on_statement_as,
|
27
|
-
:hold_uri => hold_uri,
|
28
|
-
:meta => meta,
|
29
|
-
:description => description,
|
30
|
-
:source_uri => source_uri,
|
31
|
-
)
|
32
|
-
debit.save
|
33
|
-
end
|
34
|
-
|
35
|
-
def hold amount, meta={}, source_uri=nil
|
36
|
-
hold = Hold.new(
|
37
|
-
:uri => self.holds_uri,
|
38
|
-
:amount => amount,
|
39
|
-
:meta => meta,
|
40
|
-
:source_uri => source_uri,
|
41
|
-
)
|
42
|
-
hold.save
|
43
|
-
end
|
44
|
-
|
45
|
-
def credit amount, description=nil, meta={}, destination_uri=nil
|
46
|
-
credit = Credit.new(
|
47
|
-
:uri => self.credits_uri,
|
48
|
-
:amount => amount,
|
49
|
-
:meta => meta,
|
50
|
-
:description => description,
|
51
|
-
:destination_uri => destination_uri,
|
52
|
-
)
|
53
|
-
credit.save
|
54
|
-
end
|
55
|
-
|
56
|
-
def add_card card_uri
|
57
|
-
self.card_uri = card_uri
|
58
|
-
save
|
59
|
-
end
|
60
|
-
|
61
|
-
def add_bank_account bank_account_uri
|
62
|
-
self.bank_account_uri = bank_account_uri
|
63
|
-
save
|
64
|
-
end
|
65
|
-
|
66
|
-
def promote_to_merchant merchant_data
|
67
|
-
self.merchant = merchant
|
68
|
-
save
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
class Merchant < Resource
|
74
|
-
|
75
|
-
def self.me
|
76
|
-
# TODO: use query
|
77
|
-
response = Balanced.get collection_path
|
78
|
-
return nil if response.body.to_s.length.zero? or response.body['total'] == 0
|
79
|
-
payload = response.body
|
80
|
-
construct_from_response payload['items'][0]
|
81
|
-
end
|
82
|
-
|
83
|
-
def me
|
84
|
-
self.class.me
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
class Marketplace < Resource
|
90
|
-
|
91
|
-
def self.my_marketplace
|
92
|
-
# TODO: use query
|
93
|
-
response = Balanced.get collection_path
|
94
|
-
return nil if response.body.to_s.length.zero? or response.body['total'] == 0
|
95
|
-
payload = response.body
|
96
|
-
construct_from_response payload['items'][0]
|
97
|
-
end
|
98
|
-
|
99
|
-
def my_marketplace
|
100
|
-
self.class.my_marketplace
|
101
|
-
end
|
102
|
-
|
103
|
-
def create_buyer email_address, card_uri, name=nil, meta={}
|
104
|
-
account = Account.new(
|
105
|
-
:uri => self.accounts_uri,
|
106
|
-
:email_address => email_address,
|
107
|
-
:card_uri => card_uri,
|
108
|
-
:name => name,
|
109
|
-
:meta => meta,
|
110
|
-
)
|
111
|
-
account.save
|
112
|
-
end
|
113
|
-
|
114
|
-
def create_merchant email_address, merchant, bank_account_uri=nil, name=nil, meta={}
|
115
|
-
account = Account.new(
|
116
|
-
:uri => self.accounts_uri,
|
117
|
-
:email_address => email_address,
|
118
|
-
:merchant => merchant,
|
119
|
-
:bank_account_uri => bank_account_uri,
|
120
|
-
:name => name,
|
121
|
-
:meta => meta,
|
122
|
-
)
|
123
|
-
account.save
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
class Hold < Resource
|
129
|
-
|
130
|
-
def initialize attributes = {}
|
131
|
-
Balanced::Utils.stringify_keys! attributes
|
132
|
-
unless attributes.has_key? 'uri'
|
133
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
134
|
-
end
|
135
|
-
super attributes
|
136
|
-
end
|
137
|
-
|
138
|
-
def void
|
139
|
-
@is_void = true
|
140
|
-
save
|
141
|
-
end
|
142
|
-
|
143
|
-
def capture amount=nil, appears_on_statement_as=nil, meta={}, description=nil
|
144
|
-
amount ||= self.amount
|
145
|
-
self.account.debit(amount, appears_on_statement_as, self.uri, meta, description)
|
146
|
-
end
|
147
|
-
|
148
|
-
end
|
149
|
-
|
150
|
-
class Debit < Resource
|
151
|
-
|
152
|
-
def initialize attributes = {}
|
153
|
-
Balanced::Utils.stringify_keys! attributes
|
154
|
-
unless attributes.has_key? 'uri'
|
155
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
156
|
-
end
|
157
|
-
super attributes
|
158
|
-
end
|
159
|
-
|
160
|
-
def refund amount=nil, description=nil
|
161
|
-
refund = Refund.new(
|
162
|
-
:uri => self.refunds_uri,
|
163
|
-
:debit_uri => self.uri,
|
164
|
-
:amount => amount,
|
165
|
-
:description => description,
|
166
|
-
)
|
167
|
-
refund.save
|
168
|
-
end
|
169
|
-
|
170
|
-
end
|
171
|
-
|
172
|
-
class Credit < Resource
|
173
|
-
def initialize attributes = {}
|
174
|
-
Balanced::Utils.stringify_keys! attributes
|
175
|
-
unless attributes.has_key? 'uri'
|
176
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
177
|
-
end
|
178
|
-
super attributes
|
179
|
-
end
|
180
|
-
|
181
|
-
end
|
182
|
-
|
183
|
-
class Refund < Resource
|
184
|
-
def initialize attributes = {}
|
185
|
-
Balanced::Utils.stringify_keys! attributes
|
186
|
-
unless attributes.has_key? 'uri'
|
187
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
188
|
-
end
|
189
|
-
super attributes
|
190
|
-
end
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
class Transaction < Resource
|
195
|
-
def initialize attributes = {}
|
196
|
-
Balanced::Utils.stringify_keys! attributes
|
197
|
-
unless attributes.has_key? 'uri'
|
198
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
199
|
-
end
|
200
|
-
super attributes
|
201
|
-
end
|
202
|
-
|
203
|
-
end
|
204
|
-
|
205
|
-
class Card < Resource
|
206
|
-
def initialize attributes = {}
|
207
|
-
Balanced::Utils.stringify_keys! attributes
|
208
|
-
unless attributes.has_key? 'uri'
|
209
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
210
|
-
end
|
211
|
-
super attributes
|
212
|
-
end
|
213
|
-
|
214
|
-
def debit amount=nil, appears_on_statement_as=nil, holds_uri=nil, meta={}, description=nil
|
215
|
-
self.account.debit(amount, appears_on_statement_as, holds_uri, meta, description, self.uri)
|
216
|
-
end
|
217
|
-
|
218
|
-
def hold amount=nil, meta=nil
|
219
|
-
self.account.hold(amount, meta, self.uri)
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
class BankAccount < Resource
|
224
|
-
def initialize attributes = {}
|
225
|
-
Balanced::Utils.stringify_keys! attributes
|
226
|
-
unless attributes.has_key? 'uri'
|
227
|
-
attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
|
228
|
-
end
|
229
|
-
super attributes
|
230
|
-
end
|
231
|
-
|
232
|
-
def debit amount, appears_on_statement_as=nil, meta={}, description=nil
|
233
|
-
self.account.debit(amount, appears_on_statement_as, meta, description, self.uri)
|
234
|
-
end
|
235
|
-
|
236
|
-
def credit amount, description=nil, meta={}
|
237
|
-
self.account.credit(amount, description, meta, self.uri)
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
end
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "balanced", "resources")
|
2
|
+
require "resource"
|
3
|
+
require "account"
|
4
|
+
require "api_key"
|
5
|
+
require "bank_account"
|
6
|
+
require "card"
|
7
|
+
require "credit"
|
8
|
+
require "debit"
|
9
|
+
require "hold"
|
10
|
+
require "marketplace"
|
11
|
+
require "merchant"
|
12
|
+
require "refund"
|
13
|
+
require "transaction"
|