balanced 0.3.11 → 0.5.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.
@@ -4,12 +4,39 @@ module Balanced
4
4
  class Marketplace
5
5
  include Balanced::Resource
6
6
 
7
+ @@marketplace_uri = nil
8
+
7
9
  # Returns an instance representing the marketplace associated with
8
10
  # the current API key.
9
11
  #
10
12
  # @return [Marketplace]
11
13
  def self.my_marketplace
12
- Balanced::Merchant.me.marketplace
14
+ marketplace = Balanced::Merchant.me.marketplace
15
+ @@marketplace_uri = marketplace.uri if marketplace
16
+ marketplace
17
+ end
18
+
19
+ # @return [String, nil] the marketplace's URI
20
+ def self.marketplace_uri
21
+ if !@@marketplace_uri and Balanced.is_configured_with_api_key?
22
+ begin
23
+ self.my_marketplace
24
+ rescue Balanced::Error
25
+ # do nothing..
26
+ end
27
+ end
28
+ @@marketplace_uri
29
+ end
30
+
31
+ def self.marketplace_exists?
32
+ !!marketplace_uri
33
+ end
34
+
35
+ # @return [Marketplace]
36
+ def save
37
+ marketplace = super
38
+ @@marketplace_uri = marketplace.uri
39
+ marketplace
13
40
  end
14
41
 
15
42
  # Returns an instance representing the marketplace associated with
@@ -20,10 +47,33 @@ module Balanced
20
47
  self.class.my_marketplace
21
48
  end
22
49
 
50
+ # Create an Account associated with this Marketplace. This account
51
+ # will have no roles associated with it.
52
+ #
53
+ # @return [Account]
54
+ def create_account options={}
55
+ email_address = options.fetch(:email_address) { nil }
56
+ name = options.fetch(:name) { nil }
57
+ meta = options.fetch(:meta) { nil }
58
+
59
+ account_attributes = {
60
+ :uri => self.accounts_uri,
61
+ :email_address => email_address,
62
+ :name => name,
63
+ :meta => meta
64
+ }
65
+
66
+ account = Account.new account_attributes
67
+ account.save
68
+ end
69
+
70
+
23
71
  # Create a buyer Account associated with this Marketplace.
24
72
  #
25
73
  # @return [Account]
26
74
  def create_buyer *args
75
+ warn_on_positional args
76
+
27
77
  options = args.last.is_a?(Hash) ? args.pop : {}
28
78
  email_address = args[0] || options.fetch(:email_address) { nil }
29
79
  card = args[1] || options.fetch(:card_uri) { options.fetch(:card) { nil} }
@@ -61,6 +111,8 @@ module Balanced
61
111
  #
62
112
  # @return [Account]
63
113
  def create_merchant *args
114
+ warn_on_positional args
115
+
64
116
  options = args.last.is_a?(Hash) ? args.pop : {}
65
117
  email_address = args[0] || options.fetch(:email_address) { nil }
66
118
  merchant = args[1] || options.fetch(:merchant) { nil }
@@ -73,7 +125,7 @@ module Balanced
73
125
  :email_address => email_address,
74
126
  :bank_account_uri => bank_account_uri,
75
127
  :name => name,
76
- :meta => meta,
128
+ :meta => meta
77
129
  }
78
130
 
79
131
  if merchant.respond_to? :keys
@@ -86,7 +138,71 @@ module Balanced
86
138
  account.save
87
139
  end
88
140
 
