Class: EcwidApi::PagedEcwidResponse
- Inherits:
-
Object
- Object
- EcwidApi::PagedEcwidResponse
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/ecwid_api/paged_ecwid_response.rb
Instance Method Summary collapse
-
#initialize(client, path, params = {}, &block) ⇒ PagedEcwidResponse
constructor
Public: Initialize a new PagedEcwidResponse.
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 |
# File 'lib/ecwid_api/paged_ecwid_response.rb', line 30 def initialize(client, path, params = {}, &block) params[:limit] = 100 params.delete(:offset) block ||= Proc.new { |item| item } response = client.get(path, params) @paged_enumerator = PagedEnumerator.new(response) do |response, yielder| response.body["items"].each do |item| yielder << block.call(item) end count, offset, total = %w(count offset total).map do |i| response.body[i].to_i end if count == 0 || count + offset >= total false else client.get(path, params.merge(offset: offset + count)) end end end |