balanced 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,8 @@ require "cgi"
2
2
 
3
3
  module Balanced
4
4
  class Pager
5
+ DEFAULT_SEP = /[&;] */n
6
+
5
7
  include Enumerable
6
8
 
7
9
  # A pager for paginating through resource records.
@@ -21,12 +23,6 @@ module Balanced
21
23
  def first
22
24
  load! unless @page
23
25
  items.first.nil? ? nil : resource_class.construct_from_response(items.first)
24
- =begin
25
- if items.first.nil?
26
- return nil
27
- end
28
- resource_class.construct_from_response items.first
29
- =end
30
26
  end
31
27
 
32
28
  def total
@@ -55,15 +51,13 @@ module Balanced
55
51
  return enum_for :each unless block_given?
56
52
 
57
53
  load! unless @page
58
-
59
54
  loop do
60
- @page[:items].each do |record|
61
- yield resource_class.construct_from_response record
55
+ @page[:items].each do |r|
56
+ yield resource_class.construct_from_response r
62
57
  end
63
- break if @page[:next_uri].nil?
58
+ raise StopIteration if @page[:next_uri].nil?
64
59
  self.next
65
60
  end
66
-
67
61
  end
68
62
 
69
63
  # @return [nil]
@@ -138,8 +132,17 @@ module Balanced
138
132
  def load_from uri, params
139
133
  parsed_uri = URI.parse(uri)
140
134
 
135
+ params ||= {}
136
+
141
137
  unless parsed_uri.query.nil?
142
- params.merge! CGI::parse(parsed_uri.query)
138
+ # The reason we don't use CGI::parse here is because
139
+ # the balanced api currently can't handle variable[]=value.
140
+ # Faraday ends up encoding a simple query string like:
141
+ # {"limit"=>["10"], "offset"=>["0"]}
142
+ # to limit[]=10&offset[]=0 and that's cool, but
143
+ # we have to make sure balanced supports it.
144
+ query_params = parse_query(parsed_uri.query)
145
+ params.merge! query_params
143
146
  parsed_uri.query = nil
144
147
  end
145
148
 
@@ -148,5 +151,28 @@ module Balanced
148
151
  @uri = @page[:uri]
149
152
  end
150
153
 
154
+ # Stolen from Mongrel, with some small modifications:
155
+ # Parses a query string by breaking it up at the '&'
156
+ # and ';' characters. You can also use this to parse
157
+ # cookies by changing the characters used in the second
158
+ # parameter (which defaults to '&;').
159
+ def parse_query(qs, d = nil)
160
+ params = {}
161
+
162
+ (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
163
+ k, v = p.split('=', 2).map { |x| CGI::unescape(x) }
164
+ if (cur = params[k])
165
+ if cur.class == Array
166
+ params[k] << v
167
+ else
168
+ params[k] = [cur, v]
169
+ end
170
+ else
171
+ params[k] = v
172
+ end
173
+ end
174
+
175
+ params
176
+ end
151
177
  end
152
178
  end
@@ -88,11 +88,11 @@ module Balanced
88
88
  destination_uri = args[3] || options.fetch(:destination_uri) { nil }
89
89
 
90
90
  credit = Credit.new(
91
- :uri => self.credits_uri,
92
- :amount => amount,
93
- :meta => meta,
94
- :description => description,
95
- :destination_uri => destination_uri,
91
+ :uri => self.credits_uri,
92
+ :amount => amount,
93
+ :meta => meta,
94
+ :description => description,
95
+ :destination_uri => destination_uri,
96
96
  )
97
97
  credit.save
98
98
  end
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.3.5'
2
+ VERSION = '0.3.6'
3
3
  end
@@ -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
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.3.5
4
+ version: 0.3.6
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-07-05 00:00:00.000000000 Z
12
+ date: 2012-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -98,6 +98,7 @@ files:
98
98
  - spec/balanced/resources/api_key_spec.rb
99
99
  - spec/balanced/resources/hold_spec.rb
100
100
  - spec/balanced/resources/marketplace_spec.rb
101
+ - spec/balanced/resources/transactions_spec.rb
101
102
  - spec/balanced/response/balanced_exception_middleware_spec.rb
102
103
  - spec/balanced_spec.rb
103
104
  - spec/client_spec.rb
@@ -133,6 +134,7 @@ test_files:
133
134
  - spec/balanced/resources/api_key_spec.rb
134
135
  - spec/balanced/resources/hold_spec.rb
135
136
  - spec/balanced/resources/marketplace_spec.rb
137
+ - spec/balanced/resources/transactions_spec.rb
136
138
  - spec/balanced/response/balanced_exception_middleware_spec.rb
137
139
  - spec/balanced_spec.rb
138
140
  - spec/client_spec.rb