Class: Sophos::RequestPagination::PagesPagination

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

Overview

Sophos uses different pagination for global api and tenant api

response structure: { “pages”: {

  "current": 1, # page no - not laways present
  "size": 50,
  "total": 3,   # not always present - pages
  "items": 123, # not always present - records
  "maxSize": 100
},

“items”: []

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_size) ⇒ PagesPagination

Returns a new instance of PagesPagination.



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

def initialize(page_size)
  # ignore page size
  @page_size = page_size
  @total = @current = 1
  @nextKey = nil
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



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

def current
  @current
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



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

def page_size
  @page_size
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Class Method Details

.data(body) ⇒ Object



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

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

Instance Method Details

#more_pages?Boolean

only single page available

Returns:

  • (Boolean)


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

def more_pages?
  @current <= @total
end

#next_page!(data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sophos/pagination.rb', line 36

def next_page!(data)

  pages = page_info(data)
  if pages
    @total = pages['total'].to_i
    if pages['current']
      @current = pages['current'].to_i + 1
    else
      @current += 1
    end
    @nextKey = pages['nextKey'] if pages['nextKey']
  else
    # no page info so assume single page request
    @total = 0
  end
end

#page_info(body) ⇒ Object



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

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

#page_optionsObject



30
31
32
33
34
# File 'lib/sophos/pagination.rb', line 30

def page_options
  options = { 'page': @current, 'pageSize': @page_size, 'pageTotal': true }
  options = options.merge({ 'pageFromKey': @nextKey }) if @current > 1
  options
end