141
+ # Creates a BankAccount object tied to this marketplace, for use with
142
+ # accounts
143
+ #
144
+ # @return [BankAccount]
145
+ def create_bank_account *args
146
+ warn_on_positional args
147
+
148
+ options = args.last.is_a?(Hash) ? args.pop : {}
149
+ name = args[0] || options.fetch(:name) { }
150
+ account_number = args[1] || options.fetch(:account_number) { nil }
151
+ bank_code = args[2] || options.fetch(:bank_code) {
152
+ options.fetch(:routing_number) {
153
+ nil
154
+ }
155
+ }
156
+ meta = args[3] || options.fetch(:meta) { nil }
157
+
158
+ bank_account = BankAccount.new(
159
+ :uri => self.bank_accounts_uri,
160
+ :name => name,
161
+ :account_number => account_number,
162
+ :bank_code => bank_code,
163
+ :meta => meta
164
+ )
165
+
166
+ bank_account.save
167
+ end
168
+
169
+ # Creates a Card object tied to this marketplace, for use with
170
+ # accounts
171
+ #
172
+ # @return [Card]
173
+ def create_card *args
174
+ warn_on_positional args
175
+
176
+ options = args.last.is_a?(Hash) ? args.pop : {}
177
+ card_number = args[0] || options.fetch(:card_number) { nil }
178
+ expiration_month = args[1] || options.fetch(:expiration_month) { nil }
179
+ expiration_year = args[2] || options.fetch(:expiration_year) { nil }
180
+ security_code = args[3] || options.fetch(:expiration_year) { nil }
181
+ postal_code = args[4] || options.fetch(:postal_code) { nil }
182
+ name = args[5] || options.fetch(:name) { nil }
183
+ phone_number = args[6] || options.fetch(:phone_number) { nil }
184
+ street_address = args[7] || options.fetch(:street_address) { nil }
185
+ city = args[8] || options.fetch(:city) { nil }
186
+ country_code = args[9] || options.fetch(:country_code) { nil }
187
+ meta = args[10] || options.fetch(:meta) { nil }
188
+
189
+ card = Card.new(
190
+ :uri => self.cards_uri,
191
+ :card_number => card_number,
192
+ :expiration_month => expiration_month,
193
+ :expiration_year => expiration_year,
194
+ :name => name,
195
+ :security_code => security_code,
196
+ :street_address => street_address,
197
+ :city => city,
198
+ :postal_code => postal_code,
199
+ :country_code => country_code,
200
+ :phone_number => phone_number,
201
+ :meta => meta
202
+ )
203
+ card.save
204
+ end
205
+
89
206
  end
90
207
 
91
208
  end
92
-
@@ -1,4 +1,5 @@
1
1
  module Balanced
2
+ # Resource representing the marketplace merchant object.
2
3
  class Merchant
3
4
  include Balanced::Resource
4
5
 
@@ -18,4 +19,3 @@ module Balanced
18
19
 
19
20
  end
20
21
  end
21
-
@@ -27,6 +27,25 @@ module Balanced
27
27
  reload @response
28
28
  end
29
29
 
30
+ def warn_on_positional args
31
+ msg = <<-WARNING
32
+ #############################################################
33
+ # WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! #
34
+ #############################################################
35
+
36
+ Using positional arguments is **DEPRECATED**. Please use the
37
+ keyword options pattern instead. Version __0.7.0__ of the
38
+ Ruby client will not support positional arguments.
39
+
40
+ If you need help, please hop on irc.freenode.net #balanced
41
+ or contact support@balancedpayments.com
42
+ WARNING
43
+ # warn if [...] otherwise, it's ok if it's: [], [{}] or [{...}]
44
+ unless (args.size == 1 and args.last.is_a? Hash) or (args.size == 0)
45
+ warn msg
46
+ end
47
+ end
48
+
30
49
  def response
31
50
  @response
32
51
  end
@@ -83,20 +102,27 @@ module Balanced
83
102
  Utils.underscore resource_name
84
103
  end
85
104
 
105
+ # Returns the resource URI for a given class.
106
+ #
107
+ # @example A Balanced::Account resource
108
+ # Balanced::Account.uri # => "/v1/marketplaces/TEST-MP72zVdg2j9IiqRffW9lczRZ/accounts"
109
+ #
110
+ # @return [String] the uri of the instance or the class
86
111
  def uri
87
- # the uri of a particular resource depends if there's a marketplace created or not
88
- # if there's a marketplace, then all resources have their own uri from there and the top level ones
89
- # if there's not a marketplace
112
+ # the uri of a particular resource depends if there's a marketplace
113
+ # created or not. if there's a marketplace, then all resources have their
114
+ # own uri from there and the top level ones. if there's not a marketplace
115
+ #
90
116
  # if there's an api key, then the merchant is available
91
117
  # if there's no api key, the only resources exposed are purely top level
92
- if self == Balanced::Merchant or self == Balanced::Marketplace or self == Balanced::ApiKey
118
+ if self == Balanced::Merchant || self == Balanced::Marketplace || self == Balanced::ApiKey
93
119
  collection_path
94
120
  else
