balanced 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/examples/examples.rb CHANGED
@@ -1,7 +1,9 @@
1
+ $:.unshift("/Users/mahmoud/code/poundpay/ruby/balanced-ruby/lib")
2
+ require 'balanced'
1
3
 
2
4
  # create our new api key
3
5
  api_key = Balanced::ApiKey.new.save
4
- puts "Our secret is: ", api_key
6
+ puts "Our secret is: ", api_key.secret
5
7
 
6
8
  # configure with our secret
7
9
  Balanced.configure(api_key.secret)
@@ -9,6 +11,14 @@ Balanced.configure(api_key.secret)
9
11
  # create our marketplace
10
12
  marketplace = Balanced::Marketplace.new.save
11
13
 
14
+ # what's my merchant?
15
+ raise "Merchant.me should not be nil" if Balanced::Merchant.me.nil?
16
+
17
+ # what's my marketplace?
18
+ raise "Marketplace.my_marketplace should not be nil" if Balanced::Marketplace.my_marketplace.nil?
19
+
20
+ # cool! let's create a new card.
21
+
12
22
  card = Balanced::Card.new(
13
23
  :card_number => "5105105105105100",
14
24
  :expiration_month => "12",
@@ -16,32 +26,59 @@ card = Balanced::Card.new(
16
26
  ).save
17
27
 
18
28
  # create our account
19
- buyer = marketplace.create_buyer(
20
- :email_address => "buyer@example.org",
21
- :card_uri => card.uri
22
- )
29
+ buyer = marketplace.create_buyer("buyer@example.org", card.uri)
23
30
 
24
31
  # hold some amount of funds on the buyer, lets say 15$
25
- the_hold = buyer.hold(
26
- :amount => 1500,
27
- )
32
+ the_hold = buyer.hold(1500)
28
33
 
29
34
  # the hold has a fee of 35c
30
- assert the_hold.fee == 35
35
+ raise "The hold's fee is incorrect" if the_hold.fee != 35
31
36
 
32
- # nah, lets just debit it
33
- debit = the_hold.debit()
37
+ # nah, lets just capture it
38
+ debit = the_hold.capture()
34
39
 
35
40
  # hmm, how much money do i have in escrow? should equal
36
41
  # the debit amount
37
- marketplace = marketplace.find(marketplace.uri)
38
- assert marketplace.in_escrow == 1500
42
+ marketplace = marketplace.my_marketplace
43
+ raise "1500 is not in escrow! this is wrong" if marketplace.in_escrow != 1500
39
44
 
40
45
 
41
46
  # cool. now let me refund
42
47
  refund = debit.refund() # the full amount!
43
48
 
44
49
  # notice how Balanced refunds you your fees?
45
- assert (refund.fee + debit.fee) == 0
50
+ raise "Woah, fees are incorrect" if (refund.fee + debit.fee) != 0
51
+
52
+ # ok, we have a merchant that's signing up, let's create an account for them
53
+ # first, lets create their bank account
54
+ bank_account = Balanced::BankAccount.new(
55
+ :account_number => "1234567890",
56
+ :bank_code => "12",
57
+ :name => "Jack Q Merchant",
58
+ ).save
59
+
60
+ merchant = marketplace.create_merchant(
61
+ "merchant@example.org",
62
+ {
63
+ :type => "person",
64
+ :name => "Billy Jones",
65
+ :street_address => "801 High St.",
66
+ :postal_code => "94301",
67
+ :country => "USA",
68
+ :dob => "1842-01",
69
+ :phone_number => "+16505551234",
70
+ },
71
+ bank_account.uri,
72
+ "Jack Q Merchant",
73
+ )
74
+
75
+ # oh our buyer is interested in buying something for 130.00$
76
+ another_debit = buyer.debit(13000, "MARKETPLACE.COM")
77
+
78
+ # lets credit our merchant 110.00$
79
+ credit = merchant.credit(11000, "Buyer purchased something on MARKETPLACE.COM")
46
80
 
81
+ # our fee is 15% so, we earned ~20
82
+ mp_credit = marketplace.owner_account.credit(2000, "Our commission from MARKETPLACE.COM")
47
83
 
84
+ # and there you have it :)
data/lib/balanced.rb CHANGED
@@ -69,7 +69,7 @@ module Balanced
69
69
  begin
70
70
  Balanced.const_get class_name
71
71
  rescue NameError
72
- false
72
+ return false
73
73
  end
74
74
  true
75
75
  end
data/lib/balanced/base.rb CHANGED
@@ -19,6 +19,42 @@ module Balanced
19
19
  Utils.underscore resource_name
20
20
  end
21
21
 
22
+ def construct_from_response payload
23
+ payload = Balanced::Utils.hash_with_indifferent_read_access payload
24
+ klass = Balanced.from_uri(payload[:uri])
25
+ instance = klass.new payload
26
+ payload.each do |name, value|
27
+ klass.class_eval {
28
+ attr_accessor name.to_s
29
+ }
30
+ # here is where our interpretations will begin.
31
+ # if the value is a sub-resource, lets instantiate the class
32
+ # and set it correctly
33
+ if value.instance_of? Hash and value.has_key? 'uri'
34
+ value = construct_from_response value
35
+ elsif name =~ /_uri$/
36
+ modified_name = name.sub(/_uri$/, '')
37
+ klass.instance_eval {
38
+ define_method(modified_name) {
39
+ values_class = Balanced.from_uri(value)
40
+ # if uri is a collection -> this would definitely be if it ends in a symbol
41
+ # then we should allow a lazy executor of the query pager
42
+ if Balanced.is_collection(value)
43
+ # TODO: return the pager
44
+ p "TODO: return the pager for this class: #{values_class}"
45
+ values_class.new
46
+ else
47
+ values_class.find(value)
48
+ end
49
+ }
50
+ }
51
+ end
52
+
53
+ instance.instance_variable_set "@#{name}", value
54
+ end
55
+ instance
56
+ end
57
+
22
58
  end
23
59
 
24
60
  attr_reader :attributes
@@ -46,7 +82,7 @@ module Balanced
46
82
 
47
83
  # delegate the query to the pager module
48
84
 
49
- def find (uri, options={})
85
+ def find uri, options={}
50
86
  payload = Balanced.get :uri => uri
51
87
  construct_from_response payload
52
88
  end
@@ -56,7 +92,7 @@ module Balanced
56
92
  method = :post
57
93
  if uri.nil?
58
94
  uri = self.class.collection_path
59
- else
95
+ elsif !Balanced.is_collection(uri)
60
96
  method = :put
61
97
  end
62
98
  response = Balanced.send(method, uri, self.attributes)
@@ -67,45 +103,10 @@ module Balanced
67
103
  Balanced.delete :uri => self.attributes['uri']
68
104
  end
69
105
 
70
- def construct_from_response payload
71
- klass = Balanced.from_uri(payload['uri'])
72
- instance = klass.new payload
73
- payload.each do |name, value|
74
- klass.class_eval {
75
- attr_accessor name.to_s
76
- }
77
- # here is where our interpretations will begin.
78
- # if the value is a sub-resource, lets instantiate the class
79
- # and set it correctly
80
- if value.instance_of? Hash and value.has_key? 'uri'
81
- value = construct_from_response value
82
- elsif name =~ /_uri$/
83
- modified_name = name.sub(/_uri$/, '')
84
- klass.instance_eval {
85
- define_method(modified_name) {
86
- values_class = Balanced.from_uri(value)
87
- # if uri is a collection -> this would definitely be if it ends in a symbol
88
- # then we should allow a lazy executor of the query pager
89
- if Balanced.is_collection(value)
90
- # TODO: return the pager
91
- p "TODO: return the pager for this class: #{values_class}"
92
- values_class.new
93
- else
94
- values_class.find(value)
95
- end
96
- }
97
- }
98
- end
99
-
100
- instance.instance_variable_set "@#{name}", value
101
- end
102
- instance
103
- end
104
-
105
106
  def reload response = nil
106
107
  if response
107
108
  return if response.body.to_s.length.zero?
108
- fresh = self.construct_from_response response.body
109
+ fresh = self.class.construct_from_response response.body
109
110
  else
110
111
  fresh = self.find(@attributes['uri'])
111
112
  end
@@ -45,11 +45,13 @@ module Balanced
45
45
  end
46
46
 
47
47
  def url
48
- URI::HTTP.build(
49
- :scheme => @config[:scheme],
48
+ _url = URI::HTTP.build(
50
49
  :host => @config[:host],
51
50
  :port => @config[:port],
52
- ).to_s
51
+ )
52
+ # wow. yes, this is what you actually have to do.
53
+ _url.scheme = @config[:scheme]
54
+ _url
53
55
  end
54
56
 
55
57
  # wtf..
@@ -5,10 +5,19 @@ module Balanced
5
5
 
6
6
  class Account < Resource
7
7
 
8
+
9
+ def initialize attributes = {}
10
+ Balanced::Utils.stringify_keys! attributes
11
+ unless attributes.has_key? 'uri'
12
+ attributes['uri'] = Balanced::Marketplace.my_marketplace.send(self.class.collection_name + '_uri')
13
+ end
14
+ super attributes
15
+ end
16
+
8
17
  def debit (amount=nil,
9
18
  appears_on_statement_as=nil,
10
19
  hold_uri=nil,
11
- meta=nil,
20
+ meta={},
12
21
  description=nil,
13
22
  source_uri=nil)
14
23
  debit = Debit.new(
@@ -64,7 +73,15 @@ module Balanced
64
73
  class Merchant < Resource
65
74
 
66
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
67
82
 
83
+ def me
84
+ self.class.me
68
85
  end
69
86
 
70
87
  end
@@ -72,6 +89,15 @@ module Balanced
72
89
  class Marketplace < Resource
73
90
 
74
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
75
101
  end
76
102
 
77
103
  def create_buyer email_address, card_uri, name=nil, meta={}
@@ -101,12 +127,21 @@ module Balanced
101
127
 
102
128
  class Hold < Resource
103
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
+
104
138
  def void
105
139
  @is_void = true
106
140
  save
107
141
  end
108
142
 
109
- def capture amount, appears_on_statement_as, meta, description
143
+ def capture amount=nil, appears_on_statement_as=nil, meta={}, description=nil
144
+ amount ||= self.amount
110
145
  self.account.debit(amount, appears_on_statement_as, self.uri, meta, description)
111
146
  end
112
147
 
@@ -114,6 +149,14 @@ module Balanced
114
149
 
115
150
  class Debit < Resource
116
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
+
117
160
  def refund amount=nil, description=nil
118
161
  refund = Refund.new(
119
162
  :uri => self.refunds_uri,
@@ -127,17 +170,48 @@ module Balanced
127
170
  end
128
171
 
129
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
+
130
181
  end
131
182
 
132
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
+
133
192
  end
134
193
 
135
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
+
136
203
  end
137
204
 
138
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
139
213
 
140
- def debit amount=nil, appears_on_statement_as=nil, holds_uri=nil, meta=nil, description=nil
214
+ def debit amount=nil, appears_on_statement_as=nil, holds_uri=nil, meta={}, description=nil
141
215
  self.account.debit(amount, appears_on_statement_as, holds_uri, meta, description, self.uri)
142
216
  end
143
217
 
@@ -147,12 +221,19 @@ module Balanced
147
221
  end
148
222
 
149
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
150
231
 
151
- def debit amount, appears_on_statement_as=nil, meta=mil, description=nil
232
+ def debit amount, appears_on_statement_as=nil, meta={}, description=nil
152
233
  self.account.debit(amount, appears_on_statement_as, meta, description, self.uri)
153
234
  end
154
235
 
155
- def credit amount, description=nil, meta=nil
236
+ def credit amount, description=nil, meta={}
156
237
  self.account.credit(amount, description, meta, self.uri)
157
238
  end
158
239
  end
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: balanced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-19 00:00:00.000000000 Z
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday