Class: Clever::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/clever/paginator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, path, method, type, client: nil) ⇒ Paginator

Returns a new instance of Paginator.



8
9
10
11
12
13
14
15
# File 'lib/clever/paginator.rb', line 8

def initialize(connection, path, method, type, client: nil)
  @connection = connection
  @path       = path
  @method     = method
  @type       = type
  @client     = client
  @next_path  = nil
end

Class Method Details

.fetch(*params) ⇒ Object



34
35
36
# File 'lib/clever/paginator.rb', line 34

def self.fetch(*params)
  new(*params).fetch
end

Instance Method Details

#fetchObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/clever/paginator.rb', line 17

def fetch
  Enumerator.new do |yielder|
    loop do
      response = request(@next_path || @path)
      body = response.body

      fail "Failed to fetch #{@path}" unless response.success?

      body.each { |item| add_record(yielder, item) } if body.any?

      @next_path = response.next_uri

      fail StopIteration unless @next_path
    end
  end.lazy
end