class GdsApi::ListResponse
subsequent pages, indicated by entries in the response’s ‘Link` header.
contained under the `results` key. The response may also have previous and
This expects responses to be in a common format, with the list of results
Response class for lists of multiple items.
def has_next_page?
def has_next_page? ! page_link("next").nil? end
def has_previous_page?
def has_previous_page? ! page_link("previous").nil? end
def initialize(response, api_client, options = {})
The ListResponse is instantiated with a reference back to the API client,
def initialize(response, api_client, options = {}) super(response, options) @api_client = api_client end
def link_header
def link_header @link_header ||= LinkHeader.parse @http_response.headers[:link] end
def next_page
def next_page # This shouldn't be a performance problem, since the cache will generally # avoid us making multiple requests for the same page, but we shouldn't # allow the data to change once it's already been loaded, so long as we # retain a reference to any one page in the sequence @next_page ||= if has_next_page? @api_client.get_list! page_link("next").href else nil end end
def page_link(rel)
def page_link(rel) link_header.find_link(["rel", rel]) end
def previous_page
def previous_page # See the note in `next_page` for why this is memoised @previous_page ||= if has_previous_page? @api_client.get_list! page_link("previous").href else nil end end
def results
def results # support group_by results from the content api by checking if there is a # grouped_results key present first. if it's not, then fallback to the # results key to_ostruct.grouped_results || to_ostruct.results end
def with_subsequent_pages
point. Note that the responses are stored so subsequent pages will not be
invoke a method such as #count, this method will fetch all pages at that
fetching pages as results from the current page are exhausted. If you
Pages of results are fetched on demand. When iterating, that means
list_response.with_subsequent_pages.count
or:
end
...
list_response.with_subsequent_pages.each do |result|
Example:
or #results which only iterate over the current page.
Transparently get all results across all pages. Compare this with #each
def with_subsequent_pages Enumerator.new { |yielder| self.each do |i| yielder << i end if has_next_page? next_page.with_subsequent_pages.each do |i| yielder << i end end } end