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.
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  *.rbc
3
+ *~
3
4
  .bundle
4
5
  .config
5
6
  coverage
@@ -23,3 +24,6 @@ werk
23
24
 
24
25
  # lib
25
26
  Gemfile.lock
27
+
28
+ # vcr cassettes
29
+ spec/cassettes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,4 @@
1
+ Mahmoud Abdelkader
2
+ Marshall Jones
3
+ Alex Notov
4
+ Josh Deeden
data/Gemfile CHANGED
@@ -1,4 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+
3
4
  # Specify your gem's dependencies in balanced.gemspec
4
5
  gemspec
6
+
7
+ gem 'rspec'
@@ -0,0 +1,7 @@
1
+ ignore %r{^doc}
2
+ guard 'rspec', :version => 2 do
3
+ watch(%r{^spec/.+_spec\.rb$})
4
+ watch(%r{^lib/balanced/(.+)\.rb$}) { |m| "spec/balanced/#{m[1]}_spec.rb" }
5
+ watch(%r{^lib/balanced/resources/(.+)\.rb$}) { |m| "spec/balanced/resources/#{m[1]}_spec.rb" }
6
+ watch('spec/spec_helper.rb') { "spec" }
7
+ end
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Balanced
2
2
 
3
- TODO: Write a gem description
3
+ Online Marketplace Payments
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,8 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ See https://www.balancedpayments.com/docs/ruby for tutorials and documentation.
22
+
22
23
 
23
24
  ## Contributing
24
25
 
@@ -27,3 +28,31 @@ TODO: Write usage instructions here
27
28
  3. Commit your changes (`git commit -am 'Add some feature'`)
28
29
  4. Push to the branch (`git push origin my-new-feature`)
29
30
  5. Create new Pull Request
31
+
32
+
33
+ ### Specs
34
+
35
+ The spec suite is a work in progress. Existing specs can either be run
36
+ using [guard](https://github.com/guard/guard) or rake.
37
+
38
+ Guard
39
+
40
+ $ bundle exec guard
41
+
42
+ Rake
43
+
44
+ $ rake spec
45
+
46
+ We use [VCR](https://www.relishapp.com/myronmarston/vcr/docs) to stub
47
+ out and save http interactions. Cassettes are not stored in the repo.
48
+ They are generated the first time the spec suite is run and stored in
49
+ spec/cassettes. To clear them and regenerate:
50
+
51
+ $ rm -rf spec/cassettes
52
+
53
+
54
+ ### Building Documentation
55
+
56
+ Documentation is build using YARD - http://rubydoc.info/docs/yard
57
+
58
+ yard doc -e doc/balanced_plugin.rb -p doc/balanced_templates/ 'lib/**/resources.rb' --one-file
@@ -14,7 +14,10 @@ Gem::Specification.new do |gem|
14
14
  gem.add_dependency("faraday", '~> 0.8')
15
15
  gem.add_dependency("faraday_middleware", '~> 0.8.7')
16
16
 
17
- gem.add_development_dependency("rspec", '~> 2.0')
17
+ gem.add_development_dependency("rspec", '~> 2.10')
18
+ gem.add_development_dependency("vcr", '~> 2.2.0')
19
+ gem.add_development_dependency("guard", '~> 1.1.1')
20
+ gem.add_development_dependency("guard-rspec", '~> 1.0.0')
18
21
 
19
22
  gem.files = `git ls-files`.split($\)
20
23
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -6,6 +6,16 @@ rescue NameError
6
6
  raise "wtf"
7
7
  end
8
8
 
9
+ host = ENV['BALANCED_HOST'] or nil
10
+ options = {}
11
+ if host then
12
+ options[:scheme] = 'http'
13
+ options[:host] = host
14
+ options[:port] = 5000
15
+ end
16
+
17
+ Balanced.configure(nil, options)
18
+
9
19
  puts "create our new api key"
10
20
  api_key = Balanced::ApiKey.new.save
11
21
  puts "Our secret is: ", api_key.secret
@@ -99,6 +109,12 @@ puts "ok lets invalid a card"
99
109
  card['is_valid'] = false
100
110
  card.save
101
111
 
102
- raise "This card is NOT VALID" if card.is_valid
112
+ raise "This card is INCORRECTLY VALID" if card.is_valid
113
+
114
+ puts "invalidating a bank account"
115
+ bank_account['is_valid'] = false
116
+ bank_account.save
117
+
118
+ raise "This card is INCORRECTLY VALID" if bank_account.is_valid
103
119
 
104
- puts "and there you have it :)"
120
+ puts "and there you have it :)"
@@ -1,8 +1,9 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "balanced", "resources")
2
+
1
3
  require 'uri'
2
4
  require 'balanced/version' unless defined? Balanced::VERSION
3
5
  require 'balanced/client'
4
6
  require 'balanced/utils'
5
- require 'balanced/base'
6
7
  require 'balanced/resources'
7
8
 
8
9
  module Balanced
@@ -17,34 +18,15 @@ module Balanced
17
18
 
18
19
  class << self
19
20
 
20
- attr_accessor 'client'
21
- attr_accessor 'config'
21
+ attr_accessor :client
22
+ attr_accessor :config
22
23
 
23
24
  def configure(api_key=nil, options={})
24
- options = @config.merge! options
25
- @config = options
26
- @client = Balanced::Client.new(api_key, @config)
27
- end
28
-
29
- def get uri, params = {}
30
- self.client.get uri, params
31
- end
32
-
33
- def post uri, data = {}
34
- self.client.post uri, data
35
- end
36
-
37
- def put uri, data = {}
38
- self.client.put uri, data
39
- end
40
-
41
- def delete uri
42
- self.client.delete uri
25
+ @client = Balanced::Client.new(api_key, @config.merge(options))
43
26
  end
44
27
 
45
28
  def split_the_uri uri
46
- parsed_uri = URI.parse(uri)
47
- parsed_uri.path.sub(/\/$/, '').split('/')
29
+ URI.parse(uri).path.sub(/\/$/, '').split('/')
48
30
  end
49
31
 
50
32
  def from_uri uri
@@ -72,9 +54,18 @@ module Balanced
72
54
  end
73
55
  true
74
56
  end
57
+
58
+ def method_missing(method, *args, &block)
59
+ case method
60
+ when :get, :post, :put, :delete
61
+ self.client.send method, *args
62
+ else
63
+ super
64
+ end
65
+ end
75
66
  end
76
67
 
77
68
  # configure on import so we don't have to configure for creating
78
69
  # an api key
79
70
  configure
80
- end
71
+ end
@@ -14,17 +14,15 @@ module Balanced
14
14
  :logging_level => 'WARN',
15
15
  }
