balanced 0.3.10 → 0.3.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -57,7 +57,7 @@ spec/cassettes. To clear them and regenerate:
57
57
 
58
58
  ### Building Documentation
59
59
 
60
- Documentation is build using YARD - http://rubydoc.info/docs/yard
60
+ Documentation is built using YARD - http://rubydoc.info/docs/yard
61
61
 
62
62
  export AMAZON_ACCESS_KEY_ID='xxx'
63
63
  export AMAZON_SECRET_ACCESS_KEY='yyy'
data/examples/examples.rb CHANGED
@@ -65,9 +65,6 @@ puts "our buyer account: #{buyer.uri}"
65
65
  puts "hold some amount of funds on the buyer, lets say 15$"
66
66
  the_hold = buyer.hold(:amount => 1500)
67
67
 
68
- puts "the hold has a fee of 30c, here's the fee #{the_hold.fee}"
69
- raise "The hold's fee is incorrect" if the_hold.fee != 30
70
-
71
68
  puts "ok, no more holds! lets just capture it (for the full amount)"
72
69
  debit = the_hold.capture
73
70
 
@@ -79,9 +76,6 @@ puts "i have #{marketplace.in_escrow} in escrow!"
79
76
  puts "cool. now let me refund the full amount"
80
77
  refund = debit.refund() # the full amount!
81
78
 
82
- puts "notice how Balanced refunds you your fees? refund fees: #{refund.fee}"
83
- raise "Woah, fees are incorrect" if (refund.fee + debit.fee) != 0
84
-
85
79
  puts "ok, we have a merchant that's signing up, let's create an account for them " \
86
80
  "first, lets create their bank account."
87
81
 
@@ -14,6 +14,9 @@ module Balanced
14
14
  :port => 5000,
15
15
  :version => '1',
16
16
  :logging_level => 'WARN',
17
+ :connection_timeout => 2,
18
+ :read_timeout => 5,
19
+ :logger => nil
17
20
  }
18
21
 
19
22
  attr_reader :conn
@@ -26,13 +29,23 @@ module Balanced
26
29
  end
27
30
 
28
31
  def build_conn
29
- logger = Logger.new(STDOUT)
30
- logger.level = Logger.const_get(DEFAULTS[:logging_level].to_s)
32
+ if config[:logger]
33
+ logger = config[:logger]
34
+ else
35
+ logger = Logger.new(STDOUT)
36
+ logger.level = Logger.const_get(config[:logging_level].to_s)
37
+ end
31
38
 
32
39
  Faraday.register_middleware :response,
33
40
  :handle_balanced_errors => lambda {Faraday::Response::RaiseBalancedError}
34
41
 
35
- @conn = Faraday.new url do |cxn|
42
+ options = {
43
+ :request => {
44
+ :open_timeout => config[:connection_timeout],
45
+ :timeout => config[:read_timeout]
46
+ }
47
+ }
48
+ @conn = Faraday.new(url, options) do |cxn|
36
49
  cxn.request :json
37
50
 
38
51
  cxn.response :logger, logger
@@ -3,6 +3,7 @@ require "cgi"
3
3
  module Balanced
4
4
  class Pager
5
5
  DEFAULT_SEP = /[&;] */n
6
+ DEFAULT_LIMIT = 10
6
7
 
7
8
  include Enumerable
8
9
 
@@ -34,17 +35,29 @@ module Balanced
34
35
  load! unless @page
35
36
  @page[:limit]
36
37
  end
38
+ alias limit_value limit
37
39
 
38
40
  def offset
39
41
  load! unless @page
40
42
  @page[:offset]
41
43
  end
44
+ alias offset_value offset
42
45
 
43
46
  def items
44
47
  load! unless @page
45
48
  @page[:items]
46
49
  end
47
50
 
51
+ def current_page
52
+ (offset / limit) + 1
53
+ end
54
+
55
+ def num_pages
56
+ num = total / limit
57
+ num += 1 if total % limit > 0
58
+ num
59
+ end
60
+
48
61
  # @return [Array] Iterates through the current page of records.
49
62
  # @yield [record]
50
63
  def each
@@ -133,6 +146,7 @@ module Balanced
133
146
  parsed_uri = URI.parse(uri)
134
147
 
135
148
  params ||= {}
149
+ params = adjust_pagination_params(params)
136
150
 
137
151
  unless parsed_uri.query.nil?
138
152
  # The reason we don't use CGI::parse here is because
