Skip to content

Customizing the geocoding configuration

Werner Robitza edited this page Oct 3, 2018 · 2 revisions

By default, the app uses the geocoder gem for geocoding, and uses Google's service for lookups. If you'd like to use a different lookup service supported by the geocoder gem, set it on line 2 of geocoder.rb.

If you'd like to cache geocoding requests to improve your app's performance, read the Caching section of the geocoder gem's README. Here's how you would set it up with Redis:

  • Add gem 'redis' to the Gemfile
  • Run bundle install from the command line
  • Replace the contents of geocoder.rb with this:
require 'redis'

REDIS = Redis.new(url: ENV['REDISTOGO_URL'])

Geocoder.configure(
  lookup: :google,
  cache: REDIS,
  always_raise: [
    Geocoder::OverQueryLimitError,
    Geocoder::RequestDenied,
    Geocoder::InvalidRequest,
    Geocoder::InvalidApiKey
  ]
)
  • Install the Redis To Go add-on on Heroku: heroku addons:add redistogo

If you want to test that the Redis cache is properly working in development:

  1. Install Redis on your machine (or use our Virtual Machine)

  2. Add the entry below to your config/application.yml, then stop and restart the server.

REDISTOGO_URL: redis://127.0.0.1:6379/0

To test that the cache is being populated, make a few location-based searches, such as http://localhost:8080/api/search?location=94403, then go to the Rails console and check the contents of the Redis store:

$ rails c
> REDIS.keys

You should get an array of geocoder entries that look like this:

["geocoder:http://maps.googleapis.com/maps/api/geocode/json?address=94403&language=en&sensor=false"]