Skip to content
Snippets Groups Projects
Commit a4e6f662 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #1055 from alexreisner/configuration_merging

Add Method for Merging Lookup Configuration
parents 83417eb7 84bfcab9
No related branches found
No related tags found
No related merge requests found
...@@ -38,6 +38,14 @@ module Geocoder ...@@ -38,6 +38,14 @@ module Geocoder
data data
end end
##
# Merge the given hash into a lookup's existing configuration.
#
def self.merge_into_lookup_config(lookup_name, options)
base = Geocoder.config[lookup_name]
Geocoder.configure(lookup_name => base.merge(options))
end
class Configuration class Configuration
include Singleton include Singleton
......
...@@ -48,7 +48,7 @@ module Geocoder::Lookup ...@@ -48,7 +48,7 @@ module Geocoder::Lookup
end end
def token def token
fetch_and_save_token! if !valid_token_configured? and configuration.api_key create_and_save_token! if !valid_token_configured? and configuration.api_key
configuration[:token].to_s unless configuration[:token].nil? configuration[:token].to_s unless configuration[:token].nil?
end end
...@@ -56,9 +56,16 @@ module Geocoder::Lookup ...@@ -56,9 +56,16 @@ module Geocoder::Lookup
!configuration[:token].nil? and configuration[:token].active? !configuration[:token].nil? and configuration[:token].active?
end end
def fetch_and_save_token! def create_and_save_token!
token_instance = Geocoder::EsriToken.generate_token(*configuration.api_key) save_token!(create_token)
Geocoder.configure(:esri => Geocoder.config[:esri].merge({:token => token_instance})) end
def create_token
Geocoder::EsriToken.generate_token(*configuration.api_key)
end
def save_token!(token_instance)
Geocoder.merge_into_lookup_config(:esri, token: token_instance)
end end
end end
end end
...@@ -51,4 +51,23 @@ class ConfigurationTest < GeocoderTestCase ...@@ -51,4 +51,23 @@ class ConfigurationTest < GeocoderTestCase
# method option > per-model configuration # method option > per-model configuration
assert_equal 111, v.distance_to([0,1], :km).round assert_equal 111, v.distance_to([0,1], :km).round
end end
def test_merge_into_lookup_config
base = {
timeout: 5,
api_key: "xxx"
}
new = {
timeout: 10,
units: :km,
}
merged = {
timeout: 10, # overwritten
units: :km, # added
api_key: "xxx" # preserved
}
Geocoder.configure(google: base)
Geocoder.merge_into_lookup_config(:google, new)
assert_equal merged, Geocoder.config[:google]
end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment