Class: Plaid::Internal::OffsetItemIterator

Inherits:
ItemIterator show all
Defined in:
lib/plaid/internal/iterators/offset_item_iterator.rb

Instance Attribute Summary

Attributes inherited from ItemIterator

#http_response

Instance Method Summary collapse

Methods inherited from ItemIterator

#each, #load_first_page, #next?, #next_element, #rewind

Constructor Details

#initialize(initial_page:, item_field:, has_next_field:, step:, &block) ⇒ Plaid::Internal::OffsetItemIterator

Instantiates an OffsetItemIterator, an Enumerable class which wraps calls to an offset-based paginated API and yields the individual items from it.

Parameters:

  • initial_page (Integer) —

    The initial page or offset to start from when iterating.

  • item_field (Symbol, Array<Symbol>) —

    The name (or nested path) of the field in API responses to extract the items to iterate over.

  • has_next_field (Symbol, Array<Symbol>) —

    The name (or nested path) of the field in API responses containing a boolean of whether another page exists.

  • step (Boolean) —

    If true, treats the page number as a true offset (i.e. increments the page number by the number of items returned from each call rather than just 1)

  • block (Proc) —

    A block which is responsible for receiving a page number to use and returning the given page from the API.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/plaid/internal/iterators/offset_item_iterator.rb', line 15

def initialize(initial_page:, item_field:, has_next_field:, step:, &block)
  super()
  @item_field = item_field
  @http_response = nil
  get_page = track_http_responses(block)
  @page_iterator = OffsetPageIterator.new(initial_page:, item_field:, has_next_field:, step:, &get_page)
  # Items are read through their own page cursor so that reading `pages` never skips items.
  @item_pages = OffsetPageIterator.new(initial_page:, item_field:, has_next_field:, step:, &get_page)
  @page = nil
  @item_index = 0
end

Instance Method Details

#pages ⇒ Plaid::Internal::OffsetPageIterator

Returns the OffsetPageIterator that is mediating access to the underlying API.



30
31
32
# File 'lib/plaid/internal/iterators/offset_item_iterator.rb', line 30

def pages
  @page_iterator
end