@@ -151,6 +165,15 @@ module Balanced
151
165
  @uri = @page[:uri]
152
166
  end
153
167
 
168
+ def adjust_pagination_params(original)
169
+ adjusted = original.dup
170
+ per = adjusted.delete(:per)
171
+ adjusted[:limit] = per unless per.nil?
172
+ page = adjusted.delete(:page)
173
+ adjusted[:offset] = (adjusted[:limit] || DEFAULT_LIMIT) * ([page, 1].max - 1) unless page.nil?
174
+ adjusted
175
+ end
176
+
154
177
  # Stolen from Mongrel, with some small modifications:
155
178
  # Parses a query string by breaking it up at the '&'
156
179
  # and ';' characters. You can also use this to parse
@@ -17,9 +17,9 @@ module Balanced
17
17
 
18
18
  # Attempts to find an existing buyer account by email
19
19
  #
20
- # @param [String] email An email of a buyer account
21
- # @return [Account] if buyer is found
22
- # @return [nil] if buyer is not found
20
+ # @param [String] email An email of an account
21
+ # @return [Account] if account is found
22
+ # @return [nil] if account is not found
23
23
  def self.find_by_email email
24
24
  self.find(:first, :email_address => email)
25
25
  end
@@ -1,3 +1,3 @@
1
1
  module Balanced
2
- VERSION = '0.3.10'
2
+ VERSION = '0.3.11'
3
3
  end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Pager do
4
+ describe "#adjust_pagination_params" do
5
+ subject { Balanced::Pager.new 'a uri'}
6
+
7
+ it "sets limit based on per" do
8
+ params = subject.send(:adjust_pagination_params, per: 5)
9
+ params[:limit].should == 5
10
+ end
11
+
12
+ it "sets offset based on page and default limit" do
13
+ params = subject.send(:adjust_pagination_params, page: 2)
14
+ params[:offset].should == 10
15
+ end
16
+
17
+ it "sets offset based on page and per" do
18
+ params = subject.send(:adjust_pagination_params, page: 2, per: 4)
19
+ params[:offset].should == 4
20
+ end
21
+
22
+ it "prefers per to limit" do
23
+ params = subject.send(:adjust_pagination_params, per: 5, limit: 10)
24
+ params[:limit].should == 5
25
+ end
26
+
27
+ it "falls back to limit" do
28
+ params = subject.send(:adjust_pagination_params, limit: 3)
29
+ params[:limit].should == 3
30
+ end
31
+
32
+ it "prefers page to offset" do
33
+ params = subject.send(:adjust_pagination_params, page: 2, offset: 0)
34
+ params[:offset].should == 10
35
+ end
36
+
37
+ it "falls back to offset" do
38
+ params = subject.send(:adjust_pagination_params, offset: 6)
39
+ params[:offset].should == 6
40
+ end
41
+ end
42
+ 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.10
4
+ version: 0.3.11
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-08-02 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -47,7 +47,8 @@ description: ! "Balanced is the payments platform for marketplaces.\n Integra
47
47
  a payments experience just like Amazon for your marketplace.\n Forget about dealing
48
48
  with banking systems, compliance, fraud, and security.\n "
49
49
  email:
50
- - mahmoud@poundpay.com
50
+ - !binary |-
51
+ bWFobW91ZEBwb3VuZHBheS5jb20=
51
52
  executables: []
52
53
  extensions: []
53
54
  extra_rdoc_files: []
@@ -94,6 +95,7 @@ files:
94
95
  - lib/balanced/response/balanced_exception_middleware.rb
95
96
  - lib/balanced/utils.rb
96
97
  - lib/balanced/version.rb
98
+ - spec/balanced/pager_spec.rb
97
99
  - spec/balanced/resources/account_spec.rb
98
100
  - spec/balanced/resources/api_key_spec.rb
99
101
  - spec/balanced/resources/hold_spec.rb
@@ -130,6 +132,7 @@ signing_key:
130
132
  specification_version: 3
131
133
  summary: Sign up on https://balancedpayments.com/
132
134
  test_files:
135
+ - spec/balanced/pager_spec.rb
133
136
  - spec/balanced/resources/account_spec.rb
134
137
  - spec/balanced/resources/api_key_spec.rb
135
138
  - spec/balanced/resources/hold_spec.rb
@@ -140,3 +143,4 @@ test_files:
140
143
  - spec/client_spec.rb
141
144
  - spec/spec_helper.rb
142
145
  - spec/utils_spec.rb
146
+ has_rdoc: