Module: FulfilApi::Relation::Batchable
- Included in:
- FulfilApi::Relation
- Defined in:
- lib/fulfil_api/relation/batchable.rb
Overview
The Batchable module includes a set of
helper/query methods that queries resources in Fulfil in batches.
Instance Method Summary collapse
-
#find_each(batch_size: 500) {|FulfilApi::Resource| ... } ⇒ FulfilApi::Relation
The #find_each is a shorthand for iterating over individual API resources in a memory effective way.
-
#in_batches(of: 500) {|FulfilApi::Relation| ... } ⇒ FulfilApi::Relation
Finds API resources in batches.
Instance Method Details
#find_each(batch_size: 500) {|FulfilApi::Resource| ... } ⇒ FulfilApi::Relation
The #find_each is a shorthand for iterating over individual API resources
in a memory effective way.
Under the hood, it uses the #in_batches to find API resources in batches
and process them efficiently.
23 24 25 26 27 |
# File 'lib/fulfil_api/relation/batchable.rb', line 23 def find_each(batch_size: 500, &block) in_batches(of: batch_size) do |batch| batch.each(&block) end end |
#in_batches(of: 500) {|FulfilApi::Relation| ... } ⇒ FulfilApi::Relation
Note:
the #in_batches automatically retries when it encounters a 429 (TooManyRequests) HTTP error to ensure the lookup can be completed.
Finds API resources in batches. Defaults to the maximum number of resources
Fulfil's API endpoints will return (500 resources).
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/fulfil_api/relation/batchable.rb', line 47 def in_batches(of: 500) # rubocop:disable Metrics/MethodLength current_offset = request_offset.presence || 0 batch_size = of loop do batch_relation = dup.offset(current_offset * batch_size).limit(batch_size) batch_relation.load yield(batch_relation) break unless batch_relation.size == batch_size current_offset += 1 rescue FulfilApi::Error => e retry if e.details[:response_status] == 429 end self end |