Class: Panoptes::Endpoints::JsonApiEndpoint

Inherits:
BaseEndpoint show all
Defined in:
lib/panoptes/endpoints/json_api_endpoint.rb

Instance Attribute Summary

Attributes inherited from BaseEndpoint

#auth, #params, #prefix, #url

Instance Method Summary collapse

Methods inherited from BaseEndpoint

#connection, #delete, #etag_header, #get, #handle_response, #initialize, #patch, #post, #put, #request

Constructor Details

This class inherits a constructor from Panoptes::Endpoints::BaseEndpoint

Instance Method Details

#paginate(path, query, resource: nil) {|data, last_response| ... } ⇒ Object

Get a path and perform automatic depagination

Yields:

  • (data, last_response)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/panoptes/endpoints/json_api_endpoint.rb', line 9

def paginate(path, query, resource: nil)
  resource = path.split('/').last if resource.nil?
  data = last_response = get(path, query)

  # ensure we yield the first page of data
  yield data, last_response if block_given?

  # while we have more result set pages
  while (next_path = last_response['meta'][resource]['next_href'])
    # fetch next page of data
    last_response = get(next_path, query)
    if block_given?
      # if we pass a block then yield the first page and current page responses
      yield data, last_response
    else
      # add to the data representation of all result set pages
      data[resource].concat(last_response[resource]) if data[resource].is_a?(Array)
      data['meta'][resource].merge!(last_response['meta'][resource])
      data['links'].merge!(last_response['links'])
    end
  end

  # return the data results for when we don't use a block to process response pages
  data
end