balanced 0.2.5 → 0.3.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.
@@ -0,0 +1,130 @@
1
+ module Balanced
2
+ module Resource
3
+ attr_accessor :attributes
4
+
5
+ def initialize attributes = {}
6
+ @attributes = attributes
7
+ end
8
+
9
+ # delegate the query to the pager module
10
+
11
+ def find uri
12
+ self.class.find uri
13
+ end
14
+
15
+ def save
16
+ uri = @attributes.delete('uri')
17
+ method = :post
18
+ if uri.nil?
19
+ uri = self.class.collection_path
20
+ elsif !Balanced.is_collection(uri)
21
+ method = :put
22
+ end
23
+ response = Balanced.send(method, uri, self.attributes)
24
+ reload response
25
+ end
26
+
27
+ def destroy
28
+ Balanced.delete @attributes[:uri]
29
+ end
30
+
31
+ def reload response = nil
32
+ if response
33
+ return if response.body.to_s.length.zero?
34
+ fresh = self.class.construct_from_response response.body
35
+ else
36
+ fresh = self.find(@attributes[:uri])
37
+ end
38
+ fresh and copy_from fresh
39
+ self
40
+ end
41
+
42
+ def copy_from other
43
+ other.instance_variables.each do |ivar|
44
+ instance_variable_set ivar, other.instance_variable_get(ivar)
45
+ end
46
+ end
47
+
48
+ def method_missing(method, *args, &block)
49
+ case method
50
+ when /(.+)\=$/
51
+ attr = method.to_s.chop
52
+ @attributes[attr] = args[0]
53
+ else
54
+ super
55
+ end
56
+ end
57
+ def self.included(base)
58
+ base.extend ClassMethods
59
+ end
60
+
61
+ module ClassMethods
62
+ def resource_name
63
+ Utils.demodulize name
64
+ end
65
+
66
+ def collection_name
67
+ Utils.pluralize Utils.underscore(resource_name)
68
+ end
69
+
70
+ def collection_path
71
+ ["/v#{Balanced.config[:version]}", collection_name].compact.join '/'
72
+ end
73
+
74
+ def member_name
75
+ Utils.underscore resource_name
76
+ end
77
+
78
+ def construct_from_response payload
79
+ payload = Balanced::Utils.hash_with_indifferent_read_access payload
80
+ klass = Balanced.from_uri(payload[:uri])
81
+ instance = klass.new payload
82
+ payload.each do |name, value|
83
+ klass.class_eval {
84
+ attr_accessor name.to_s
85
+ }
86
+ # here is where our interpretations will begin.
87
+ # if the value is a sub-resource, lets instantiate the class
88
+ # and set it correctly
89
+ if value.instance_of? Hash and value.has_key? 'uri'
90
+ value = construct_from_response value
91
+ elsif name =~ /_uri$/
92
+ modified_name = name.sub(/_uri$/, '')
93
+ klass.instance_eval {
94
+ define_method(modified_name) {
95
+ values_class = Balanced.from_uri(value)
96
+ # if uri is a collection -> this would definitely be if it ends in a symbol
97
+ # then we should allow a lazy executor of the query pager
98
+ if Balanced.is_collection(value)
99
+ # TODO: return the pager
100
+ p "TODO: return the pager for this class: #{values_class}"
101
+ values_class.new
102
+ else
103
+ values_class.find(value)
104
+ end
105
+ }
106
+ }
107
+ end
108
+
109
+ instance.class.instance_eval {
110
+ define_method(name) { @attributes[name] } # Get.
111
+ define_method("#{name}=") { |value| @attributes[name] = value } # Set.
112
+ define_method("#{name}?") { !!@attributes[name].nil? } # Present.
113
+ }
114
+ instance.send("#{name}=".to_s, value)
115
+ end
116
+ instance
117
+ end
118
+
119
+ def find uri
120
+ response = Balanced.get uri
121
+ construct_from_response response.body
122
+ end
123
+
124
+ end
125
+
126
+
127
+
128
+ end
129
+ end
130
+
@@ -0,0 +1,18 @@
1
+ module Balanced
2
+ # Any transfer, or potential transfer of, funds from or to, your
3
+ # Marketplace. E.g. a Credit, Debit, Refund, or Hold.
4
+ #
5
+ class Transaction
6
+ include Balanced::Resource
7
+
8
+ def initialize attributes = {}
9
+ Balanced::Utils.stringify_keys! attributes
10
+ unless attributes.has_key? 'uri'
11
+ attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
12
+ end
13
+ super attributes
14
+ end
15
+
16
+ end
17
+ end
18
+
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.2.5'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -0,0 +1,422 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Account do
4
+ use_vcr_cassette
5
+ before do
6
+ api_key = Balanced::ApiKey.new.save
7
+ Balanced.configure api_key.secret
8
+ @marketplace = Balanced::Marketplace.new.save
9
+ end
10
+
11
+ describe "merchant" do
12
+ use_vcr_cassette
13
+
14
+ before do
15
+ @merchant_attributes = {
16
+ :type => "person",
17
+ :name => "Billy Jones",
18
+ :street_address => "801 High St.",
19
+ :postal_code => "94301",
20
+ :country => "USA",
21
+ :dob => "1842-01",
22
+ :phone_number => "+16505551234",
23
+ }
24
+ end
25
+
26
+ describe "new" do
27
+ use_vcr_cassette
28
+ before do
29
+ @bank_account = Balanced::BankAccount.new(
30
+ :account_number => "1234567890",
31
+ :bank_code => "321174851",
32
+ :name => "Jack Q Merchant"
33
+ ).save
34
+ end
35
+ it do
36
+ -> do
37
+ @merchant = Balanced::Account.new(
38
+ :uri => @marketplace.accounts_uri,
39
+ :email_address => "merchant@example.org",
40
+ :merchant => @merchant_attributes,
41
+ :bank_account_uri => @bank_account.uri,
42
+ :name => "Jack Q Merchant"
43
+ )
44
+ end.should_not raise_error
45
+ end
46
+ end
47
+
48
+ describe "#save" do
49
+
50
+ describe "when creating" do
51
+ use_vcr_cassette
52
+ before do
53
+ bank_account = Balanced::BankAccount.new(
54
+ :account_number => "1234567890",
55
+ :bank_code => "321174851",
56
+ :name => "Jack Q Merchant"
57
+ ).save
58
+ @merchant = Balanced::Account.new(
59
+ :uri => @marketplace.accounts_uri,
60
+ :email_address => "merchant@example.org",
61
+ :merchant => @merchant_attributes,
62
+ :bank_account_uri => bank_account.uri,
63
+ :name => "Jack Q Merchant"
64
+ )
65
+ end
66
+ it { -> { @merchant.save }.should_not raise_error }
67
+ end
68
+
69
+ describe "after #save" do
70
+ describe "attributes" do
71
+ use_vcr_cassette
72
+ before do
73
+ bank_account = Balanced::BankAccount.new(
74
+ :account_number => "1234567890",
75
+ :bank_code => "321174851",
76
+ :name => "Jack Q Merchant"
77
+ ).save
78
+ @merchant = Balanced::Account.new(
79
+ :uri => @marketplace.accounts_uri,
80
+ :email_address => "merchant2@example.org",
81
+ :merchant => @merchant_attributes,
82
+ :bank_account_uri => bank_account.uri,
83
+ :name => "Jack Q Merchant"
84
+ ).save
85
+ end
86
+
87
+ describe "#id" do
88
+ subject { @merchant.id }
89
+ it { should_not be_nil }
90
+ it { should_not be_empty }
91
+ end
92
+ describe "#roles" do
93
+ subject { @merchant.roles }
94
+ it { should include("merchant") }
95
+ end
96
+ describe "#email_address" do
97
+ subject { @merchant.email_address }
98
+ it { should eql "merchant2@example.org" }
99
+ end
100
+ describe "#name" do
101
+ subject { @merchant.name }
102
+ it { should eql "Jack Q Merchant" }
103
+ end
104
+ describe "#created_at" do
105
+ subject { @merchant.created_at }
106
+ it { should_not be_nil }
107
+ it { should_not be_empty }
108
+ end
109
+ describe "#uri" do
110
+ subject { @merchant.uri }
111
+ it { should match MERCHANT_URI_REGEX }
112
+ end
113
+ describe "#holds_uri" do
114
+ subject { @merchant.holds_uri }
115
+ it { should match HOLDS_URI_REGEX }
116
+ end
117
+ describe "#bank_accounts_uri" do
118
+ subject { @merchant.bank_accounts_uri }
119
+ it { should match BANK_ACCOUNTS_URI_REGEX }
120
+ end
121
+ describe "#refunds_uri" do
122
+ subject { @merchant.refunds_uri }
123
+ it { should match REFUNDS_URI_REGEX }
124
+ end
125
+ describe "#debits_uri" do
126
+ subject { @merchant.debits_uri }
127
+ it { should match DEBITS_URI_REGEX }
128
+ end
129
+ describe "#transactions_uri" do
130
+ subject { @merchant.transactions_uri }
131
+ it { should match TRANSACTIONS_URI_REGEX }
132
+ end
133
+ describe "#credits_uri" do
134
+ subject { @merchant.credits_uri }
135
+ it { should match CREDITS_URI_REGEX }
136
+ end
137
+ describe "#cards_uri" do
138
+ subject { @merchant.cards_uri }
139
+ it { should match CARDS_URI_REGEX }
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ describe "#add_bank_account" do
146
+ describe "when executing" do
147
+ use_vcr_cassette
148
+
149
+ before do
150
+ @bank_account = Balanced::BankAccount.new(
151
+ :account_number => "1234567890",
152
+ :bank_code => "321174851",
153
+ :name => "Jack Q Merchant"
154
+ ).save
155
+
156
+ @merchant = Balanced::Account.new(
157
+ :uri => @marketplace.accounts_uri,
158
+ :email_address => "merchant1@example.org",
159
+ :merchant => @merchant_attributes,
160
+ :bank_account_uri => @bank_account.uri,
161
+ :name => "Jack Q Merchant"
162
+ ).save
163
+
164
+ @new_bank_account = Balanced::BankAccount.new(
165
+ :account_number => "53434533",
166
+ :bank_code => "12345678",
167
+ :name => "Jack Q Merchant"
168
+ ).save
169
+ end
170
+ it { -> { @merchant.add_bank_account(@new_bank_account.uri) }.should_not raise_error }
171
+ end
172
+
173
+ describe "after executing" do
174
+ use_vcr_cassette
175
+ before do
176
+ @bank_account = Balanced::BankAccount.new(
177
+ :account_number => "12345678901",
178
+ :bank_code => "321174851",
179
+ :name => "Jack Q Merchant"
180
+ ).save
181
+
182
+ @merchant = Balanced::Account.new(
183
+ :uri => @marketplace.accounts_uri,
184
+ :email_address => "merchant4@example.org",
185
+ :merchant => @merchant_attributes,
186
+ :bank_account_uri => @bank_account.uri,
187
+ :name => "Jack Q Merchant"
188
+ ).save
189
+
190
+ @new_bank_account = Balanced::BankAccount.new(
191
+ :account_number => "53434533",
192
+ :bank_code => "12345678",
193
+ :name => "Jack Q Merchant"
194
+ ).save
195
+ @merchant.add_bank_account(@new_bank_account.uri)
196
+ @bank_accounts = Balanced::BankAccount.find(@merchant.bank_accounts_uri).items
197
+ end
198
+
199
+ subject { @bank_accounts.size }
200
+ it { should eql 2 }
201
+ end
202
+
203
+
204
+ end
205
+ end
206
+
207
+ describe "buyer" do
208
+ describe "#save" do
209
+ describe "when creating" do
210
+ use_vcr_cassette
211
+ before do
212
+ card = Balanced::Card.new(
213
+ :card_number => "5105105105105100",
214
+ :expiration_month => "12",
215
+ :expiration_year => "2015",
216
+ ).save
217
+ @buyer = Balanced::Account.new(
218
+ :uri => @marketplace.accounts_uri,
219
+ :email_address => "buyer@example.org",
220
+ :card_uri => card.uri,
221
+ :name => "Jack Q Buyer"
222
+ )
223
+ end
224
+ it { -> { @buyer.save }.should_not raise_error }
225
+ end
226
+
227
+ describe "after #save" do
228
+ describe "attributes" do
229
+ use_vcr_cassette
230
+ before do
231
+ card = Balanced::Card.new(
232
+ :card_number => "4111111111111111",
233
+ :expiration_month => "12",
234
+ :expiration_year => "2015",
235
+ ).save
236
+ @buyer = Balanced::Account.new(
237
+ :uri => @marketplace.accounts_uri,
238
+ :email_address => "buyer2@example.org",
239
+ :card_uri => card.uri,
240
+ :name => "Jack Q Buyer"
241
+ ).save
242
+ end
243
+
244
+ describe "#id" do
245
+ subject { @buyer.id }
246
+ it { should_not be_nil }
247
+ it { should_not be_empty }
248
+ end
249
+ describe "#roles" do
250
+ subject { @buyer.roles }
251
+ it { should include("buyer") }
252
+ it { should_not include("merchant") }
253
+ end
254
+ describe "#email_address" do
255
+ subject { @buyer.email_address }
256
+ it { should eql "buyer2@example.org" }
257
+ end
258
+ describe "#name" do
259
+ subject { @buyer.name }
260
+ it { should eql "Jack Q Buyer" }
261
+ end
262
+ describe "#created_at" do
263
+ subject { @buyer.created_at }
264
+ it { should_not be_nil }
265
+ it { should_not be_empty }
266
+ end
267
+ describe "#uri" do
268
+ subject { @buyer.uri }
269
+ it { should match MERCHANT_URI_REGEX }
270
+ end
271
+ describe "#holds_uri" do
272
+ subject { @buyer.holds_uri }
273
+ it { should match HOLDS_URI_REGEX }
274
+ end
275
+ describe "#bank_accounts_uri" do
276
+ subject { @buyer.bank_accounts_uri }
277
+ it { should match BANK_ACCOUNTS_URI_REGEX }
278
+ end
279
+ describe "#refunds_uri" do
280
+ subject { @buyer.refunds_uri }
281
+ it { should match REFUNDS_URI_REGEX }
282
+ end
283
+ describe "#debits_uri" do
284
+ subject { @buyer.debits_uri }
285
+ it { should match DEBITS_URI_REGEX }
286
+ end
287
+ describe "#transactions_uri" do
288
+ subject { @buyer.transactions_uri }
289
+ it { should match TRANSACTIONS_URI_REGEX }
290
+ end
291
+ describe "#credits_uri" do
292
+ subject { @buyer.credits_uri }
293
+ it { should match CREDITS_URI_REGEX }
294
+ end
295
+ describe "#cards_uri" do
296
+ subject { @buyer.cards_uri }
297
+ it { should match CARDS_URI_REGEX }
298
+ end
299
+ end
300
+ end
301
+ end
302
+
303
+
304
+ describe "#add_card" do
305
+
306
+ describe "when executing" do
307
+ use_vcr_cassette
308
+ before do
309
+ card = Balanced::Card.new(
310
+ :card_number => "4111111111111111",
311
+ :expiration_month => "12",
312
+ :expiration_year => "2015",
313
+ ).save
314
+ @new_card = Balanced::Card.new(
315
+ :card_number => "4111111111111111",
316
+ :expiration_month => "1",
317
+ :expiration_year => "2015",
318
+ ).save
319
+ @buyer = Balanced::Account.new(
320
+ :uri => @marketplace.accounts_uri,
321
+ :email_address => "buyer3@example.org",
322
+ :card_uri => card.uri,
323
+ :name => "Jack Q Buyer"
324
+ ).save
325
+ end
326
+ it do
327
+ -> { @buyer.add_card(@new_card.uri) }.should_not raise_error
328
+ end
329
+ end
330
+ describe "after executing" do
331
+ use_vcr_cassette
332
+
333
+ before do
334
+ card = Balanced::Card.new(
335
+ :card_number => "4111111111111111",
336
+ :expiration_month => "12",
337
+ :expiration_year => "2015",
338
+ ).save
339
+ @new_card = Balanced::Card.new(
340
+ :card_number => "5105105105105100",
341
+ :expiration_month => "1",
342
+ :expiration_year => "2017",
343
+ ).save
344
+ @buyer = Balanced::Account.new(
345
+ :uri => @marketplace.accounts_uri,
346
+ :email_address => "buyer4@example.org",
347
+ :card_uri => card.uri,
348
+ :name => "Jack Q Buyer"
349
+ ).save
350
+ @buyer.add_card(@new_card.uri)
351
+ @cards = Balanced::Card.find @buyer.cards_uri
352
+ end
353
+ subject { @cards.items.size }
354
+ it { should eql 2 }
355
+ end
356
+ end
357
+
358
+ describe "#promote_to_merchant" do
359
+
360
+ describe "when executing" do
361
+ use_vcr_cassette
362
+ before do
363
+
364
+ @merchant_attributes = {
365
+ :type => "person",
366
+ :name => "Billy Jones",
367
+ :street_address => "801 High St.",
368
+ :postal_code => "94301",
369
+ :country => "USA",
370
+ :dob => "1842-01",
371
+ :phone_number => "+16505551234",
372
+ }
373
+ card = Balanced::Card.new(
374
+ :card_number => "4111111111111111",
375
+ :expiration_month => "12",
376
+ :expiration_year => "2015",
377
+ ).save
378
+ @buyer = Balanced::Account.new(
379
+ :uri => @marketplace.accounts_uri,
380
+ :email_address => "buyer5@example.org",
381
+ :card_uri => card.uri,
382
+ :name => "Jack Q Buyer"
383
+ ).save
384
+ end
385
+
386
+ it do
387
+ -> { @buyer.promote_to_merchant @merchant_attributes}.should_not raise_error
388
+ end
389
+ end
390
+ describe "after executing" do
391
+ use_vcr_cassette
392
+
393
+ before do
394
+ @merchant_attributes = {
395
+ :type => "person",
396
+ :name => "Billy Jones",
397
+ :street_address => "801 High St.",
398
+ :postal_code => "94301",
399
+ :country => "USA",
400
+ :dob => "1842-01",
401
+ :phone_number => "+16505551234",
402
+ }
403
+ card = Balanced::Card.new(
404
+ :card_number => "4111111111111111",
405
+ :expiration_month => "12",
406
+ :expiration_year => "2015",
407
+ ).save
408
+ @buyer = Balanced::Account.new(
409
+ :uri => @marketplace.accounts_uri,
410
+ :email_address => "buyer6@example.org",
411
+ :card_uri => card.uri,
412
+ :name => "Jack Q Buyer"
413
+ ).save
414
+ @buyer.promote_to_merchant @merchant_attributes
415
+ end
416
+ subject { @buyer.roles }
417
+ it { should include("merchant") }
418
+ end
419
+
420
+ end
421
+ end
422
+ end