class SPOT::Paginator

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

Public Class Methods

new(service:, params: {}) click to toggle source
# File lib/spot-gps/paginator.rb, line 4
def initialize(service:, params: {})
  @service = service
  @params = (params || {}).dup
end

Public Instance Methods

enumerator() click to toggle source

Get a lazy enumerable for listing data from the API

# File lib/spot-gps/paginator.rb, line 10
def enumerator
  Enumerator.new do |yielder|
    response = get_initial_response

    loop do
      items = response.records

      # If there are no records, we're done
      break if items.empty?

      # Otherwise, iterate through the records...
      items.each { |item| yielder << item }

      # ...and fetch the next page
      @params ||= {}
      @params[:page] ||= 1
      @params[:page] += 1

      response = @service.list(**@params)
    end
  end.lazy
end

Private Instance Methods

get_initial_response() click to toggle source
# File lib/spot-gps/paginator.rb, line 35
def get_initial_response
  @initial_response ||= @service.list(**@params)
end