95
- if Balanced::Marketplace.my_marketplace.nil?
96
- raise Balanced::Error, "#{self.name} is nested under a marketplace, which is not created or configured."
97
- else
98
- Balanced::Marketplace.my_marketplace.send(collection_name + '_uri')
121
+ if !Balanced::Marketplace.marketplace_exists?
122
+ raise Balanced::StandardError, "#{self.name} is nested under a marketplace, which is not created or configured."
99
123
  end
124
+
125
+ Balanced::Marketplace.marketplace_uri + "/#{collection_name}"
100
126
  end
101
127
  end
102
128
 
@@ -134,7 +160,7 @@ module Balanced
134
160
  instance.class.instance_eval {
135
161
  define_method(name) { @attributes[name] } # Get.
136
162
  define_method("#{name}=") { |value| @attributes[name] = value } # Set.
137
- define_method("#{name}?") { !!@attributes[name].nil? } # Present.
163
+ define_method("#{name}?") { !!@attributes[name] } # Present.
138
164
  }
139
165
  instance.send("#{name}=".to_s, value)
140
166
  end
@@ -168,4 +194,3 @@ module Balanced
168
194
 
169
195
  end
170
196
  end
171
-
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.3.11'
2
+ VERSION = '0.5.1'
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Balanced::Error, '#response' do
4
+ it "sets the response in the initializer" do
5
+ response = {status: 200}
6
+ Balanced::Error.new(response).response.should == response
7
+ end
8
+ end
9
+
10
+ describe Balanced::Error, '#body' do
11
+ it 'is constructed from the response[:body]' do
12
+ response = {body: {}}
13
+ error = Balanced::Error.new(response)
14
+ error.body.should == response[:body]
15
+ end
16
+
17
+ it "defaults to an empty hash when no body is passed" do
18
+ Balanced::Error.new({}).body.should == {}
19
+ end
20
+
21
+ describe "generating methods from response keys" do
22
+ before do
23
+ response = {body: {foo: 'bar'}}
24
+ @error = Balanced::Error.new(response)
25
+ end
26
+
27
+ it 'generates a getter for each key' do
28
+ @error.foo.should == 'bar'
29
+ end
30
+
31
+ it 'generates a predicate method' do
32
+ @error.foo?.should be_true
33
+ end
34
+ end
35
+ end
36
+
37
+ describe Balanced::StandardError do
38
+ subject { Balanced::StandardError.new('ohnoe!') }
39
+
40
+ its(:message) { should == 'ohnoe!' }
41
+ its(:error_message) { should == 'ohnoe!' }
42
+ end
@@ -12,8 +12,11 @@ describe Balanced::Account do
12
12
  use_vcr_cassette
13
13
 
14
14
  context "when ApiKey is not configured" do
15
+
15
16
  use_vcr_cassette
17
+
16
18
  before do
19
+ Balanced::Marketplace.stub(:marketplace_uri) { nil }
17
20
  Balanced.configure nil
18
21
  end
19
22
 
@@ -53,69 +56,73 @@ describe Balanced::Account do
53
56
  :dob => "1842-01",
54
57
  :phone_number => "+16505551234",
55
58
  }
59
+
60
+ @card = Balanced::Card.new(
61
+ :card_number => "4111111111111111",
62
+ :expiration_month => "12",
63
+ :expiration_year => "2015",
64
+ ).save
65
+
66
+ @buyer = Balanced::Account.new(
67
+ :uri => @marketplace.accounts_uri,
68
+ :email_address => "buyer7@example.org",
69
+ :card_uri => @card.uri,
70
+ :name => "Jack Q Buyer"
71
+ ).save
72
+
73
+ @bank_account = @marketplace.create_bank_account(
74
+ :account_number => "1234567890",
75
+ :bank_code => "321174851",
76
+ :name => "Jack Q Merchant"
77
+ )
78
+
79
+ @merchant = Balanced::Account.new(
80
+ :uri => @marketplace.accounts_uri,
81
+ :email_address => "merchant@example.org",
82
+ :merchant => @merchant_attributes,
83
+ :bank_account_uri => @bank_account.uri,
84
+ :name => "Jack Q Merchant"
85
+ ).save
86
+
87
+
56
88
  end
57
89
 
58
- describe "new" do
90
+ describe "#credit" do
59
91
  use_vcr_cassette
60
92
  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
93
+ @buyer.debit :amount => 1250
77
94
  end
78
- end
79
95
 
