Class: Plaid::Internal::CursorPageIterator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • initial_cursor (String) —

    The initial cursor to use when iterating, if any.

  • cursor_field (Symbol, Array<Symbol>) —

    The name (or nested path) of the field in API responses to extract the next cursor from.

  • block (Proc) —

    A block which is responsible for receiving a cursor to use and returning the given page from the API. The block should return a two-element array: [parsed_page, raw_http_response].



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.

Returns:

  • (Net::HTTPResponse, nil)


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.

Parameters:

  • block (Proc) —

    The block which each retrieved page is yielded to.

Returns:

  • (NilClass, Enumerator) —

    An Enumerator when no block is given.



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.

Returns:

  • (self)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Object, nil)


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.

Parameters:

Returns:

  • (NilClass)


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.

Returns:

  • (NilClass)


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