Class: NinjaOne::RequestPagination::CursorPagination
- Inherits:
-
Object
- Object
- NinjaOne::RequestPagination::CursorPagination
- 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": []
}
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.data(body) ⇒ Hash, ...
Processes the API response body and returns it unchanged.
Instance Method Summary collapse
-
#initialize(page_size) ⇒ CursorPagination
constructor
Initializes the pagination object with the specified page size.
-
#more_pages? ⇒ Boolean
Checks if more pages are available.
-
#next_page!(data) ⇒ Object
Updates the pagination state for the next page based on the current page's data.
-
#page_options ⇒ Hash<Symbol, Integer|String>
Returns the pagination options to be sent as query parameters.
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).
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.
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.
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
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.
43 44 45 46 47 48 |
# File 'lib/ninjaone/cursor_pagination.rb', line 43 def { pageSize: @limit, cursor: @cursor }.compact end |