diff --git a/README.md b/README.md index 4aafd4ef88df8269f1c6d2a1fb3d12996cfa8f28..a51eb798dd672a1c197ed40a5cb60b0027ac5af9 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,10 @@ Geocoder will print warnings if you exceed the rate limit for your geocoding ser rake geocode:all CLASS=YourModel SLEEP=0.25 BATCH=100 +To avoid per-day limit issues (for example if you are trying to geocode thousands of objects and don't want to reach the limit), you can add a `LIMIT` option. Warning: This will ignore the `BATCH` value if provided. + + rake geocode:all CLASS=YourModel LIMIT=1000 + ### Avoiding Unnecessary API Requests Geocoding only needs to be performed under certain conditions. To avoid unnecessary work (and quota usage) you will probably want to geocode an object only when: diff --git a/lib/tasks/geocoder.rake b/lib/tasks/geocoder.rake index b4d8c5048c60bc073ef702439eebf5ee59c4257b..f17dba688b89fd0e2f10fcd66b34fe42d8a98f20 100644 --- a/lib/tasks/geocoder.rake +++ b/lib/tasks/geocoder.rake @@ -1,4 +1,4 @@ -require "geocoder/models/mongoid" +require "geocoder/models/mongoid" namespace :geocode do desc "Geocode all objects without coordinates." @@ -7,6 +7,7 @@ namespace :geocode do sleep_timer = ENV['SLEEP'] || ENV['sleep'] batch = ENV['BATCH'] || ENV['batch'] reverse = ENV['REVERSE'] || ENV['reverse'] + limit = ENV['LIMIT'] || ENV['limit'] raise "Please specify a CLASS (model)" unless class_name klass = class_from_string(class_name) batch = (batch.to_i unless batch.nil?) || 1000 @@ -20,13 +21,20 @@ namespace :geocode do } scope = reverse ? klass.not_reverse_geocoded : klass.not_geocoded + scope = scope.limit(limit) if limit if orm == 'mongoid' scope.each do |obj| geocode_record.call(obj) end elsif orm == 'active_record' - scope.find_each(batch_size: batch) do |obj| - geocode_record.call(obj) + if limit + scope.each do |obj| + geocode_record.call(obj) + end + else + scope.find_each(batch_size: batch) do |obj| + geocode_record.call(obj) + end end end