80
- describe "#save" do
96
+ # WARNING: This test is deprecated
97
+ context "all args passed directly" do
98
+ subject {
99
+ @merchant.credit 1250, "description", {}, @bank_account.uri
100
+ }
81
101
 
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"
102
+ its(:amount) { should == 1250 }
103
+ its(:meta) { should == {} }
104
+ its(:description) { should == "description" }
105
+ end
106
+
107
+ context "args passed by name via options hash" do
108
+ subject {
109
+ @merchant.credit(
110
+ amount: 1250,
111
+ description: "description",
112
+ meta: {},
113
+ destination_uri: @bank_account.uri
96
114
  )
97
- end
98
- it { -> { @merchant.save }.should_not raise_error }
115
+ }
116
+
117
+ its(:amount) { should == 1250 }
118
+ its(:meta) { should == {} }
119
+ its(:description) { should == "description" }
99
120
  end
121
+ end
100
122
 
123
+ describe "#save" do
101
124
  describe "after #save" do
102
125
  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
126
  describe "#id" do
120
127
  subject { @merchant.id }
121
128
  it { should_not be_nil }
@@ -127,7 +134,7 @@ describe Balanced::Account do
127
134
  end
128
135
  describe "#email_address" do
129
136
  subject { @merchant.email_address }
130
- it { should eql "merchant2@example.org" }
137
+ it { should eql "merchant@example.org" }
131
138
  end
132
139
  describe "#name" do
133
140
  subject { @merchant.name }
@@ -175,55 +182,22 @@ describe Balanced::Account do
175
182
  end
176
183
 
177
184
  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
185
+ use_vcr_cassette
186
+ before do
187
+ @new_bank_account = @marketplace.create_bank_account(
188
+ :account_number => "1234567890",
189
+ :bank_code => "321174851",
190
+ :name => "Jack Q Merchant"
191
+ )
192
+ end
195
193
 
196
- @new_bank_account = Balanced::BankAccount.new(
197
- :account_number => "53434533",
198
- :bank_code => "12345678",
199
- :name => "Jack Q Merchant"
200
- ).save
201
- end
194
+ describe "when executing" do
202
195
  it { -> { @merchant.add_bank_account(@new_bank_account.uri) }.should_not raise_error }
203
196
  end
204
197
 
205
198
  describe "after executing" do
206
199
  use_vcr_cassette
207
200
  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
201
  @merchant.add_bank_account(@new_bank_account.uri)
228
202
  @bank_accounts = Balanced::BankAccount.find(@merchant.bank_accounts_uri).items
229
203
  end
@@ -231,8 +205,6 @@ describe Balanced::Account do
231
205
  subject { @bank_accounts.size }
232
206
  it { should eql 2 }
233
207
  end
234
-
235
-
236
208
  end
237
209
  end
238
210
 
@@ -483,12 +455,13 @@ describe Balanced::Account do
483
455
  it "takes optional parameters" do
484
456
  debit = @buyer.debit(
485
457
  :amount => 500,
486
- :appears_on_statement_as => "BOBS BURGERS",
458
+ :appears_on_statement_as => "BOBS BURGERS"
487
459
  )
488
460
  debit.should be_instance_of Balanced::Debit
489
461
  debit.amount.should eql 500
490
462
  debit.appears_on_statement_as.should eql "BOBS BURGERS"
491
463
  end
464
+ # this is deprecated
492
465
  it "takes positional parameters" do
493
466
  debit = @buyer.debit(500, "FOO FIGHTER")
494
467
  debit.should be_instance_of Balanced::Debit
@@ -512,8 +485,8 @@ describe Balanced::Account do
512
485
  :expiration_year => "2015",
513
486
  ).save
514
487
  Balanced::Marketplace.my_marketplace.create_buyer(
515
- "john.doe@example.com",
516
- card.uri
488
+ :email_address => "john.doe@example.com",
489
+ :card_uri => card.uri,
517
490
  )
518
491
  end
519
492
 
@@ -548,9 +521,9 @@ describe Balanced::Account do
548
521
  :expiration_month => "12",
549
522
  :expiration_year => "2015",
550
523
  ).save
551
- buyer = Balanced::Marketplace.my_marketplace.create_buyer(
552
- "john.doe@example.com",
553
- card.uri
524
+ @buyer = Balanced::Marketplace.my_marketplace.create_buyer(
525
+ :email_address => "john.doe@example.com",
526
+ :card_uri => card.uri,
554
527
  )
555
528
  end
556
529