Class: ApiAdaptor::ListResponse
- Defined in:
- lib/api_adaptor/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
-
#initialize(response, api_client, options = {}) ⇒ ListResponse
constructor
The ListResponse is instantiated with a reference back to the API client, so it can make requests for the subsequent pages.
- #next_page ⇒ Object
- #next_page? ⇒ Boolean
- #previous_page ⇒ Object
- #previous_page? ⇒ Boolean
- #results ⇒ Object
-
#with_subsequent_pages ⇒ Object
Transparently get all results across all pages.
Methods inherited from Response
#blank?, #cache_control, #code, #expires_at, #expires_in, #headers, #parsed_content, #present?, #raw_response_body, #to_hash
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/api_adaptor/list_response.rb', line 16 def initialize(response, api_client, = {}) super(response, ) @api_client = api_client end |
Instance Method Details
#next_page ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/api_adaptor/list_response.rb', line 33 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 ||= (@api_client.get_list page_link("next").href if next_page?) end |
#next_page? ⇒ Boolean
29 30 31 |
# File 'lib/api_adaptor/list_response.rb', line 29 def next_page? !page_link("next").nil? end |
#previous_page ⇒ Object
45 46 47 48 |
# File 'lib/api_adaptor/list_response.rb', line 45 def previous_page # See the note in `next_page` for why this is memoised @previous_page ||= (@api_client.get_list(page_link("previous").href) if previous_page?) end |
#previous_page? ⇒ Boolean
41 42 43 |
# File 'lib/api_adaptor/list_response.rb', line 41 def previous_page? !page_link("previous").nil? end |
#results ⇒ Object
25 26 27 |
# File 'lib/api_adaptor/list_response.rb', line 25 def results to_hash["results"] end |
#with_subsequent_pages ⇒ Object
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.
68 69 70 71 72 73 |
# File 'lib/api_adaptor/list_response.rb', line 68 def with_subsequent_pages Enumerator.new do |yielder| each { |i| yielder << i } next_page.with_subsequent_pages.each { |i| yielder << i } if next_page? end end |