Class: Pinnacle::Internal::CursorPageIterator
- Inherits:
-
Object
- Object
- Pinnacle::Internal::CursorPageIterator
- Includes:
- Enumerable
- Defined in:
- lib/pinnacle/internal/iterators/cursor_page_iterator.rb
Instance Attribute Summary collapse
-
#http_response ⇒ Net::HTTPResponse?
readonly
The raw HTTP response from the most recent page response.
Instance Method Summary collapse
-
#each(&block) ⇒ NilClass
Iterates over each page returned by the API.
-
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Pinnacle::Internal::CursorPageIterator
constructor
Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items.
-
#next? ⇒ Boolean
Whether another page will be available from the API.
-
#next_page ⇒ Object?
Retrieves the next page from the API.
Constructor Details
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Pinnacle::Internal::CursorPageIterator
Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items.
19 20 21 22 23 24 25 |
# File 'lib/pinnacle/internal/iterators/cursor_page_iterator.rb', line 19 def initialize(initial_cursor:, cursor_field:, &block) @need_initial_load = initial_cursor.nil? @cursor = initial_cursor @cursor_field = cursor_field @get_next_page = block @http_response = nil end |
Instance Attribute Details
#http_response ⇒ Net::HTTPResponse? (readonly)
The raw HTTP response from the most recent page response.
10 11 12 |
# File 'lib/pinnacle/internal/iterators/cursor_page_iterator.rb', line 10 def http_response @http_response end |
Instance Method Details
#each(&block) ⇒ NilClass
Iterates over each page returned by the API.
31 32 33 34 35 |
# File 'lib/pinnacle/internal/iterators/cursor_page_iterator.rb', line 31 def each(&block) while (page = next_page) block.call(page) end end |
#next? ⇒ Boolean
Whether another page will be available from the API.
40 41 42 |
# File 'lib/pinnacle/internal/iterators/cursor_page_iterator.rb', line 40 def next? @need_initial_load || !@cursor.nil? end |
#next_page ⇒ Object?
Retrieves the next page from the API.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/pinnacle/internal/iterators/cursor_page_iterator.rb', line 47 def next_page return if !@need_initial_load && @cursor.nil? @need_initial_load = false result = @get_next_page.call(@cursor) if result.is_a?(Array) fetched_page, raw_response = result @http_response = raw_response else fetched_page = result end @cursor = fetched_page.send(@cursor_field) fetched_page end |