module Lightspeed::API::Helpers::Pagination

Public Instance Methods

with_pages(limit:) { |options_string(offset, limit)| ... } click to toggle source
# File lib/lightspeed/api/helpers/pagination.rb, line 5
def with_pages(limit:)
  limit ||= 100
  results = []
  offset = 0
  count = Float::INFINITY
  while offset < count
    response = yield(options_string(offset, limit))
    body = response.body
    raise_error(response) unless response.success?
    attributes = body.delete("@attributes")
    count = attributes["count"].to_i
    offset = attributes["offset"].to_i + limit
    results << body.values
  end
  results.flatten
end

Private Instance Methods

options_string(offset, limit) click to toggle source
# File lib/lightspeed/api/helpers/pagination.rb, line 24
def options_string(offset, limit)
  "offset=#{offset}&limit=#{limit}"
end
raise_error(response) click to toggle source
# File lib/lightspeed/api/helpers/pagination.rb, line 28
def raise_error(response)
  raise Lightspeed::API::Error.new(
    message: response.body["message"],
    response: response,
  )
end