Module: LHS::Service::Batch::ClassMethods

Defined in:
lib/lhs/concerns/service/batch.rb

Instance Method Summary collapse

Instance Method Details

#find_each(options = {}) ⇒ Object

Process single entries fetched in batches



11
12
13
14
15
16
17
18
# File 'lib/lhs/concerns/service/batch.rb', line 11

def find_each(options = {})
  find_in_batches(options) do |data|
    data.each do |record|
      item = LHS::Item.new(LHS::Data.new(record, data, self.class))
      yield LHS::Data.new(item, data, self.class)
    end
  end
end

#find_in_batches(options = {}) ⇒ Object

Process batches of entries



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lhs/concerns/service/batch.rb', line 21

def find_in_batches(options = {})
  fail 'No block given' unless block_given?
  start = options[:start] || 1
  batch_size = options[:batch_size] || 100
  params = options[:params] || {}
  loop do # as suggested by Matz
    data = instance.request(params: params.merge(limit: batch_size, offset: start))
    batch_size = data._raw['limit']
    left = data._raw['total'].to_i - data._raw['offset'].to_i - data._raw['limit'].to_i
    yield data
    break if left <= 0
    start += batch_size
  end
end