From e7abcbe11cb46fafccd8ab8c6b485e091e57dbb4 Mon Sep 17 00:00:00 2001
From: Moncef Belyamani <giter@done>
Date: Thu, 4 Jul 2013 19:43:42 -0400
Subject: [PATCH] Add sleep option for geocode rake task.

---
 README.md               | 4 +++-
 lib/tasks/geocoder.rake | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 26915ed7..dfa1370d 100644
--- a/README.md
+++ b/README.md
@@ -98,7 +98,9 @@ If you have just added geocoding to an existing application with a lot of object
 
     rake geocode:all CLASS=YourModel
 
-Geocoder will print warnings if you exceed the rate limit for your geocoding service.
+Geocoder will print warnings if you exceed the rate limit for your geocoding service. Some services — Google notably — enforce a per-second limit in addition to a per-day limit. To avoid exceeding the per-second limit, you can add a `sleep` option to the rake task, like so:
+
+    rake geocode:all CLASS=YourModel sleep=0.25
 
 
 Request Geocoding by IP Address
diff --git a/lib/tasks/geocoder.rake b/lib/tasks/geocoder.rake
index 33ed4829..4d0840fc 100644
--- a/lib/tasks/geocoder.rake
+++ b/lib/tasks/geocoder.rake
@@ -2,11 +2,13 @@ namespace :geocode do
   desc "Geocode all objects without coordinates."
   task :all => :environment do
     class_name = ENV['CLASS'] || ENV['class']
+    sleep_timer = ENV['SLEEP'] || ENV['sleep']
     raise "Please specify a CLASS (model)" unless class_name
     klass = class_from_string(class_name)
 
     klass.not_geocoded.each do |obj|
       obj.geocode; obj.save
+      sleep(sleep_timer.to_f) if sleep_timer.present?
     end
   end
 end
-- 
GitLab