Class: DuffelAPI::Paginator

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Paginator

Returns a new instance of Paginator.



5
6
7
8
# File 'lib/duffel_api/paginator.rb', line 5

def initialize(options = {})
  @service = options.fetch(:service)
  @options = options.fetch(:options)
end

Instance Method Details

#enumeratorObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/duffel_api/paginator.rb', line 10

def enumerator
  response = @service.list(@options)

  Enumerator.new do |yielder|
    loop do
      response.records.each { |item| yielder << item }

      after_cursor = response.after
      break if after_cursor.nil?

      @options[:params] ||= {}
      @options[:params] = @options[:params].merge(after: after_cursor)
      response = @service.list(@options)
    end
  end.lazy
end