Module: InsalesApi::Resource::Paginated

Included in:
Base
Defined in:
lib/insales_api/resource/paginated.rb

Instance Method Summary collapse

Instance Method Details

#find_each(*args) ⇒ Object



6
7
8
9
10
# File 'lib/insales_api/resource/paginated.rb', line 6

def find_each(*args)
  find_in_batches(*args) do |batch|
    batch.each { |record| yield record }
  end
end

#find_in_batches(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/insales_api/resource/paginated.rb', line 12

def find_in_batches(options = {})
  per_page = options[:per_page] || PER_PAGE_DEFAULT
  params = { per_page: per_page }.merge(options[:params] || {})
  page = 1
  loop do
    items = all(params: params.merge(page: page))
    return unless items.any?

    yield items
    return if items.count < per_page

    page += 1
  end
end