Class: GoCardlessPro::Paginator

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Paginator

initialize a paginator

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :service (Object)

    the service class to use to make requests to

  • :options (Object)

    additional options to send with the requests



8
9
10
11
# File 'lib/gocardless_pro/paginator.rb', line 8

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

Instance Method Details

#enumeratorObject

Get a lazy enumerable for listing data from the API



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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