class EcwidApi::PagedEcwidResponse

Public Class Methods

new(client, path, params = {}, &block) click to toggle source

Public: Initialize a new PagedEcwidResponse

client - an EcwidApi::Client path - a String that is the path to retrieve from the client params - a Hash of parameters to pass along with the request &block - a Block that processes each item returned in the Response

# File lib/ecwid_api/paged_ecwid_response.rb, line 30
def initialize(client, path, params = {}, &block)
  params[:limit] = 100
  params.delete(:offset)

  block ||= proc { |item| item }

  response = client.get(path, params)

  @paged_enumerator = PagedEnumerator.new(response) do |enum_response, yielder|
    count, offset, total = %w[count offset total].map do |i|
      enum_response.body[i].to_i
    end

    if count > 0
      enum_response.body['items'].each do |item|
        yielder << block.call(item)
      end
    end

    if count.zero? || count + offset >= total
      false
    else
      client.get(path, params.merge(offset: offset + count))
    end
  end
end