balanced-ach 0.1

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 (77) hide show
  1. data/.idea/.name +1 -0
  2. data/.idea/.rakeTasks +7 -0
  3. data/.idea/balanced-ach.iml +9 -0
  4. data/.idea/encodings.xml +5 -0
  5. data/.idea/misc.xml +5 -0
  6. data/.idea/modules.xml +9 -0
  7. data/.idea/scopes/scope_settings.xml +5 -0
  8. data/.idea/vcs.xml +7 -0
  9. data/.idea/workspace.xml +561 -0
  10. data/CONTRIBUTORS +1 -0
  11. data/Gemfile +19 -0
  12. data/Gemfile.lock +64 -0
  13. data/Guardfile +7 -0
  14. data/LICENSE +22 -0
  15. data/README.md +64 -0
  16. data/Rakefile +12 -0
  17. data/balanced.gemspec +22 -0
  18. data/lib/balanced.rb +84 -0
  19. data/lib/balanced/client.rb +82 -0
  20. data/lib/balanced/error.rb +85 -0
  21. data/lib/balanced/pager.rb +201 -0
  22. data/lib/balanced/resources.rb +5 -0
  23. data/lib/balanced/resources/bank_account.rb +36 -0
  24. data/lib/balanced/resources/credit.rb +20 -0
  25. data/lib/balanced/resources/debit.rb +27 -0
  26. data/lib/balanced/resources/resource.rb +158 -0
  27. data/lib/balanced/response/balanced_exception_middleware.rb +33 -0
  28. data/lib/balanced/utils.rb +62 -0
  29. data/lib/balanced/version.rb +3 -0
  30. data/spec/balanced/pager_spec.rb +42 -0
  31. data/spec/balanced/resources/account_spec.rb +571 -0
  32. data/spec/balanced/resources/api_key_spec.rb +55 -0
  33. data/spec/balanced/resources/hold_spec.rb +55 -0
  34. data/spec/balanced/resources/marketplace_spec.rb +97 -0
  35. data/spec/balanced/resources/transactions_spec.rb +72 -0
  36. data/spec/balanced/response/balanced_exception_middleware_spec.rb +47 -0
  37. data/spec/balanced_spec.rb +77 -0
  38. data/spec/cassettes/Balanced/configure.yml +48 -0
  39. data/spec/cassettes/Balanced/configure/reconfigure_with_new_api_key.yml +48 -0
  40. data/spec/cassettes/Balanced_Account.yml +110 -0
  41. data/spec/cassettes/Balanced_Account/Account_uri/when_ApiKey_is_configured.yml +240 -0
  42. data/spec/cassettes/Balanced_Account/Account_uri/when_ApiKey_is_not_configured.yml +44 -0
  43. data/spec/cassettes/Balanced_Account/_find.yml +400 -0
  44. data/spec/cassettes/Balanced_Account/_find/_all_some_field_op_.yml +186 -0
  45. data/spec/cassettes/Balanced_Account/_find/_first_some_field_op_.yml +186 -0
  46. data/spec/cassettes/Balanced_Account/_find_by_email.yml +400 -0
  47. data/spec/cassettes/Balanced_Account/_find_by_email/email_address_does_not_exist.yml +175 -0
  48. data/spec/cassettes/Balanced_Account/_find_by_email/email_address_is_in_system.yml +186 -0
  49. data/spec/cassettes/Balanced_Account/buyer/_add_card/after_executing.yml +530 -0
  50. data/spec/cassettes/Balanced_Account/buyer/_add_card/when_executing.yml +455 -0
  51. data/spec/cassettes/Balanced_Account/buyer/_debit.yml +363 -0
  52. data/spec/cassettes/Balanced_Account/buyer/_promote_to_merchant/after_executing.yml +281 -0
  53. data/spec/cassettes/Balanced_Account/buyer/_promote_to_merchant/when_executing.yml +281 -0
  54. data/spec/cassettes/Balanced_Account/buyer/_save/after_save/attributes.yml +1013 -0
  55. data/spec/cassettes/Balanced_Account/buyer/_save/when_creating.yml +229 -0
  56. data/spec/cassettes/Balanced_Account/merchant/_add_bank_account/after_executing.yml +536 -0
  57. data/spec/cassettes/Balanced_Account/merchant/_add_bank_account/when_executing.yml +460 -0
  58. data/spec/cassettes/Balanced_Account/merchant/_save/after_save/attributes.yml +232 -0
  59. data/spec/cassettes/Balanced_Account/merchant/_save/when_creating.yml +232 -0
  60. data/spec/cassettes/Balanced_Account/merchant/new.yml +179 -0
  61. data/spec/cassettes/Balanced_ApiKey/attributes.yml +48 -0
  62. data/spec/cassettes/Balanced_ApiKey/new_key/after_configure.yml +93 -0
  63. data/spec/cassettes/Balanced_ApiKey/new_key/before_configure.yml +48 -0
  64. data/spec/cassettes/Balanced_Hold.yml +335 -0
  65. data/spec/cassettes/Balanced_Hold/_void.yml +62 -0
  66. data/spec/cassettes/Balanced_Hold/_void/after.yml +62 -0
  67. data/spec/cassettes/Balanced_Hold/_void/when_exception_is_thrown.yml +306 -0
  68. data/spec/cassettes/Balanced_Marketplace.yml +339 -0
  69. data/spec/cassettes/Balanced_Transaction.yml +1261 -0
  70. data/spec/cassettes/Balanced_Transaction/Transaction.yml +634 -0
  71. data/spec/cassettes/Balanced_Transaction/Transaction_paginate.yml +634 -0
  72. data/spec/client_spec.rb +12 -0
  73. data/spec/spec_helper.rb +31 -0
  74. data/spec/utils_spec.rb +8 -0
  75. data/upload_docs.rb +39 -0
  76. data/x.rb +22 -0
  77. metadata +199 -0
