Increment parameter in rake task

Go To StackoverFlow.com

0

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?

2012-04-03 21:00
by Shpigford
In the first call to rake task, offset would be 0; 2nd time you call rake task, offset should be 100; and so on. Is it like that - Jatin Ganhotra 2012-04-04 05:25
@JatinGanhotra Yes, that's correct - Shpigford 2012-04-04 13:45


1

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
2012-04-04 12:04
by CMW
Ads