class DuffelAPI::Paginator

An internal class used within the library to paginated automatically thruogh results from list actions that can be spread over multiple pages

Public Class Methods

new(service:, options:) click to toggle source

@param service [Services::BaseService] a service which implements ‘#list` @param options [Hash] the options originally passed to `#all`

# File lib/duffel_api/paginator.rb, line 9
def initialize(service:, options:)
  @service = service
  @options = options
end

Public Instance Methods

enumerator() click to toggle source

Returns an enumerator that is able to automatically cycle through paginated data returned by the API

@return [Enumerator]

# File lib/duffel_api/paginator.rb, line 18
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