Class: Hudu::RequestPagination::PagingInfoPager
- Inherits:
-
Object
- Object
- Hudu::RequestPagination::PagingInfoPager
- Defined in:
- lib/hudu/pagination.rb
Overview
The PagingInfoPager 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.
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) ⇒ Array, ...
Extracts paginated data from the response body.
Instance Method Summary collapse
-
#initialize(page_size) ⇒ PagingInfoPager
constructor
Initializes a new PagingInfoPager 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) ⇒ PagingInfoPager
Initializes a new PagingInfoPager instance.
26 27 28 29 |
# File 'lib/hudu/pagination.rb', line 26 def initialize(page_size) @page = 1 @page_total = @page_size = page_size end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
21 22 23 |
# File 'lib/hudu/pagination.rb', line 21 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
21 22 23 |
# File 'lib/hudu/pagination.rb', line 21 def offset @offset end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
21 22 23 |
# File 'lib/hudu/pagination.rb', line 21 def total @total end |
Class Method Details
.data(body) ⇒ Array, ...
Extracts paginated data from the response body.
74 75 76 77 78 79 80 81 82 |
# File 'lib/hudu/pagination.rb', line 74 def self.data(body) # assume hash {"resource":[...]}, get first key and return array data result = body if result.respond_to?(:first) _k, v = body.first result = v if v.is_a?(Array) || v.is_a?(Hash) end result end |
Instance Method Details
#more_pages? ⇒ Boolean
Determines whether there are more pages to fetch.
61 62 63 64 |
# File 'lib/hudu/pagination.rb', line 61 def more_pages? # while full page we have next page @page_total == @page_size end |
#next_page!(body) ⇒ Integer
Advances to the next page based on the response body and updates internal pagination state.
49 50 51 52 53 |
# File 'lib/hudu/pagination.rb', line 49 def next_page!(body) @page += 1 a = PagingInfoPager.data(body) @page_total = a.is_a?(Array) ? a.count : 1 end |
#page_options ⇒ Hash
Provides the current pagination parameter options for each rest request.
37 38 39 |
# File 'lib/hudu/pagination.rb', line 37 def { page: @page, page_size: @page_size } end |