16
16
 
17
- attr :api_key, true
18
17
  attr_reader :conn
19
- attr_accessor :config
18
+ attr_accessor :api_key, :config
20
19
 
21
20
  def initialize(api_key, options={})
22
21
  @api_key = api_key.nil? ? api_key : api_key.strip
23
- @config = DEFAULTS.merge! options
22
+ @config = DEFAULTS.merge options
24
23
  build_conn
25
24
  end
26
25
 
27
-
28
26
  def build_conn
29
27
  logger = Logger.new(STDOUT)
30
28
  logger.level = Logger.const_get(DEFAULTS[:logging_level].to_s)
@@ -37,50 +35,29 @@ module Balanced
37
35
  cxn.response :raise_error # raise exceptions on 40x, 50x responses
38
36
  cxn.adapter Faraday.default_adapter
39
37
  end
40
- @conn.path_prefix = '/'
41
- @conn.headers['User-Agent'] = "balanced-ruby/#{Balanced::VERSION}"
38
+ conn.path_prefix = '/'
39
+ conn.headers['User-Agent'] = "balanced-ruby/#{Balanced::VERSION}"
42
40
  end
43
41
 
44
- def inspect # :nodoc:
45
- "<Balanced::Client @api_key=#@api_key, @url=#{url}>"
46
- end
42
+ #def inspect # :nodoc:
43
+ #"<Balanced::Client @api_key=#{api_key}, @url=#{url}>"
44
+ #end
47
45
 
48
46
  def url
49
- _url = URI::HTTP.build(
50
- :host => @config[:host],
51
- :port => @config[:port],
52
- )
53
- # wow. yes, this is what you actually have to do.
54
- _url.scheme = @config[:scheme]
55
- _url
56
- end
57
-
58
- # wtf..
59
- def get *args
60
- op(:get, *args)
61
- end
62
-
63
- def post *args
64
- op(:post, *args)
65
- end
66
-
67
- def put *args
68
- op(:put, *args)
69
- end
70
-
71
- def delete *args
72
- op(:delete, *args)
47
+ URI::HTTPS.build :host => config[:host], :port => config[:port]
73
48
  end
74
49
 
75
50
  private
76
51
 
77
- def op (method, *args)
78
- unless @api_key.nil?
79
- @conn.basic_auth(@api_key, '')
52
+ def method_missing(method, *args, &block)
53
+ case method
54
+ when :get, :post, :put, :delete
55
+ conn.basic_auth(api_key, '') unless api_key.nil?
56
+ conn.send method, *args
57
+ else
58
+ super
80
59
  end
