class GoCardless::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 :path the path to make the request to @option options :options additional options to send with the requests

# File lib/gocardless-pro/paginator.rb, line 9
def initialize(options = {})
  @service = options.fetch(:service)
  @path = options.fetch(:path)
  @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 16
def enumerator
  response = get_initial_response
  Enumerator.new do |yielder|
    loop do
      items = @service.unenvelope_body(response.body)
      items.each { |item| yielder << item }

      after_cursor = response.meta['cursors']['after']
      break if after_cursor.nil?

      response = @service.make_request(:get, @path, @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 33
def get_initial_response
  @initial_response ||= @service.make_request(:get, @path, @options)
end