Shpigford
Shpigford

Reputation: 25368

Increment parameter in rake task

I have an API call that needs to be made in "chunks" (due to API limits). I'd then loop through that data.

All of this happens in a rake task.

Here's the loop:

Stripe::Event.all(:count => 100, :offset => 0).each do |event|

end

But I need to increment the offset parameter by 100 (ie. 100, 200, 300) to actually be able to loop through all the events.

So what's the best way to increment that offset parameter within the rake task?

Upvotes: 0

Views: 376

Answers (1)

CMW
CMW

Reputation: 3144

If Stripe::Event is an ActiveRecord model, I think you are looking for:

Stripe::Event.find_in_batches(:batch_size => 100).each do |batch|
  batch.each do |event|
    ...
  end
end

Otherwise I think you could look at http://apidock.com/rails/ActiveRecord/Batches/find_in_batches for inspiration on how to solve this problem.

The most straight forward would be something like

events = "not blank"
count = 100
offset = 0
until events.blank? do
  events = Stripe::Event.all(:count => count, :offset => offset)
  events.each do |event|

  end
  offset += count
end

Upvotes: 1

Related Questions