love 0.0.3 → 0.0.4

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.
Files changed (4) hide show
  1. data/Gemfile.lock +1 -1
  2. data/lib/love.rb +30 -11
  3. data/love.gemspec +1 -1
  4. metadata +3 -3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- love (0.0.3)
4
+ love (0.0.4)
5
5
  activesupport
6
6
  yajl-ruby
7
7
 
data/lib/love.rb CHANGED
@@ -5,6 +5,8 @@ require 'yajl'
5
5
 
6
6
  module Love
7
7
 
8
+ VERSION = "0.0.4"
9
+
8
10
  # Create a custom exception class.
9
11
  class Exception < StandardError; end
10
12
 
@@ -40,6 +42,14 @@ module Love
40
42
  raise Love::Exception, "This does not appear to be a Tender #{kind} URI or ID!"
41
43
  end
42
44
  end
45
+
46
+ def request_uri(base_uri, added_params = {})
47
+ base_params = base_uri.query ? CGI.parse(base_uri.query) : {}
48
+ get_params = base_params.merge(added_params || {})
49
+ base_uri.dup.tap do |uri|
50
+ uri.query = get_params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join('&')
51
+ end
52
+ end
43
53
  end
44
54
 
45
55
  class Client
@@ -119,25 +129,34 @@ module Love
119
129
  end
120
130
 
121
131
  def buffered_each(uri, list_key, options = {}, &block)
122
- query_options = {}
123
- query_options[:since] = options[:since].to_date.to_s(:db) if options[:since]
132
+ query_params = {}
133
+ query_params[:since] = options[:since].to_date.to_s(:db) if options[:since]
134
+ query_params[:page] = [options[:start_page].to_i, 1].max rescue 1
135
+
136
+ initial_result = get(request_uri(uri, query_params))
124
137
 
125
- uri.query = query_options.map { |k, v| "#{k}=#{v}" }.join('&')
126
- initial_result = get(uri)
127
- start_page = [options[:start_page].to_i, 1].max rescue 1
138
+ # Determine the amount of pages that is going to be requested.
128
139
  max_page = (initial_result['total'].to_f / initial_result['per_page'].to_f).ceil
129
140
  end_page = options[:end_page].nil? ? max_page : [options[:end_page].to_i, max_page].min
130
141
 
131
142
  # Print out some initial debugging information
132
- Love.logger.debug "Paged requests to #{uri}: #{max_page} total pages, importing #{start_page} upto #{end_page}." if Love.logger
143
+ Love.logger.debug "Paged requests to #{uri}: #{max_page} total pages, importing #{query_params[:page]} upto #{end_page}." if Love.logger
133
144
 
134
- start_page.upto(end_page) do |page|
135
- query_options[:page] = page
136
- uri.query = query_options.map { |k, v| "#{k}=#{v}" }.join('&')
137
- result = get(uri)
138
- result[list_key].each { |record| yield(record) }
145
+ # Handle first page of results
146
+ if initial_result[list_key].kind_of?(Array)
147
+ initial_result[list_key].each { |record| yield(record) }
139
148
  sleep(sleep_between_requests) if sleep_between_requests
140
149
  end
150
+
151
+ start_page = query_params[:page].to_i + 1
152
+ start_page.upto(end_page) do |page|
153
+ query_params[:page] = page
154
+ result = get(request_uri(uri, query_params))
155
+ if result[list_key].kind_of?(Array)
156
+ result[list_key].each { |record| yield(record) }
157
+ sleep(sleep_between_requests) if sleep_between_requests
158
+ end
159
+ end
141
160
  end
142
161
  end
143
162
  end
data/love.gemspec CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3
3
 
4
4
  # Do not change the version and date fields by hand. This will be done
5
5
  # automatically by the gem release script.
6
- s.version = "0.0.3"
6
+ s.version = "0.0.4"
7
7
  s.date = "2010-11-29"
8
8
 
9
9
  s.summary = "Ruby library to access the Tender REST API."
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: love
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Willem van Bergen