class Clever::Paginator

Public Class Methods

fetch(*params) click to toggle source
# File lib/clever/paginator.rb, line 34
def self.fetch(*params)
  new(*params).fetch
end
new(connection, path, method, type, client: nil) click to toggle source
# File lib/clever/paginator.rb, line 8
def initialize(connection, path, method, type, client: nil)
  @connection = connection
  @path       = path
  @method     = method
  @type       = type
  @client     = client
  @next_path  = nil
end

Public Instance Methods

fetch() click to toggle source
# File lib/clever/paginator.rb, line 17
def fetch
  Enumerator.new do |yielder|
    loop do
      response = request(@next_path || @path)
      body = response.body

      fail "Failed to fetch #{@path}" unless response.success?

      body.each { |item| add_record(yielder, item) } if body.any?

      @next_path = response.next_uri

      fail StopIteration unless @next_path
    end
  end.lazy
end

Private Instance Methods

add_record(yielder, item) click to toggle source
# File lib/clever/paginator.rb, line 40
def add_record(yielder, item)
  return unless should_add_record?(item)

  yielder << @type.new(item['data'], client: @client)
end
request(path = @path) click to toggle source
# File lib/clever/paginator.rb, line 52
def request(path = @path)
  @connection.execute(path, @method, limit: PAGE_LIMIT)
end
should_add_record?(item) click to toggle source
# File lib/clever/paginator.rb, line 46
def should_add_record?(item)
  return true unless @type == Clever::Types::Event

  item['data']['type'].start_with?(*EVENT_TYPES)
end