Class: Huntress::RequestPagination::PaginationPager
- Inherits:
-
Object
- Object
- Huntress::RequestPagination::PaginationPager
- Defined in:
- lib/huntress/pagination.rb
Overview
The PaginationPager class provides a mechanism to handle pagination information for API responses.
It manages the current page, page size, and provides utilities for determining if there are more pages to fetch.
Class Method Summary collapse
-
.data(body) ⇒ Array, ...
Extracts paginated data from the response body.
-
.pagination_data(body) ⇒ Object
Extracts pagination information.
Instance Method Summary collapse
-
#initialize(page_size) ⇒ PaginationPager
constructor
Initializes a new PaginationPager instance.
-
#more_pages? ⇒ Boolean
Determines whether there are more pages to fetch.
-
#next_page!(body) ⇒ Integer
Advances to the next page based on the response body and updates internal pagination state.
-
#page_options ⇒ Hash
Provides the current pagination parameter options for each rest request.
Constructor Details
#initialize(page_size) ⇒ PaginationPager
Initializes a new PaginationPager instance.
24 25 26 27 28 |
# File 'lib/huntress/pagination.rb', line 24 def initialize(page_size) @page = 1 @page_token = @pagination_data = nil @limit = page_size end |
Class Method Details
.data(body) ⇒ Array, ...
Extracts paginated data from the response body.
78 79 80 81 82 83 84 85 86 |
# File 'lib/huntress/pagination.rb', line 78 def self.data(body) # assume hash {"resource":[...]}, get first key and return array data result = body if result.is_a?(Hash) _k, v = body.first result = v if v.is_a?(Array) || v.is_a?(Hash) end result end |
.pagination_data(body) ⇒ Object
Extracts pagination information
99 100 101 102 103 104 105 106 |
# File 'lib/huntress/pagination.rb', line 99 def self.pagination_data(body) # assume hash {"resource":[...], "pagination":{}}, return pagination data result = nil if body.is_a?(Hash) result = body["pagination"] end result end |
Instance Method Details
#more_pages? ⇒ Boolean
Determines whether there are more pages to fetch.
61 62 63 64 65 66 67 68 |
# File 'lib/huntress/pagination.rb', line 61 def more_pages? # while full page we have next page if @pagination_data @page_token = @pagination_data["next_page_token"] else @page.eql? 1 end end |
#next_page!(body) ⇒ Integer
Advances to the next page based on the response body and updates internal pagination state.
50 51 52 53 |
# File 'lib/huntress/pagination.rb', line 50 def next_page!(body) @page += 1 @pagination_data = PaginationPager.pagination_data(body) end |
#page_options ⇒ Hash
Provides the current pagination parameter options for each rest request.
36 37 38 39 40 |
# File 'lib/huntress/pagination.rb', line 36 def = { limit: @limit } .merge!({ page_token: @page_token }) if @page_token end |