Class: ApiAdaptor::ListResponse
- Defined in:
- lib/api_adaptor/list_response.rb
Overview
Response wrapper for paginated API results.
ListResponse handles paginated responses using Link headers (RFC 5988) for navigation. It expects responses to have a "results" array and provides methods to navigate through pages.
Instance Method Summary collapse
-
#each {|Hash| ... } ⇒ Object
Iterate over results on the current page only.
-
#initialize(response, api_client, options = {}) ⇒ ListResponse
constructor
Initializes a new ListResponse with API client reference for pagination.
-
#next_page ⇒ ListResponse?
Fetches the next page of results.
-
#next_page? ⇒ Boolean
Checks if there is a next page available.
-
#previous_page ⇒ ListResponse?
Fetches the previous page of results.
-
#previous_page? ⇒ Boolean
Checks if there is a previous page available.
-
#results ⇒ Array<Hash>
Returns the array of results from the current page.
-
#to_ary ⇒ Array
Convert results to array.
-
#with_subsequent_pages ⇒ Enumerator
Returns an enumerator that transparently fetches and iterates over all pages.
Methods inherited from Response
#<=>, #[], #blank?, #cache_control, #code, #dig, #expires_at, #expires_in, #headers, #parsed_content, #present?, #raw_response_body, #to_hash
Constructor Details
#initialize(response, api_client, options = {}) ⇒ ListResponse
Initializes a new ListResponse with API client reference for pagination
34 35 36 37 |
# File 'lib/api_adaptor/list_response.rb', line 34 def initialize(response, api_client, = {}) super(response, ) @api_client = api_client end |
Instance Method Details
#each {|Hash| ... } ⇒ Object
Iterate over results on the current page only
47 |
# File 'lib/api_adaptor/list_response.rb', line 47 def_delegators :results, :each, :to_ary |
#next_page ⇒ ListResponse?
Fetches the next page of results
Results are memoized to avoid refetching the same page multiple times.
68 69 70 71 72 73 74 |
# File 'lib/api_adaptor/list_response.rb', line 68 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
Checks if there is a next page available
59 60 61 |
# File 'lib/api_adaptor/list_response.rb', line 59 def next_page? !page_link("next").nil? end |
#previous_page ⇒ ListResponse?
Fetches the previous page of results
Results are memoized to avoid refetching the same page multiple times.
88 89 90 91 |
# File 'lib/api_adaptor/list_response.rb', line 88 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
Checks if there is a previous page available
79 80 81 |
# File 'lib/api_adaptor/list_response.rb', line 79 def previous_page? !page_link("previous").nil? end |
#results ⇒ Array<Hash>
Returns the array of results from the current page
52 53 54 |
# File 'lib/api_adaptor/list_response.rb', line 52 def results to_hash["results"] end |
#to_ary ⇒ Array
Convert results to array
47 |
# File 'lib/api_adaptor/list_response.rb', line 47 def_delegators :results, :each, :to_ary |
#with_subsequent_pages ⇒ Enumerator
Returns an enumerator that transparently fetches and iterates over all pages
Pages are fetched on demand as you iterate. If you call a method like #count, all pages will be fetched immediately. Results are memoized to avoid duplicate requests.
110 111 112 113 114 115 |
# File 'lib/api_adaptor/list_response.rb', line 110 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 |