Class: Plaid::Internal::ItemIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/plaid/internal/iterators/item_iterator.rb

Direct Known Subclasses

CursorItemIterator, OffsetItemIterator

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#http_response ⇒ Net::HTTPResponse? (readonly)

The raw HTTP response from the most recent page request made by this pager or its pages.

Returns:

  • (Net::HTTPResponse, nil)


10
11
12
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 10

def http_response
  @http_response
end

Instance Method Details

#each ⇒ NilClass, Enumerator

Iterates over each item returned by the API, starting again from the first page on every call. Each loop is independent of other loops and of next_element.

Parameters:

  • block (Proc) —

    The block which each retrieved item is yielded to.

Returns:

  • (NilClass, Enumerator) —

    An Enumerator when no block is given.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 17

def each(&)
  return enum_for(:each) unless block_given?

  # Each loop reads through its own copy of the page stream, so repeated or nested loops never
  # share a position with each other or with `next_element`.
  pass = @item_pages.dup.tap(&:rewind)
  while (page = pass.next_page)
    (read_field(page, @item_field) || []).each(&)
  end
  nil
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 (over items or pages) that starts from the first page reuses it instead of requesting it again. Does nothing if this pager already sent a request.

Returns:

  • (self)


57
58
59
60
61
62
63
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 57

def load_first_page
  return self if @requested

  @item_pages.load_first_page
  @page_iterator.reuse_first_page(@item_pages)
  self
end

#next? ⇒ Boolean

Whether another item will be available from the API.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 42

def next?
  load_next_page if @page.nil?
  return false if @page.nil?

  return true if any_items_in_cached_page?

  load_next_page
  any_items_in_cached_page?
end

#next_element ⇒ Object

Retrieves the next item from the API.



66
67
68
69
70
71
72
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 66

def next_element
  item = next_item_from_cached_page
  return item if item

  load_next_page
  next_item_from_cached_page
end

#rewind ⇒ NilClass

Resets manual item-by-item iteration (next_element / next?) to the first page.

Returns:

  • (NilClass)


32
33
34
35
36
37
# File 'lib/plaid/internal/iterators/item_iterator.rb', line 32

def rewind
  @item_pages.rewind
  @page = nil
  @item_index = 0
  nil
end