Class: Certynix::Models::Paginator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/certynix/models/paginator.rb

Overview

Paginador automático com suporte completo ao módulo Enumerable.

Examples:

Iterar todos os assets

client.assets.list.each { |a| puts a[:id] }

Lazy — pegar apenas os 10 primeiros

client.assets.list.lazy.first(10)

Usar métodos Enumerable

verified = client.assets.list.select { |a| a[:status] == 'verified' }

Instance Method Summary collapse

Constructor Details

#initialize(http:, path:, params: {}) ⇒ Paginator

Returns a new instance of Paginator.



18
19
20
21
22
# File 'lib/certynix/models/paginator.rb', line 18

def initialize(http:, path:, params: {})
  @http   = http
  @path   = path
  @params = params
end

Instance Method Details

#eachObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/certynix/models/paginator.rb', line 24

def each
  cursor = nil

  loop do
    query    = cursor ? @params.merge(cursor: cursor) : @params
    response = @http.get(@path, query)

    data       = response[:data] || []
    pagination = response[:pagination] || {}

    data.each { |item| yield item }

    break unless pagination[:has_more]
    cursor = pagination[:next_cursor]
    break if cursor.nil? || cursor.empty?
  end
end