Class: Plaid::Internal::CursorPageIterator
- Inherits:
-
Object
- Object
- Plaid::Internal::CursorPageIterator
- Includes:
- Enumerable
- Defined in:
- lib/plaid/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, Enumerator
Iterates over each page returned by the API, starting again from the first page on every call.
-
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Plaid::Internal::CursorPageIterator
constructor
Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items.
-
#load_first_page ⇒ self
Sends the request for the first page now instead of on the first iteration, so an API error for that page is raised here.
-
#next? ⇒ Boolean
Whether another page will be available from the API.
-
#next_page ⇒ Object?
Retrieves the next page from the API.
-
#reuse_first_page(other) ⇒ NilClass
Uses the first page that
otherloaded withload_first_page, unless this iterator already sent a request. -
#rewind ⇒ NilClass
Resets page-by-page iteration (
next_page/next?) to the first page.
Constructor Details
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Plaid::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 26 27 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 19 def initialize(initial_cursor:, cursor_field:, &block) @initial_cursor = initial_cursor @cursor_field = cursor_field @get_next_page = block @http_response = nil @first_page_requested = false @first_page = nil rewind end |
Instance Attribute Details
#http_response ⇒ Net::HTTPResponse? (readonly)
The raw HTTP response from the most recent page response.
10 11 12 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 10 def http_response @http_response end |
Instance Method Details
#each(&block) ⇒ NilClass, Enumerator
Iterates over each page returned by the API, starting again from the first page on every call.
57 58 59 60 61 62 63 64 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 57 def each(&block) return enum_for(:each) unless block_given? rewind while (page = next_page) block.call(page) end end |
#load_first_page ⇒ self
Sends the request for the first page now instead of on the first iteration, so an API error for that page is raised here. The page is kept, and every loop that starts from the first page reuses it instead of requesting it again. Does nothing if the first page was already requested.
34 35 36 37 38 39 40 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 34 def load_first_page return self if @first_page_requested @first_page = fetch_page(@initial_cursor) rewind self end |
#next? ⇒ Boolean
Whether another page will be available from the API.
79 80 81 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 79 def next? (@at_first_page && !@first_page.nil?) || @need_initial_load || !@cursor.nil? end |
#next_page ⇒ Object?
Retrieves the next page from the API.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 86 def next_page if @at_first_page && !@first_page.nil? page = @first_page else return if !@need_initial_load && @cursor.nil? page = fetch_page(@cursor) end @at_first_page = false @need_initial_load = false @cursor = read_field(page, @cursor_field) page end |
#reuse_first_page(other) ⇒ NilClass
Uses the first page that other loaded with load_first_page, unless this iterator already sent a request.
46 47 48 49 50 51 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 46 def reuse_first_page(other) return if @first_page_requested || other.first_page.nil? @first_page = other.first_page rewind end |
#rewind ⇒ NilClass
Resets page-by-page iteration (next_page / next?) to the first page.
69 70 71 72 73 74 |
# File 'lib/plaid/internal/iterators/cursor_page_iterator.rb', line 69 def rewind @need_initial_load = @initial_cursor.nil? @cursor = @initial_cursor @at_first_page = true nil end |