@@ -0,0 +1,33 @@
1
+ require 'faraday'
2
+ require 'balanced/error'
3
+
4
+ # @api private
5
+ module Faraday
6
+
7
+ class Response::RaiseBalancedError < Response::Middleware
8
+
9
+ HTTP_STATUS_CODES = {
10
+ 300 => Balanced::MoreInformationRequired,
11
+ 400 => Balanced::BadRequest,
12
+ 401 => Balanced::Unauthorized,
13
+ 402 => Balanced::PaymentRequired,
14
+ 403 => Balanced::Forbidden,
15
+ 404 => Balanced::NotFound,
16
+ 405 => Balanced::MethodNotAllowed,
17
+ 409 => Balanced::Conflict,
18
+ 410 => Balanced::Gone,
19
+ 500 => Balanced::InternalServerError,
20
+ 501 => Balanced::NotImplemented,
21
+ 502 => Balanced::BadGateway,
22
+ 503 => Balanced::ServiceUnavailable,
23
+ 504 => Balanced::GatewayTimeout,
24
+ }
25
+ def on_complete(response)
26
+ status_code = response[:status].to_i
27
+ error_class = HTTP_STATUS_CODES[status_code]
28
+ raise error_class.new(response) if error_class
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,62 @@
1
+ module Balanced
2
+ module Utils
3
+ def camelize underscored_word
4
+ underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
5
+ end
6
+
7
+ def classify table_name
8
+ camelize singularize(table_name.to_s.sub(/.*\./, ''))
9
+ end
10
+
11
+ def demodulize class_name_in_module
12
+ class_name_in_module.to_s.sub(/^.*::/, '')
13
+ end
14
+
15
+ def pluralize word
16
+ word.to_s.sub(/([^s])$/, '\1s')
17
+ end
18
+
19
+ def singularize word
20
+ word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
21
+ end
22
+
23
+ def underscore camel_cased_word
24
+ word = camel_cased_word.to_s.dup
25
+ word.gsub!(/::/, '/')
26
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
27
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
28
+ word.tr! "-", "_"
29
+ word.downcase!
30
+ word
31
+ end
32
+
33
+ def hash_with_indifferent_read_access base = {}
34
+ indifferent = Hash.new do |hash, key|
35
+ hash[key.to_s] if key.is_a? Symbol
36
+ end
37
+ base.each_pair do |key, value|
38
+ if value.is_a? Hash
39
+ value = hash_with_indifferent_read_access value
40
+ elsif value.respond_to? :each
41
+ value.map! do |v|
42
+ if v.is_a? Hash
43
+ v = hash_with_indifferent_read_access v
44
+ end
45
+ v
46
+ end
47
+ end
48
+ indifferent[key.to_s] = value
49
+ end
50
+ indifferent
51
+ end
52
+
53
+ def stringify_keys! hash
54
+ hash.keys.each do |key|
55
+ stringify_keys! hash[key] if hash[key].is_a? Hash
56
+ hash[key.to_s] = hash.delete key if key.is_a? Symbol
57
+ end
58
+ end
59
+
60
+ extend self
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Balanced
2
+ VERSION = '0.1'
3
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Pager do
4
+ describe "#adjust_pagination_params" do
5
+ subject { Balanced::Pager.new 'a uri'}
6
+
7
+ it "sets limit based on per" do
8
+ params = subject.send(:adjust_pagination_params, per: 5)
9
+ params[:limit].should == 5
10
+ end
11
+
12
+ it "sets offset based on page and default limit" do
13
+ params = subject.send(:adjust_pagination_params, page: 2)
14
+ params[:offset].should == 10
15
+ end
16
+
17
+ it "sets offset based on page and per" do
18
+ params = subject.send(:adjust_pagination_params, page: 2, per: 4)
19
+ params[:offset].should == 4
20
+ end
21
+
22
+ it "prefers per to limit" do
23
+ params = subject.send(:adjust_pagination_params, per: 5, limit: 10)
24
+ params[:limit].should == 5
25
+ end
26
+
27
+ it "falls back to limit" do
28
+ params = subject.send(:adjust_pagination_params, limit: 3)
29
+ params[:limit].should == 3
30
+ end
31
+
32
+ it "prefers page to offset" do
33
+ params = subject.send(:adjust_pagination_params, page: 2, offset: 0)
34
+ params[:offset].should == 10
35
+ end
36
+
37
+ it "falls back to offset" do
38
+ params = subject.send(:adjust_pagination_params, offset: 6)
39
+ params[:offset].should == 6
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,571 @@
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 "Account.uri" do
12
+ use_vcr_cassette
13
+
14
+ context "when ApiKey is not configured" do
15
+ use_vcr_cassette
16
+ before do
17
+ Balanced.configure nil
18
+ end
19
+
20
+ it "throw an exception that it can not generate the URI" do
21
+ expect {
22
+ Balanced::Account.uri
23
+ }.to raise_error Balanced::Error
24
+ end
25
+ end
26
+
27
+ context "when ApiKey is configured" do
28
+ use_vcr_cassette
29
+ before do
30
+ api_key = Balanced::ApiKey.new.save
31
+ Balanced.configure api_key.secret
32
+ @marketplace = Balanced::Marketplace.new.save
33
+ end
34
+
35
+ it "it matches the resource's uri structure" do
36
+ uri = Balanced::Account.uri
37
+ uri.should_not be_nil
38
+ uri.should match ACCOUNTS_URI_REGEX
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "merchant" do
44
+ use_vcr_cassette
45
+
46
+ before do
47
+ @merchant_attributes = {
48
+ :type => "person",
49
+ :name => "Billy Jones",
50
+ :street_address => "801 High St.",
51
+ :postal_code => "94301",
52
+ :country => "USA",
53
+ :dob => "1842-01",
54
+ :phone_number => "+16505551234",
55
+ }
56
+ end
57
+
58
+ describe "new" do
59
+ use_vcr_cassette
60
+ before do
61
+ @bank_account = Balanced::BankAccount.new(
62
+ :account_number => "1234567890",
63
+ :bank_code => "321174851",
64
+ :name => "Jack Q Merchant"
65
+ ).save
66
+ end
67
+ it do
68
+ -> do
69
+ @merchant = Balanced::Account.new(
70
+ :uri => @marketplace.accounts_uri,
71
+ :email_address => "merchant@example.org",
72
+ :merchant => @merchant_attributes,
73
+ :bank_account_uri => @bank_account.uri,
74
+ :name => "Jack Q Merchant"
75
+ )
76
+ end.should_not raise_error
77
+ end
78
+ end
79
+
80
+ describe "#save" do
81
+
82
+ describe "when creating" do
83
+ use_vcr_cassette
84
+ before do
85
+ bank_account = Balanced::BankAccount.new(
86
+ :account_number => "1234567890",
87
+ :bank_code => "321174851",
88
+ :name => "Jack Q Merchant"
89
+ ).save
90
+ @merchant = Balanced::Account.new(
91
+ :uri => @marketplace.accounts_uri,
92
+ :email_address => "merchant@example.org",
93
+ :merchant => @merchant_attributes,
94
+ :bank_account_uri => bank_account.uri,
95
+ :name => "Jack Q Merchant"
96
+ )
97
+ end
98
+ it { -> { @merchant.save }.should_not raise_error }
99
+ end
100
+
101
+ describe "after #save" do
102
+ describe "attributes" do
103
+ use_vcr_cassette
104
+ before do
105
+ bank_account = Balanced::BankAccount.new(
106
+ :account_number => "1234567890",
107
+ :bank_code => "321174851",
108
+ :name => "Jack Q Merchant"
109
+ ).save
110
+ @merchant = Balanced::Account.new(
111
+ :uri => @marketplace.accounts_uri,
112
+ :email_address => "merchant2@example.org",
113
+ :merchant => @merchant_attributes,
114
+ :bank_account_uri => bank_account.uri,
115
+ :name => "Jack Q Merchant"
116
+ ).save
117
+ end
118
+
119
+ describe "#id" do
120
+ subject { @merchant.id }
121
+ it { should_not be_nil }
122
+ it { should_not be_empty }
123
+ end
124
+ describe "#roles" do
125
+ subject { @merchant.roles }
126
+ it { should include("merchant") }
127
+ end
128
+ describe "#email_address" do
129
+ subject { @merchant.email_address }
130
+ it { should eql "merchant2@example.org" }
131
+ end
132
+ describe "#name" do
133
+ subject { @merchant.name }
134
+ it { should eql "Jack Q Merchant" }
135
+ end
136
+ describe "#created_at" do
137
+ subject { @merchant.created_at }
138
+ it { should_not be_nil }
139
+ it { should_not be_empty }
140
+ end
141
+ describe "#uri" do
142
+ subject { @merchant.uri }
143
+ it { should match MERCHANT_URI_REGEX }
144
+ end
145
+ describe "#holds_uri" do
146
+ subject { @merchant.holds_uri }
147
+ it { should match HOLDS_URI_REGEX }
148
+ end
149
+ describe "#bank_accounts_uri" do
150
+ subject { @merchant.bank_accounts_uri }
151
+ it { should match BANK_ACCOUNTS_URI_REGEX }
152
+ end
153
+ describe "#refunds_uri" do
154
+ subject { @merchant.refunds_uri }
155
+ it { should match REFUNDS_URI_REGEX }
156
+ end
157
+ describe "#debits_uri" do
158
+ subject { @merchant.debits_uri }
159
+ it { should match DEBITS_URI_REGEX }
160
+ end
161
+ describe "#transactions_uri" do
162
+ subject { @merchant.transactions_uri }
163
+ it { should match TRANSACTIONS_URI_REGEX }
164
+ end
165
+ describe "#credits_uri" do
166
+ subject { @merchant.credits_uri }
167
+ it { should match CREDITS_URI_REGEX }
168
+ end
169
+ describe "#cards_uri" do
170
+ subject { @merchant.cards_uri }
171
+ it { should match CARDS_URI_REGEX }
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+ describe "#add_bank_account" do
178
+ describe "when executing" do
179
+ use_vcr_cassette
180
+
181
+ before do
182
+ @bank_account = Balanced::BankAccount.new(
183
+ :account_number => "1234567890",
184
+ :bank_code => "321174851",
185
+ :name => "Jack Q Merchant"
186
+ ).save
187
+
188
+ @merchant = Balanced::Account.new(
189
+ :uri => @marketplace.accounts_uri,
190
+ :email_address => "merchant1@example.org",
191
+ :merchant => @merchant_attributes,
192
+ :bank_account_uri => @bank_account.uri,
193
+ :name => "Jack Q Merchant"
194
+ ).save
195
+
196
+ @new_bank_account = Balanced::BankAccount.new(
197
+ :account_number => "53434533",
198
+ :bank_code => "12345678",
199
+ :name => "Jack Q Merchant"
200
+ ).save
201
+ end
202
+ it { -> { @merchant.add_bank_account(@new_bank_account.uri) }.should_not raise_error }
203
+ end
204
+
205
+ describe "after executing" do
206
+ use_vcr_cassette
207
+ before do
208
+ @bank_account = Balanced::BankAccount.new(
209
+ :account_number => "12345678901",
210
+ :bank_code => "321174851",
211
+ :name => "Jack Q Merchant"
212
+ ).save
213
+
214
+ @merchant = Balanced::Account.new(
215
+ :uri => @marketplace.accounts_uri,
216
+ :email_address => "merchant4@example.org",
217
+ :merchant => @merchant_attributes,
218
+ :bank_account_uri => @bank_account.uri,
219
+ :name => "Jack Q Merchant"
220
+ ).save
221
+
222
+ @new_bank_account = Balanced::BankAccount.new(
223
+ :account_number => "53434533",
224
+ :bank_code => "12345678",
225
+ :name => "Jack Q Merchant"
226
+ ).save
227
+ @merchant.add_bank_account(@new_bank_account.uri)
228
+ @bank_accounts = Balanced::BankAccount.find(@merchant.bank_accounts_uri).items
229
+ end
230
+
231
+ subject { @bank_accounts.size }
232
+ it { should eql 2 }
233
+ end
234
+
235
+
236
+ end
237
+ end
238
+
239
+ describe "buyer" do
240
+ describe "#save" do
241
+ describe "when creating" do
242
+ use_vcr_cassette :record => :new_episodes
243
+ before do
244
+ card = Balanced::Card.new(
245
+ :card_number => "5105105105105100",
246
+ :expiration_month => "12",
247
+ :expiration_year => "2015",
248
+ ).save
249
+ @buyer = Balanced::Account.new(
250
+ :uri => @marketplace.accounts_uri,
251
+ :email_address => "buyer@example.org",
252
+ :card_uri => card.uri,
253
+ :name => "Jack Q Buyer"
254
+ )
255
+ end
256
+ it { -> { @buyer.save }.should_not raise_error }
257
+ end
258
+
259
+ describe "after #save" do
260
+ describe "attributes" do
261
+ use_vcr_cassette :record => :new_episodes
262
+ before do
263
+ card = Balanced::Card.new(
264
+ :card_number => "4111111111111111",
265
+ :expiration_month => "12",
266
+ :expiration_year => "2015",
267
+ ).save
268
+ @buyer = Balanced::Account.new(
269
+ :uri => @marketplace.accounts_uri,
270
+ :email_address => "buyer2@example.org",
271
+ :card_uri => card.uri,
272
+ :name => "Jack Q Buyer"
273
+ ).save
274
+ end
275
+
276
+ describe "#id" do
277
+ subject { @buyer.id }
278
+ it { should_not be_nil }
279
+ it { should_not be_empty }
280
+ end
281
+ describe "#roles" do
282
+ subject { @buyer.roles }
283
+ it { should include("buyer") }
284
+ it { should_not include("merchant") }
285
+ end
286
+ describe "#email_address" do
287
+ subject { @buyer.email_address }
288
+ it { should eql "buyer2@example.org" }
289
+ end
290
+ describe "#name" do
291
+ subject { @buyer.name }
292
+ it { should eql "Jack Q Buyer" }
293
+ end
294
+ describe "#created_at" do
295
+ subject { @buyer.created_at }
296
+ it { should_not be_nil }
297
+ it { should_not be_empty }
298
+ end
299
+ describe "#uri" do
300
+ subject { @buyer.uri }
301
+ it { should match MERCHANT_URI_REGEX }
302
+ end
303
+ describe "#holds_uri" do
304
+ subject { @buyer.holds_uri }
305
+ it { should match HOLDS_URI_REGEX }
306
+ end
307
+ describe "#bank_accounts_uri" do
308
+ subject { @buyer.bank_accounts_uri }
309
+ it { should match BANK_ACCOUNTS_URI_REGEX }
310
+ end
311
+ describe "#refunds_uri" do
312
+ subject { @buyer.refunds_uri }
313
+ it { should match REFUNDS_URI_REGEX }
314
+ end
315
+ describe "#debits_uri" do
316
+ subject { @buyer.debits_uri }
317
+ it { should match DEBITS_URI_REGEX }
318
+ end
319
+ describe "#transactions_uri" do
320
+ subject { @buyer.transactions_uri }
321
+ it { should match TRANSACTIONS_URI_REGEX }
322
+ end
323
+ describe "#credits_uri" do
324
+ subject { @buyer.credits_uri }
325
+ it { should match CREDITS_URI_REGEX }
326
+ end
327
+ describe "#cards_uri" do
328
+ subject { @buyer.cards_uri }
329
+ it { should match CARDS_URI_REGEX }
330
+ end
331
+ describe "#cards" do
332
+ subject { @buyer.cards }
333
+ it { should be_instance_of Array }
334
+ it { should_not be_empty }
335
+ it { subject.first.should be_instance_of Balanced::Card }
336
+ end
337
+ describe "#bank_accounts" do
338
+ subject { @buyer.bank_accounts }
339
+ it { should be_instance_of Array }
340
+ it { should be_empty }
341
+ end
342
+ end
343
+ end
344
+ end
345
+
346
+ describe "#add_card" do
347
+
348
+ describe "when executing" do
349
+ use_vcr_cassette
350
+ before do
351
+ card = Balanced::Card.new(
352
+ :card_number => "4111111111111111",
353
+ :expiration_month => "12",
354
+ :expiration_year => "2015",
355
+ ).save
356
+ @new_card = Balanced::Card.new(
357
+ :card_number => "4111111111111111",
358
+ :expiration_month => "1",
359
+ :expiration_year => "2015",
360
+ ).save
361
+ @buyer = Balanced::Account.new(
362
+ :uri => @marketplace.accounts_uri,
363
+ :email_address => "buyer3@example.org",
364
+ :card_uri => card.uri,
365
+ :name => "Jack Q Buyer"
366
+ ).save
367
+ end
368
+ it do
369
+ -> { @buyer.add_card(@new_card.uri) }.should_not raise_error
370
+ end
371
+ end
372
+ describe "after executing" do
373
+ use_vcr_cassette
374
+
375
+ before do
376
+ card = Balanced::Card.new(
377
+ :card_number => "4111111111111111",
378
+ :expiration_month => "12",
379
+ :expiration_year => "2015",
380
+ ).save
381
+ @new_card = Balanced::Card.new(
382
+ :card_number => "5105105105105100",
383
+ :expiration_month => "1",
384
+ :expiration_year => "2017",
385
+ ).save
386
+ @buyer = Balanced::Account.new(
387
+ :uri => @marketplace.accounts_uri,
388
+ :email_address => "buyer4@example.org",
389
+ :card_uri => card.uri,
390
+ :name => "Jack Q Buyer"
391
+ ).save
392
+ @buyer.add_card(@new_card.uri)
393
+ @cards = Balanced::Card.find @buyer.cards_uri
394
+ end
395
+ subject { @cards.items.size }
396
+ it { should eql 2 }
397
+ end
398
+ end
399
+
400
+ describe "#promote_to_merchant" do
401
+
402
+ describe "when executing" do
403
+ use_vcr_cassette
404
+ before do
405
+
406
+ @merchant_attributes = {
407
+ :type => "person",
408
+ :name => "Billy Jones",
409
+ :street_address => "801 High St.",
410
+ :postal_code => "94301",
411
+ :country => "USA",
412
+ :dob => "1842-01",
413
+ :phone_number => "+16505551234",
414
+ }
415
+ card = Balanced::Card.new(
416
+ :card_number => "4111111111111111",
417
+ :expiration_month => "12",
418
+ :expiration_year => "2015",
419
+ ).save
420
+ @buyer = Balanced::Account.new(
421
+ :uri => @marketplace.accounts_uri,
422
+ :email_address => "buyer5@example.org",
423
+ :card_uri => card.uri,
424
+ :name => "Jack Q Buyer"
425
+ ).save
426
+ end
427
+
428
+ it do
429
+ -> { @buyer.promote_to_merchant @merchant_attributes}.should_not raise_error
430
+ end
431
+ end
432
+ describe "after executing" do
433
+ use_vcr_cassette
434
+
435
+ before do
436
+ @merchant_attributes = {
437
+ :type => "person",
438
+ :name => "Billy Jones",
439
+ :street_address => "801 High St.",
440
+ :postal_code => "94301",
441
+ :country => "USA",
442
+ :dob => "1842-01",
443
+ :phone_number => "+16505551234",
444
+ }
445
+ card = Balanced::Card.new(
446
+ :card_number => "4111111111111111",
447
+ :expiration_month => "12",
448
+ :expiration_year => "2015",
449
+ ).save
450
+ @buyer = Balanced::Account.new(
451
+ :uri => @marketplace.accounts_uri,
452
+ :email_address => "buyer6@example.org",
453
+ :card_uri => card.uri,
454
+ :name => "Jack Q Buyer"
455
+ ).save
456
+ @buyer.promote_to_merchant @merchant_attributes
457
+ end
458
+ subject { @buyer.roles }
459
+ it { should include("merchant") }
460
+ end
461
+
462
+ end
463
+
464
+ describe "#debit" do
465
+ use_vcr_cassette :match_requests_on => [:body], :record => :new_episodes
466
+ before do
467
+ card = Balanced::Card.new(
468
+ :card_number => "4111111111111111",
469
+ :expiration_month => "12",
470
+ :expiration_year => "2015",
471
+ ).save
472
+ begin
473
+ @buyer = Balanced::Account.new(
474
+ :uri => @marketplace.accounts_uri,
475
+ :email_address => "buyer7@example.org",
476
+ :card_uri => card.uri,
477
+ :name => "Jack Q Buyer"
478
+ ).save
479
+ rescue Balanced::Conflict => ex
480
+ @buyer = Balanced::Account.find(ex.extras[:account_uri])
481
+ end
482
+ end
483
+ it "takes optional parameters" do
484
+ debit = @buyer.debit(
485
+ :amount => 500,
486
+ :appears_on_statement_as => "BOBS BURGERS",
487
+ )
488
+ debit.should be_instance_of Balanced::Debit
489
+ debit.amount.should eql 500
490
+ debit.appears_on_statement_as.should eql "BOBS BURGERS"
491
+ end
492
+ it "takes positional parameters" do
493
+ debit = @buyer.debit(500, "FOO FIGHTER")
494
+ debit.should be_instance_of Balanced::Debit
495
+ debit.amount.should eql 500
496
+ debit.appears_on_statement_as.should eql "FOO FIGHTER"
497
+ end
498
+
499
+ end
500
+ end
501
+
502
+ describe ".find" do
503
+ use_vcr_cassette
504
+
505
+ before do
506
+ api_key = Balanced::ApiKey.new.save
507
+ Balanced.configure api_key.secret
508
+ @marketplace = Balanced::Marketplace.new.save
509
+ card = Balanced::Card.new(
510
+ :card_number => "5105105105105100",
511
+ :expiration_month => "12",
512
+ :expiration_year => "2015",
513
+ ).save
514
+ Balanced::Marketplace.my_marketplace.create_buyer(
515
+ "john.doe@example.com",
516
+ card.uri
517
+ )
518
+ end
519
+
520
+ context "(:all, :some_field => 'op')" do
521
+ use_vcr_cassette
522
+ it "should find the account by returning a page with items of one" do
523
+ response = Balanced::Account.find(:all, :email_address => "john.doe@example.com")
524
+ response.should be_instance_of Array
525
+ response[0].should be_instance_of Balanced::Account
526
+ end
527
+ end
528
+
529
+ context "(:first, :some_field => 'op')" do
530
+ use_vcr_cassette
531
+ it "should find the account by returning the first item" do
532
+ response = Balanced::Account.find(:first, :email_address => "john.doe@example.com")
533
+ response.should be_instance_of Balanced::Account
534
+ end
535
+ end
536
+
537
+
538
+ end
539
+
540
+ describe ".find_by_email" do
541
+ use_vcr_cassette :record => :new_episodes
542
+ before do
543
+ api_key = Balanced::ApiKey.new.save
544
+ Balanced.configure api_key.secret
545
+ @marketplace = Balanced::Marketplace.new.save
546
+ card = Balanced::Card.new(
547
+ :card_number => "5105105105105100",
548
+ :expiration_month => "12",
549
+ :expiration_year => "2015",
550
+ ).save
551
+ buyer = Balanced::Marketplace.my_marketplace.create_buyer(
552
+ "john.doe@example.com",
553
+ card.uri
554
+ )
555
+ end
556
+
557
+ context "email address is in system" do
558
+ use_vcr_cassette :record => :new_episodes
559
+ it "should return account object" do
560
+ Balanced::Account.find_by_email("john.doe@example.com").should be_instance_of Balanced::Account
561
+ end
562
+ end
563
+
564
+ context "email address does not exist" do
565
+ use_vcr_cassette :record => :new_episodes
566
+ it "should return nil" do
567
+ Balanced::Account.find_by_email("foo@bar.com").should be_nil
568
+ end
569
+ end
570
+ end
571
+ end