balanced-ach 0.1

Sign up to get free protection for your applications and to get access to all the features.
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,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::ApiKey do
4
+ describe "attributes" do
5
+ use_vcr_cassette
6
+ before do
7
+ @key = Balanced::ApiKey.new.save
8
+ end
9
+ describe "#secret" do
10
+ subject { @key.secret }
11
+ it { should_not be_nil }
12
+ it { should_not be_empty }
13
+ end
14
+
15
+ describe "#merchant" do
16
+ subject { @key.merchant }
17
+ it { should_not be_nil }
18
+ it { should be_instance_of Balanced::Merchant }
19
+ end
20
+ end
21
+
22
+ describe "new key" do
23
+ use_vcr_cassette
24
+
25
+ describe "before configure" do
26
+ use_vcr_cassette
27
+ before do
28
+ @new_key = Balanced::ApiKey.new.save
29
+ end
30
+ describe "#merchant" do
31
+ use_vcr_cassette
32
+ subject { @new_key.merchant }
33
+ it { should_not be_nil }
34
+ it { should be_instance_of Balanced::Merchant }
35
+ end
36
+ end
37
+
38
+ describe "after configure" do
39
+ use_vcr_cassette
40
+ before do
41
+ @new_key = Balanced::ApiKey.new.save
42
+ Balanced.configure @new_key.secret
43
+ @new_key = Balanced::ApiKey.new.save
44
+ end
45
+
46
+ describe "#merchant" do
47
+ use_vcr_cassette
48
+ subject { @new_key.merchant }
49
+ it { should_not be_nil }
50
+ it { should be_instance_of Balanced::Merchant }
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Hold 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
+ card = Balanced::Card.new(
10
+ :card_number => "5105105105105100",
11
+ :expiration_month => "12",
12
+ :expiration_year => "2015"
13
+ ).save
14
+ @buyer = @marketplace.create_buyer("buyer@example.org", card.uri)
15
+ end
16
+
17
+ describe "#void" do
18
+ use_vcr_cassette
19
+
20
+ before do
21
+ @hold = @buyer.hold 100
22
+ end
23
+
24
+ describe 'before' do
25
+ use_vcr_cassette
26
+ subject { @hold.is_void }
27
+ it { should be_false }
28
+ end
29
+
30
+ describe 'after' do
31
+ use_vcr_cassette
32
+ before { @hold.void }
33
+
34
+ subject { @hold.is_void }
35
+ it { should be_true }
36
+ end
37
+
38
+ describe 'when exception is thrown' do
39
+ use_vcr_cassette
40
+ before do
41
+ @hold = @buyer.hold 150
42
+ @debit = @hold.capture
43
+ end
44
+
45
+ it "should not change void state since its captured" do
46
+ lambda { @hold.void }.should raise_error(Balanced::Conflict)
47
+ @hold.is_void.should be_false
48
+ end
49
+ # void here.
50
+
51
+
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,97 @@
1
+ require "spec_helper"
2
+
3
+
4
+ describe Balanced::Marketplace do
5
+ use_vcr_cassette
6
+ before do
7
+ api_key = Balanced::ApiKey.new.save
8
+ Balanced.configure api_key.secret
9
+ @marketplace = Balanced::Marketplace.new.save
10
+ @bank_account = Balanced::BankAccount.new(
11
+ :account_number => "1234567890",
12
+ :bank_code => "321174851",
13
+ :name => "Jack Q Merchant"
14
+ ).save
15
+ @merchant = @marketplace.create_merchant(
16
+ "merchant@example.org",
17
+ {
18
+ :type => "person",
19
+ :name => "Billy Jones",
20
+ :street_address => "801 High St.",
21
+ :postal_code => "94301",
22
+ :country => "USA",
23
+ :dob => "1842-01",
24
+ :phone_number => "+16505551234",
25
+ },
26
+ @bank_account.uri,
27
+ "Jack Q Merchant",
28
+ )
29
+ end
30
+
31
+ describe "create_merchant" do
32
+ use_vcr_cassette
33
+
34
+ describe "class" do
35
+ subject { @merchant }
36
+ it { should be_instance_of Balanced::Account }
37
+ end
38
+
39
+ describe "attributes" do
40
+ describe "#id" do
41
+ subject { @merchant.id }
42
+ it { should_not be_nil }
43
+ it { should_not be_empty }
44
+ end
45
+ describe "#roles" do
46
+ subject { @merchant.roles }
47
+ it { should include("merchant") }
48
+ end
49
+ describe "#email_address" do
50
+ subject { @merchant.email_address }
51
+ it { should eql "merchant@example.org" }
52
+ end
53
+ describe "#name" do
54
+ subject { @merchant.name }
55
+ it { should eql "Jack Q Merchant" }
56
+ end
57
+ describe "#created_at" do
58
+ subject { @merchant.created_at }
59
+ it { should_not be_nil }
60
+ it { should_not be_empty }
61
+ end
62
+ describe "#uri" do
63
+ subject { @merchant.uri }
64
+ it { should match MERCHANT_URI_REGEX }
65
+ end
66
+ describe "#holds_uri" do
67
+ subject { @merchant.holds_uri }
68
+ it { should match HOLDS_URI_REGEX }
69
+ end
70
+ describe "#bank_accounts_uri" do
71
+ subject { @merchant.bank_accounts_uri }
72
+ it { should match BANK_ACCOUNTS_URI_REGEX }
73
+ end
74
+ describe "#refunds_uri" do
75
+ subject { @merchant.refunds_uri }
76
+ it { should match REFUNDS_URI_REGEX }
77
+ end
78
+ describe "#debits_uri" do
79
+ subject { @merchant.debits_uri }
80
+ it { should match DEBITS_URI_REGEX }
81
+ end
82
+ describe "#transactions_uri" do
83
+ subject { @merchant.transactions_uri }
84
+ it { should match TRANSACTIONS_URI_REGEX }
85
+ end
86
+ describe "#credits_uri" do
87
+ subject { @merchant.credits_uri }
88
+ it { should match CREDITS_URI_REGEX }
89
+ end
90
+ describe "#cards_uri" do
91
+ subject { @merchant.cards_uri }
92
+ it { should match CARDS_URI_REGEX }
93
+ end
94
+ end
95
+ end
96
+ end
97
+
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Transaction do
4
+
5
+ use_vcr_cassette
6
+
7
+ before do
8
+ api_key = Balanced::ApiKey.new.save
9
+ Balanced.configure api_key.secret
10
+ @marketplace = Balanced::Marketplace.new.save
11
+ @merchant_attributes = {
12
+ :type => "person",
13
+ :name => "Billy Jones",
14
+ :street_address => "801 High St.",
15
+ :postal_code => "94301",
16
+ :country => "USA",
17
+ :dob => "1842-01",
18
+ :phone_number => "+16505551234",
19
+ }
20
+ bank_account = Balanced::BankAccount.new(
21
+ :account_number => "1234567890",
22
+ :bank_code => "321174851",
23
+ :name => "Jack Q Merchant"
24
+ ).save
25
+ card = Balanced::Card.new(
26
+ :card_number => "4111111111111111",
27
+ :expiration_month => "1",
28
+ :expiration_year => "2015",
29
+ ).save
30
+ @merchant = @marketplace.create_merchant(
31
+ :email_address => "merchant@example.org",
32
+ :merchant => @merchant_attributes,
33
+ :bank_account_uri => bank_account.uri,
34
+ :name => "Jack Q Merchant"
35
+ )
36
+ @buyer = @marketplace.create_buyer(
37
+ :email_address => "buyer+transactions@example.org",
38
+ :card_uri => card.uri,
39
+ :name => "Jack Q Buyer"
40
+ ).save
41
+ 1.upto 5 do |n|
42
+ @buyer.debit(1000, :description => "Transaction ##{n}")
43
+ @merchant.credit(500, :description => "Credit from Debit ##{n}")
44
+ end
45
+ end
46
+
47
+ describe "Transaction" do
48
+ use_vcr_cassette
49
+
50
+ it "#all" do
51
+ Balanced::Transaction.all.length.should eql(15)
52
+ end
53
+
54
+ describe "#paginate" do
55
+ use_vcr_cassette
56
+
57
+ it "#total" do
58
+ Balanced::Transaction.paginate.total.should eql(15)
59
+ end
60
+
61
+ it "#each" do
62
+ counter = 0
63
+ Balanced::Transaction.paginate.each do |transaction|
64
+ counter += 1
65
+ end
66
+ counter.should eql(15)
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+ require "faraday"
3
+
4
+ describe Faraday::Response::RaiseBalancedError, :type => :response do
5
+ context "when used" do
6
+ let(:raise_server_error) { described_class.new }
7
+ end
8
+
9
+ context "integration test" do
10
+
11
+ before(:each) do
12
+ Faraday.register_middleware :response,
13
+ :handle_balanced_errors => lambda { described_class }
14
+ VCR.turn_off!
15
+ end
16
+
17
+ after(:each) do
18
+ VCR.turn_on!
19
+ end
20
+
21
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
22
+ let(:connection) do
23
+ Faraday::Connection.new do |builder|
24
+ builder.response :handle_balanced_errors
25
+ builder.response :json
26
+ builder.adapter :test, stubs
27
+ end
28
+ end
29
+
30
+ it "should raise exceptions on bad request errors" do
31
+ stubs.get("/error") {
32
+ [400, {}, JSON.dump({
33
+ :status =>"Bad Request",
34
+ :status_code =>400,
35
+ :description => "Missing expected field expiration_year Your request "\
36
+ "id is OHM62bf258abfea11e1aaba026ba7e5e72e.",
37
+ :request_id => "OHM62bf258abfea11e1aaba026ba7e5e72e"
38
+ })]
39
+ }
40
+
41
+ lambda {
42
+ connection.get('/error')
43
+ }.should raise_error(Balanced::BadRequest)
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,77 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced do
4
+ describe "configure" do
5
+ use_vcr_cassette
6
+ before do
7
+ @api_key = Balanced::ApiKey.new.save
8
+
9
+ Balanced.configure @api_key.secret
10
+ end
11
+
12
+ describe "api key" do
13
+ describe "merchant" do
14
+ subject { @api_key.merchant }
15
+ it { should_not be_nil }
16
+ end
17
+ end
18
+
19
+ describe "#client" do
20
+ subject { Balanced.client }
21
+ it { should_not be_nil }
22
+
23
+ describe "#connection" do
24
+ subject { Balanced.client.conn }
25
+ it { should_not be_nil }
26
+ end
27
+
28
+ describe "#api_key" do
29
+ subject { Balanced.client.api_key }
30
+ it { should eql @api_key.secret }
31
+ end
32
+
33
+ describe "#url" do
34
+ subject { Balanced.client.url.to_s }
35
+ it { should eql "https://api.balancedpayments.com" }
36
+ end
37
+ end
38
+
39
+ describe "reconfigure with new api key" do
40
+ use_vcr_cassette
41
+ before do
42
+ @new_api_key = Balanced::ApiKey.new.save
43
+ Balanced.configure @new_api_key.secret
44
+ end
45
+
46
+ describe "api key" do
47
+ describe "merchant" do
48
+ use_vcr_cassette
49
+ subject { @api_key.merchant }
50
+ it { should_not be_nil }
51
+ end
52
+ end
53
+
54
+ describe "#client" do
55
+ describe "#api_key" do
56
+ subject { Balanced.client.api_key }
57
+ it { should_not eql @api_key.secret }
58
+ it { should eql @new_api_key.secret }
59
+ end
60
+ end
61
+
62
+ describe "new api key" do
63
+ describe "secret" do
64
+ subject { @new_api_key.secret }
65
+ it { should_not be_nil }
66
+
67
+ end
68
+ describe "merchant" do
69
+ use_vcr_cassette
70
+
71
+ subject { @new_api_key.merchant }
72
+ it { should_not be_nil }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,48 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://api.balancedpayments.com/v1/api_keys
6
+ body:
7
+ encoding: UTF-8
8
+ string: ! '{}'
9
+ headers:
10
+ User-Agent:
11
+ - balanced-ruby/0.3.5
12
+ Content-Type:
13
+ - application/json
14
+ response:
15
+ status:
16
+ code: 201
17
+ message: !!null
18
+ headers:
19
+ content-type:
20
+ - application/json
21
+ date:
22
+ - Fri, 06 Jul 2012 04:22:45 GMT
23
+ server:
24
+ - nginx/1.0.14
25
+ x-balanced-guru:
26
+ - OHM3c57a2b6c72211e1a29c026ba7e5e72e
27
+ x-balanced-version:
28
+ - ''
29
+ content-length:
30
+ - '854'
31
+ connection:
32
+ - Close
33
+ body:
34
+ encoding: US-ASCII
35
+ string: ! "{\n \"merchant\": {\n \"phone_number\": \"+16505551212\", \n
36
+ \ \"city\": \"Nowhere\", \n \"marketplace\": null, \n \"name\": \"William
37
+ Henry Cavendish III\", \n \"email_address\": \"whc@example.org\", \n \"created_at\":
38
+ \"2012-07-06T04:22:45.622860Z\", \n \"uri\": \"/v1/merchants/TEST-MR1Qdkfd3s1CdsDYpIyE1clM\",
39
+ \n \"accounts_uri\": \"/v1/merchants/TEST-MR1Qdkfd3s1CdsDYpIyE1clM/accounts\",
40
+ \n \"meta\": {}, \n \"postal_code\": \"90210\", \n \"country_code\":
41
+ \"USA\", \n \"type\": \"person\", \n \"balance\": 0, \n \"api_keys_uri\":
42
+ \"/v1/merchants/TEST-MR1Qdkfd3s1CdsDYpIyE1clM/api_keys\", \n \"id\": \"TEST-MR1Qdkfd3s1CdsDYpIyE1clM\",
43
+ \n \"street_address\": \"123 Fake St\"\n }, \n \"secret\": \"3cac2eb2c72211e1a29c026ba7e5e72e\",
44
+ \n \"meta\": {}, \n \"created_at\": \"2012-07-06T04:22:45.865186Z\", \n
45
+ \ \"uri\": \"/v1/api_keys/AK1QufqsOoGwey1DmEmOBMho\", \n \"id\": \"AK1QufqsOoGwey1DmEmOBMho\"\n}"
46
+ http_version: !!null
47
+ recorded_at: Fri, 06 Jul 2012 04:22:46 GMT
48
+ recorded_with: VCR 2.2.2