Skip to content

Instantly share code, notes, and snippets.

@timrwilliams
timrwilliams / geocoder.rake
Created November 20, 2013 10:24
A rake task to geocode all objects of a particular class using the Ruby Geocoder gem, retrying 3 times if unsuccessful. This is primarily targetted at the Google Maps API which limits the number of requests per second. By building in an increasing back off before each retry we can ensure we recover from these temporary bans.
namespace :geocoder do
desc "Geocode all objects without coordinates."
task :all_with_retry => :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|
geocode_with_retry obj
@timrwilliams
timrwilliams / routes.rb
Created August 26, 2013 10:59
How to secure access to Resque admin page in Rails 3.2 by only allowing users with a specified role to access the /resque route. Non authorized users will receive 404 error.
# Assumes you have a role attribue on user model
authenticate :user, lambda {|u| u.role == 'super_admin' } do
mount Resque::Server.new, :at => "/resque"
end