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

Enable lookup-specific configuration.

parent 641b5ffe
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,20 @@ module Geocoder ...@@ -27,6 +27,20 @@ module Geocoder
Configuration.instance.data = value Configuration.instance.data = value
end end
def self.config_for_lookup(lookup_name)
data = config.select{ |key,value| Configuration::OPTIONS.include? key }
if config.has_key?(lookup_name)
data.merge!(config[lookup_name])
end
# allow method-like access
data.instance_eval do
def method_missing(meth, *args, &block)
has_key?(meth) ? self[meth] : super
end
end
data
end
## ##
# This class handles geocoder Geocoder configuration # This class handles geocoder Geocoder configuration
# (geocoding service provider, caching, units of measurement, etc). # (geocoding service provider, caching, units of measurement, etc).
......
...@@ -23,6 +23,14 @@ module Geocoder ...@@ -23,6 +23,14 @@ module Geocoder
fail fail
end end
##
# Symbol which is used in configuration to refer to this Lookup.
#
def handle
str = self.class.to_s
str[str.rindex(':')+1..-1].gsub(/([a-z\d]+)([A-Z])/,'\1_\2').downcase.to_sym
end
## ##
# Query the geocoding API and return a Geocoder::Result object. # Query the geocoding API and return a Geocoder::Result object.
# Returns +nil+ on timeout or error. # Returns +nil+ on timeout or error.
...@@ -65,7 +73,7 @@ module Geocoder ...@@ -65,7 +73,7 @@ module Geocoder
# An object with configuration data for this particular lookup. # An object with configuration data for this particular lookup.
# #
def configuration def configuration
Geocoder::Configuration Geocoder.config_for_lookup(handle)
end end
## ##
...@@ -203,14 +211,14 @@ module Geocoder ...@@ -203,14 +211,14 @@ module Geocoder
timeout(configuration.timeout) do timeout(configuration.timeout) do
uri = URI.parse(query_url(query)) uri = URI.parse(query_url(query))
client = http_client.new(uri.host, uri.port) client = http_client.new(uri.host, uri.port)
client.use_ssl = true if Geocoder::Configuration.use_https client.use_ssl = true if configuration.use_https
client.get(uri.request_uri, Geocoder::Configuration.http_headers) client.get(uri.request_uri, configuration.http_headers)
end end
end end
def check_api_key_configuration!(query) def check_api_key_configuration!(query)
key_parts = query.lookup.required_api_key_parts key_parts = query.lookup.required_api_key_parts
if key_parts.size > Array(Geocoder::Configuration.api_key).size if key_parts.size > Array(configuration.api_key).size
parts_string = key_parts.size == 1 ? key_parts.first : key_parts parts_string = key_parts.size == 1 ? key_parts.first : key_parts
raise Geocoder::ConfigurationError, raise Geocoder::ConfigurationError,
"The #{query.lookup.name} API requires a key to be configured: " + "The #{query.lookup.name} API requires a key to be configured: " +
......
...@@ -34,6 +34,18 @@ class ConfigurationTest < Test::Unit::TestCase ...@@ -34,6 +34,18 @@ class ConfigurationTest < Test::Unit::TestCase
assert_equal :test, Geocoder.config[:units] assert_equal :test, Geocoder.config[:units]
end end
def test_config_for_lookup
Geocoder.config = {
:timeout => 5,
:api_key => "aaa",
:google => {
:timeout => 2
}
}
assert_equal 2, Geocoder.config_for_lookup(:google)[:timeout]
assert_equal "aaa", Geocoder.config_for_lookup(:google)[:api_key]
end
def test_model_configuration def test_model_configuration
Landmark.reverse_geocoded_by :latitude, :longitude, :method => :spherical, :units => :km Landmark.reverse_geocoded_by :latitude, :longitude, :method => :spherical, :units => :km
assert_equal :km, Landmark.geocoder_options[:units] assert_equal :km, Landmark.geocoder_options[:units]
......
...@@ -43,4 +43,9 @@ class LookupTest < Test::Unit::TestCase ...@@ -43,4 +43,9 @@ class LookupTest < Test::Unit::TestCase
Geocoder.search("Madison Square Garden, New York, NY 10001, United States") Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
end end
end end
def test_handle
assert_equal :google, Geocoder::Lookup::Google.new.handle
assert_equal :geocoder_ca, Geocoder::Lookup::GeocoderCa.new.handle
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