module LHS::Record::Batch::ClassMethods

Public Instance Methods

find_each(options = {}) { |new(data)| ... } click to toggle source

Process single entries fetched in batches

# File lib/lhs/concerns/record/batch.rb, line 12
def find_each(options = {})
  find_in_batches(options) do |records|
    records.each do |record|
      item = LHS::Item.new(record)
      yield new(LHS::Data.new(item, records._data, self))
    end
  end
end
find_in_batches(options = {}) { |new(data)| ... } click to toggle source

Process batches of entries

# File lib/lhs/concerns/record/batch.rb, line 22
def find_in_batches(options = {})
  raise 'No block given' unless block_given?
  options = options.dup
  start = options.delete(:start) || 1
  batch_size = options.delete(:batch_size) || LHS::Pagination::Base::DEFAULT_LIMIT
  loop do # as suggested by Matz
    options = options.dup
    options[:params] = (options[:params] || {}).merge(limit_key(:parameter) => batch_size, pagination_key(:parameter) => start)
    data = request(options)
    batch_size = data._raw.dig(*limit_key(:body))
    left = data._raw.dig(*total_key).to_i - data._raw.dig(*pagination_key(:body)).to_i - data._raw.dig(*limit_key(:body)).to_i
    yield new(data)
    break if left <= 0
    start += batch_size
  end
end