Module: Lightspeed::API::Helpers::Pagination

Included in:
Items
Defined in:
lib/lightspeed/api/helpers/pagination.rb

Instance Method Summary collapse

Instance Method Details

#with_pages(limit:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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