Module: Gerry::Api::Changes

Included in:
Client
Defined in:
lib/gerry/api/changes.rb

Instance Method Summary collapse

Instance Method Details

#changes(options = []) ⇒ Hash

Get changes visible to the caller.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gerry/api/changes.rb', line 10

def changes(options = [])
  endpoint = '/changes/'
  url = endpoint

  if !options.empty?
    url += '?' + map_options(options)
  end

  response = get(url)
  return response if response.empty? || !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 if response.empty? || !response.last.delete('_more_changes')
  end
end