Class: Sophos::RequestPagination::PagesPagination

Inherits:
Object
  • Object
show all
Defined in:
lib/sophos/pagination.rb

Overview

Defines HTTP request pagination logic for Sophos APIs. Sophos supports different pagination approaches for the global API and tenant APIs.

Expected response structure: { "pages": { "current": 1, # Current page number (optional) "size": 50, # Items per page "total": 3, # Total pages (optional) "items": 123, # Total items (optional) "maxSize": 100, # Maximum items per page allowed (optional) "nextKey": "abc123" # Key for fetching the next page, used when available }, "items": [] # Array of data items for the current page }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size) ⇒ PagesPagination

Returns a new instance of PagesPagination.



27
28
29
30
31
32
# File 'lib/sophos/pagination.rb', line 27

def initialize(page_size)
  @page_size = page_size
  @total = 1       # Default total pages
  @current = 1     # Start at the first page
  @next_key = nil  # Key for fetching the next page if provided by the API
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



25
26
27
# File 'lib/sophos/pagination.rb', line 25

def current
  @current
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



25
26
27
# File 'lib/sophos/pagination.rb', line 25

def page_size
  @page_size
end

#totalObject (readonly)

Returns the value of attribute total.



25
26
27
# File 'lib/sophos/pagination.rb', line 25

def total
  @total
end

Class Method Details

.data(body) ⇒ Object

Extracts the relevant data items from the API response.



58
59
60
# File 'lib/sophos/pagination.rb', line 58

def self.data(body)
  body['items'] || body
end

Instance Method Details

#more_pages?Boolean

Returns true if more pages are available.

Returns:

  • (Boolean)


63
64
65
# File 'lib/sophos/pagination.rb', line 63

def more_pages?
  @current <= @total
end

#next_page!(data) ⇒ Object

Updates the pagination state based on the API response data.



42
43
44
45
46
47
48
49
50
# File 'lib/sophos/pagination.rb', line 42

def next_page!(data)
  pages = page_info(data)
  if pages
    set_page_state(pages)
  else
    # Assume a single-page request if no pagination info is available.
    @total = 0
  end
end

#page_info(body) ⇒ Object

Extracts the 'pages' metadata from the response body.



53
54
55
# File 'lib/sophos/pagination.rb', line 53

def page_info(body)
  body['pages']
end

#page_optionsObject

Returns options to be passed as query parameters for the next API request.



35
36
37
38
39
# File 'lib/sophos/pagination.rb', line 35

def page_options
  options = { page: @current, pageSize: @page_size, pageTotal: true }
  options[:pageFromKey] = @next_key if @current > 1 && @next_key # wellicht nil zetten zonder && @next_key?
  options
end