Class: Huntress::RequestPagination::PaginationPager

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

Examples:

Basic Usage

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size) ⇒ PaginationPager

Initializes a new PaginationPager instance.

Parameters:

  • page_size (Integer)

    The number of records to fetch per page.



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.

Examples:

response_body = { "items" => [1, 2, 3] }
PaginationPager.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.



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

Examples:

response_body = { 
  "items" => [1, 2, 3],
  "pagination" => {"current_page" => 1, "current_page_count": 10, "limit": 10, "total_count":60, "next_page": 2,
  "next_page_url": "https://api.xx.io/v1/signals?page_token=NjEyNjgxMzE%3D&limit=10", "next_page_token": "NjEyNjgxMzE=" } }
PaginationPager.pagination_data(response_body) # => {"current_page" => 1, "current_page_count": 10, ...

Parameters:

  • body (Hash)

    The response body containing resource data, and pagination infromation in a hash format.

Returns:

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



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.

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
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.

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.



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_optionsHash

Provides the current pagination parameter options for each rest request.

Examples:

pager.page_options # => { page_token: 'Mjadso43289', page_size: 50 }

Returns:

  • (Hash)

    A hash containing the current page and page size.



36
37
38
39
40
# File 'lib/huntress/pagination.rb', line 36

def page_options
  options = { limit: @limit }
  options.merge!({ page_token: @page_token }) if @page_token
  options
end