class GoCardlessPro::Paginator

A class that can take an API LIST query and auto paginate through results

Public Class Methods

new(options = {}) click to toggle source

initialize a paginator @param options [Hash] @option options :service the service class to use to make requests to @option options :options additional options to send with the requests

# File lib/gocardless_pro/paginator.rb, line 8
def initialize(options = {})
  @service = options.fetch(:service)
  @options = options.fetch(:options)
end

Public Instance Methods

enumerator() click to toggle source

Get a lazy enumerable for listing data from the API

# File lib/gocardless_pro/paginator.rb, line 14
def enumerator
  response = get_initial_response
  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.merge(after: after_cursor))
    end
  end.lazy
end

Private Instance Methods

get_initial_response() click to toggle source
# File lib/gocardless_pro/paginator.rb, line 32
def get_initial_response
  @initial_response ||= @service.list(@options)
end