Class: Hudu::RequestPagination::PagingInfoPager

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

Examples:

Basic Usage

pager = PagingInfoPager.new(50)
while pager.more_pages?
  response = api_client.get_data(pager.page_options)
  pager.next_page!(response.body)
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size) ⇒ PagingInfoPager

Initializes a new PagingInfoPager instance.

Parameters:

  • page_size (Integer)

    The number of records to fetch per page.



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

#limitObject (readonly)

Returns the value of attribute limit.



21
22
23
# File 'lib/hudu/pagination.rb', line 21

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



21
22
23
# File 'lib/hudu/pagination.rb', line 21

def offset
  @offset
end

#totalObject (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.

Examples:

response_body = { "items" => [1, 2, 3] }
PagingInfoPager.data(response_body) # => [1, 2, 3]

Parameters:

  • body (Hash)

    The response body containing resource data, expected to be in a hash format.

Returns:

  • (Array, Hash, Object)

    Returns the extracted data, which could be an array, hash, or other object.



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.

Examples:

pager.more_pages? # => true or false

Returns:

  • (Boolean)

    Returns ‘true` if the current page is full, indicating another page might exist.



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.

Examples:

response_body = { "items" => [...] }
pager.next_page!(response_body)

Parameters:

  • body (Hash)

    The response body from the API, expected to contain a paginated resource.

Returns:

  • (Integer)

    The updated page total, typically the count of items on the current page.



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_optionsHash

Provides the current pagination parameter options for each rest request.

Examples:

pager.page_options # => { page: 1, page_size: 50 }

Returns:

  • (Hash)

    A hash containing the current page and page size.



37
38
39
# File 'lib/hudu/pagination.rb', line 37

def page_options
  { page: @page, page_size: @page_size }
end