Class: Tenable::Pagination

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Integer)

    maximum items per page

200

Instance Method Summary collapse

Constructor Details

#initialize(limit: MAX_PAGE_SIZE) {|offset, limit| ... } ⇒ Pagination

Creates a new paginator.

Parameters:

  • limit (Integer) (defaults to: MAX_PAGE_SIZE)

    items per page (capped at MAX_PAGE_SIZE)

Yields:

  • (offset, limit)

    block that fetches a page of results

Yield Parameters:

  • offset (Integer)

    the current offset

  • limit (Integer)

    the page size

Yield Returns:

  • (Hash)

    a hash containing “items” and “total” keys



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.

Examples:

Iterate over all results

paginator = Tenable::Pagination.new { |offset, limit| fetch_page(offset, limit) }
paginator.each { |item| process(item) }

Use Enumerable methods

paginator.first(10)
paginator.select { |item| item['severity'] > 2 }

Yields:

  • (item)

    yields each item

Returns:

  • (Enumerator)

    if no block is given



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

#lazyEnumerator::Lazy

Returns a lazy enumerator over all paginated items.

Returns:

  • (Enumerator::Lazy)


58
59
60
# File 'lib/tenable/pagination.rb', line 58

def lazy
  each.lazy
end