lib/gerry/client/changes.rb in gerry-0.1.1 vs lib/gerry/client/changes.rb in gerry-0.1.2
- old
+ new
@@ -1,20 +1,38 @@
+require 'cgi'
+
module Gerry
class Client
module Changes
# Get changes visible to the caller.
#
# @param [Array] options the query parameters.
# @return [Hash] the changes.
def changes(options = [])
- url = '/changes/'
+ endpoint = '/changes/'
+ url = endpoint
- if options.empty?
- return get(url)
+ if !options.empty?
+ url += '?' + map_options(options)
end
- options = map_options(options)
- get("#{url}?#{options}")
+ response = get(url)
+ return response unless response.last.delete('_more_changes')
+
+ # Get the original start parameter, if any, else start from 0.
+ query = URI.parse(url).query
+ query = query ? CGI.parse(query) : { 'S' => ['0'] }
+ start = query['S'].join.to_i
+
+ # Keep getting data until there are no more changes.
+ loop do
+ # Replace the start parameter, using the original start as an offset.
+ query['S'] = ["#{start + response.size}"]
+ url = endpoint + '?' + map_options(query)
+
+ response.concat(get(url))
+ return response unless response.last.delete('_more_changes')
+ end
end
end
end
end