Class: EcwidApi::PagedEcwidResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/ecwid_api/paged_ecwid_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, path, params = {}, &block) ⇒ PagedEcwidResponse

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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