0

I'm using task to make Mass Pay API call to PayPal.

EDIT: If there are more than 250 recepients I need to go out from cycle and call it again after finishing.

Here is my task:

    task :pay => :environment do
    clnt = HTTPClient.new
    i = 0;
    data = {    "METHOD" => "MassPay",
    ...}
    @users = User.all
    @users.each do |user|
       if i == 249 
            // call task again
       end
        data["L_EMAIL#{i}"] = "#{user.email}"
        data["L_AMT#{i}"] = "1.21"
        ...
        i+=1

        end

How I can make it ?

MID
  • 1,815
  • 4
  • 29
  • 40

2 Answers2

2

A more functional way to do this would be to wrap everyting in a in_groups_of call. Guessing from your code, you would get something like:

task :pay => :environment do
  @users = User.all

  @users.in_groups_of(250, false).each do |group|
    clnt = HTTPClient.new
    data = {    "METHOD" => "MassPay",
      ...}

    group.each do |user|
      data["L_EMAIL#{i}"] = "#{user.email}"
      data["L_AMT#{i}"] = "1.21"
      ...
    end
  end
end

Documentation is here.

niels
  • 1,719
  • 10
  • 20
0

Try this:

Rake::Task['pay'].reenable 
megas
  • 21,401
  • 12
  • 79
  • 130
  • do you mean `if i == 249 Rake::Task['pay'].reenable end `? – MID Aug 21 '12 at 10:46
  • I found that I'm a little bit fail in my logic. When there are 249 recepients I need to go out from the cycle and finish task.Then call it again. So I should use break operator ? – MID Aug 21 '12 at 11:00
  • or somekind of `go to` operator – MID Aug 21 '12 at 11:03