Class: NinjaOne::RequestPagination::CursorPagination

Inherits:
Object
  • Object
show all
Defined in:
lib/ninjaone/cursor_pagination.rb

Overview

Maintains pagination state for cursor-based endpoints.

NinjaOne returns payloads like:

{
"cursor": { "name": "string", "offset": 0, "count": 0, "expires": 0 },
"results": []
}

Examples:

Pagination Initialization

paginator = NinjaOne::RequestPagination::CursorPagination.new(100)
options = paginator.page_options # => { pageSize: 100 }

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size) ⇒ CursorPagination

Initializes the pagination object with the specified page size. Assumes that pagination starts at the first page (offset 0).

Parameters:

  • page_size (Integer) —

    Number of results per page



31
32
33
34
35
36
37
# File 'lib/ninjaone/cursor_pagination.rb', line 31

def initialize(page_size)
  @offset = 0
  @cursor = nil
  @limit = page_size
  # Assume at least one page initially (used to short-circuit more_pages? after last page)
  @total = @limit
end

Instance Attribute Details

#limit ⇒ Object (readonly)

Returns the value of attribute limit.



25
26
27
# File 'lib/ninjaone/cursor_pagination.rb', line 25

def limit
  @limit
end

#offset ⇒ Object (readonly)

Returns the value of attribute offset.



25
26
27
# File 'lib/ninjaone/cursor_pagination.rb', line 25

def offset
  @offset
end

#total ⇒ Object (readonly)

Returns the value of attribute total.



25
26
27
# File 'lib/ninjaone/cursor_pagination.rb', line 25

def total
  @total
end

Class Method Details

.data(body) ⇒ Hash, ...

Processes the API response body and returns it unchanged. This method can be overridden to handle specific response parsing needs.

Parameters:

  • body (Hash, Array, String) —

    The response body from the API

Returns:

  • (Hash, Array, String) —

    Processed data (unchanged)



71
72
73
74
# File 'lib/ninjaone/cursor_pagination.rb', line 71

def self.data(body)
  return body if body.is_a?(Array)
  body['results'] || body
end

Instance Method Details

#more_pages? ⇒ Boolean

Checks if more pages are available. More pages are available if the data has cursor records or if we just start with getting the first page.

Returns:

  • (Boolean) —

    true if more pages are available, false otherwise



80
81
82
# File 'lib/ninjaone/cursor_pagination.rb', line 80

def more_pages?
  @offset.zero? || @offset < @total
end

#next_page!(data) ⇒ Object

Updates the pagination state for the next page based on the current page's data. The NinjaOne API does not allow skipping

Parameters:

  • data (Hash) —

    The data from the current page



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ninjaone/cursor_pagination.rb', line 54

def next_page!(data)
  # Update the total number of results based on the size of the current data set.
  if cursor = data['cursor']
    @total = cursor['count']
    @offset = cursor['offset']
    @cursor = cursor['name']
  else
    # no cursor, then we're ready
    @offset = @total
  end
end

#page_options ⇒ Hash<Symbol, Integer|String>

Returns the pagination options to be sent as query parameters. If a cursor is present, it will be included to fetch the next page.

Returns:

  • (Hash<Symbol, Integer|String>) —

    Query parameters for pagination



43
44
45
46
47
48
# File 'lib/ninjaone/cursor_pagination.rb', line 43

def page_options
  {
    pageSize: @limit,
    cursor: @cursor
  }.compact
end