ruby-on-railsrubyrails-geocoder

Setup dynamic api keys for rails geocoder gem


I'm trying to implement a general 'application settings' page for my application. on this page I want my customers to be able to put their own google api key for the rails geocoder gem. I have read through the documentation but could not find any way to set this api key in a dynamic way (through a form).

Is there any way to do this dynamically so the client can use his own google api key?

Small note, every application will only have 1 api key, but I'm trying to do this dynamically so I won't have to think about it anymore down the line.


Solution

  • You could use Geocode.configure:

    Geocoder.configure(
         :timeout  => 5,
         :lookup   => :yandex,
         :api_key  => "2a9fsa983jaslfj982fjasd",
         :units    => :km
    )
    

    https://github.com/alexreisner/geocoder/blob/e45f8bd3173c28e03efbfaf03eab7c5afaf4621a/lib/geocoder/configuration.rb#L7