Class: Candid::Internal::OffsetItemIterator

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

Instance Method Summary collapse

Methods inherited from ItemIterator

#each, #get_next, #has_next?

Constructor Details

#initialize(initial_page:, item_field:, has_next_field:, step:, &block) ⇒ Candid::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)

    The name of the field in API responses to extract the items to iterate over.

  • has_next_field (Symbol)

    The name 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
# File 'lib/candid/internal/iterators/offset_item_iterator.rb', line 15

def initialize(initial_page:, item_field:, has_next_field:, step:, &block)
  @item_field = item_field
  @page_iterator = OffsetPageIterator.new(initial_page:, item_field:, has_next_field:, step:, &block)
  @page = nil
end

Instance Method Details

#pagesCandid::Internal::OffsetPageIterator

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



24
25
26
# File 'lib/candid/internal/iterators/offset_item_iterator.rb', line 24

def pages
  @page_iterator
end