Class: Auth0::Internal::CursorPageIterator
- Inherits:
-
Object
- Object
- Auth0::Internal::CursorPageIterator
- Includes:
- Enumerable
- Defined in:
- lib/auth0/internal/iterators/cursor_page_iterator.rb
Instance Method Summary collapse
-
#each(&block) ⇒ NilClass
Iterates over each page returned by the API.
-
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Auth0::Internal::CursorPageIterator
constructor
Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items.
-
#next? ⇒ Boolean
Whether another page will be available from the API.
-
#next_page ⇒ Object?
Retrieves the next page from the API.
Constructor Details
#initialize(initial_cursor:, cursor_field:, &block) ⇒ Auth0::Internal::CursorPageIterator
Instantiates a CursorPageIterator, an Enumerable class which wraps calls to a cursor-based paginated API and yields pages of items.
14 15 16 17 18 19 |
# File 'lib/auth0/internal/iterators/cursor_page_iterator.rb', line 14 def initialize(initial_cursor:, cursor_field:, &block) @need_initial_load = initial_cursor.nil? @cursor = initial_cursor @cursor_field = cursor_field @get_next_page = block end |
Instance Method Details
#each(&block) ⇒ NilClass
Iterates over each page returned by the API.
25 26 27 28 29 |
# File 'lib/auth0/internal/iterators/cursor_page_iterator.rb', line 25 def each(&block) while (page = next_page) block.call(page) end end |
#next? ⇒ Boolean
Whether another page will be available from the API.
34 35 36 |
# File 'lib/auth0/internal/iterators/cursor_page_iterator.rb', line 34 def next? @need_initial_load || !@cursor.nil? end |
#next_page ⇒ Object?
Retrieves the next page from the API.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/auth0/internal/iterators/cursor_page_iterator.rb', line 41 def next_page return if !@need_initial_load && @cursor.nil? @need_initial_load = false result = @get_next_page.call(@cursor) # The block returns either the parsed page directly, or a # [parsed_page, raw_http_response] tuple. Unwrap accordingly. fetched_page = if result.is_a?(Array) result[0] else result end @cursor = fetched_page.send(@cursor_field) fetched_page end |