81
- @conn.send(method, *args)
82
60
  end
83
61
 
84
62
  end
85
-
86
- end
63
+ end
@@ -1,241 +1,13 @@
1
- module Balanced
2
-
3
- class ApiKey < Resource
4
- end
5
-
6
- class Account < Resource
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
-
17
- def debit (amount=nil,
18
- appears_on_statement_as=nil,
19
- hold_uri=nil,
20
- meta={},
21
- description=nil,
22
- source_uri=nil)
23
- debit = Debit.new(
24
- :uri => self.debits_uri,
25
- :amount => amount,
26
- :appears_on_statement_as => appears_on_statement_as,
27
- :hold_uri => hold_uri,
28
- :meta => meta,
29
- :description => description,
30
- :source_uri => source_uri,
31
- )
32
- debit.save
33
- end
34
-
35
- def hold amount, meta={}, source_uri=nil
36
- hold = Hold.new(
37
- :uri => self.holds_uri,
38
- :amount => amount,
39
- :meta => meta,
40
- :source_uri => source_uri,
41
- )
42
- hold.save
43
- end
44
-
45
- def credit amount, description=nil, meta={}, destination_uri=nil
46
- credit = Credit.new(
47
- :uri => self.credits_uri,
48
- :amount => amount,
49
- :meta => meta,
50
- :description => description,
51
- :destination_uri => destination_uri,
52
- )
53
- credit.save
54
- end
55
-
56
- def add_card card_uri
57
- self.card_uri = card_uri
58
- save
59
- end
60
-
61
- def add_bank_account bank_account_uri
62
- self.bank_account_uri = bank_account_uri
63
- save
64
- end
65
-
66
- def promote_to_merchant merchant_data
67
- self.merchant = merchant
68
- save
69
- end
70
-
71
- end
72
-
73
- class Merchant < Resource
74
-
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
82
-
83
- def me
84
- self.class.me
85
- end
86
-
87
- end
88
-
89
- class Marketplace < Resource
90
-
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
101
- end
102
-
103
- def create_buyer email_address, card_uri, name=nil, meta={}
104
- account = Account.new(
105
- :uri => self.accounts_uri,
106
- :email_address => email_address,
107
- :card_uri => card_uri,
108
- :name => name,
109
- :meta => meta,
110
- )
111
- account.save
112
- end
113
-
114
- def create_merchant email_address, merchant, bank_account_uri=nil, name=nil, meta={}
115
- account = Account.new(
116
- :uri => self.accounts_uri,
117
- :email_address => email_address,
118
- :merchant => merchant,
119
- :bank_account_uri => bank_account_uri,
120
- :name => name,
121
- :meta => meta,
122
- )
123
- account.save
124
- end
125
-
126
- end
127
-
128
- class Hold < Resource
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
-
138
- def void
139
- @is_void = true
140
- save
141
- end
142
-
143
- def capture amount=nil, appears_on_statement_as=nil, meta={}, description=nil
144
- amount ||= self.amount
145
- self.account.debit(amount, appears_on_statement_as, self.uri, meta, description)
146
- end
147
-
148
- end
149
-
150
- class Debit < Resource
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
-
160
- def refund amount=nil, description=nil
161
- refund = Refund.new(
162
- :uri => self.refunds_uri,
163
- :debit_uri => self.uri,
164
- :amount => amount,
165
- :description => description,
166
- )
167
- refund.save
168
- end
169
-
170
- end
171
-
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
-
181
- end
182
-
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
-
192
- end
193
-
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
-
203
- end
204
-
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
213
-
214
- def debit amount=nil, appears_on_statement_as=nil, holds_uri=nil, meta={}, description=nil
215
- self.account.debit(amount, appears_on_statement_as, holds_uri, meta, description, self.uri)
216
- end
217
-
218
- def hold amount=nil, meta=nil
219
- self.account.hold(amount, meta, self.uri)
220
- end
221
- end
222
-
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
231
-
232
- def debit amount, appears_on_statement_as=nil, meta={}, description=nil
233
- self.account.debit(amount, appears_on_statement_as, meta, description, self.uri)
234
- end
235
-
236
- def credit amount, description=nil, meta={}
237
- self.account.credit(amount, description, meta, self.uri)
238
- end
239
- end
240
-
241
- end
1
+ $:.unshift File.join(File.dirname(__FILE__), "balanced", "resources")
2
+ require "resource"
3
+ require "account"
4
+ require "api_key"
5
+ require "bank_account"
6
+ require "card"
7
+ require "credit"
8
+ require "debit"
9
+ require "hold"
10
+ require "marketplace"
11
+ require "merchant"
12
+ require "refund"
13
+ require "transaction"