Class: GdsApi::ListResponse

Inherits:
Response show all
Defined in:
lib/gds_api/list_response.rb

Overview

Response class for lists of multiple items.

This expects responses to be in a common format, with the list of results contained under the ‘results` key. The response may also have previous and subsequent pages, indicated by entries in the response’s ‘Link` header.

Instance Method Summary collapse

Methods inherited from Response

#blank?, #code, #method_missing, #present?, #raw_response_body, #respond_to_missing?, #to_hash, #to_ostruct

Constructor Details

#initialize(response, api_client, options = {}) ⇒ ListResponse

The ListResponse is instantiated with a reference back to the API client, so it can make requests for the subsequent pages



16
17
18
19
# File 'lib/gds_api/list_response.rb', line 16

def initialize(response, api_client, options = {})
  super(response, options)
  @api_client = api_client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GdsApi::Response

Instance Method Details

#has_next_page?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/gds_api/list_response.rb', line 32

def has_next_page?
  ! page_link("next").nil?
end

#has_previous_page?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gds_api/list_response.rb', line 48

def has_previous_page?
  ! page_link("previous").nil?
end

#next_pageObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gds_api/list_response.rb', line 36

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

#previous_pageObject



52
53
54
55
56
57
58
59
# File 'lib/gds_api/list_response.rb', line 52

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

#resultsObject



25
26
27
28
29
30
# File 'lib/gds_api/list_response.rb', line 25

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

#with_subsequent_pagesObject

Transparently get all results across all pages. Compare this with #each or #results which only iterate over the current page.

Example:

list_response.with_subsequent_pages.each do |result|
  ...
end

or:

list_response.with_subsequent_pages.count

Pages of results are fetched on demand. When iterating, that means fetching pages as results from the current page are exhausted. If you invoke a method such as #count, this method will fetch all pages at that point. Note that the responses are stored so subsequent pages will not be loaded multiple times.



79
80
81
82
83
84
85
86
# File 'lib/gds_api/list_response.rb', line 79

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