Class: Tenable::Pagination
- Inherits:
-
Object
- Object
- Tenable::Pagination
- Includes:
- Enumerable
- Defined in:
- lib/tenable/pagination.rb
Overview
Provides lazy, offset-based pagination over API list endpoints.
Fetches pages on demand and yields individual items via #each, keeping memory usage constant regardless of total result count. Includes Enumerable for standard collection methods.
Constant Summary collapse
- MAX_PAGE_SIZE =
Returns maximum items per page.
200
Instance Method Summary collapse
-
#each {|item| ... } ⇒ Enumerator
Iterates over all paginated items.
-
#initialize(limit: MAX_PAGE_SIZE) {|offset, limit| ... } ⇒ Pagination
constructor
Creates a new paginator.
-
#lazy ⇒ Enumerator::Lazy
Returns a lazy enumerator over all paginated items.
Constructor Details
#initialize(limit: MAX_PAGE_SIZE) {|offset, limit| ... } ⇒ Pagination
Creates a new paginator.
22 23 24 25 |
# File 'lib/tenable/pagination.rb', line 22 def initialize(limit: MAX_PAGE_SIZE, &fetcher) @limit = [limit, MAX_PAGE_SIZE].min @fetcher = fetcher end |
Instance Method Details
#each {|item| ... } ⇒ Enumerator
Iterates over all paginated items.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/tenable/pagination.rb', line 39 def each(&block) return enum_for(:each) unless block offset = 0 loop do page = @fetcher.call(offset, @limit) items = extract_items(page) total = extract_total(page) items.each(&block) offset += @limit break if offset >= total || items.empty? end end |
#lazy ⇒ Enumerator::Lazy
Returns a lazy enumerator over all paginated items.
58 59 60 |
# File 'lib/tenable/pagination.rb', line 58 def lazy each.lazy end |