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

Refactor: define config methods dynamically.

parent 8fea0389
Branches
Tags
No related merge requests found
module Geocoder
class Configuration
def self.options_and_defaults
[
# geocoding service timeout (secs)
def self.timeout; @@timeout; end
def self.timeout=(obj); @@timeout = obj; end
[:timeout, 3],
# name of geocoding service (symbol)
def self.lookup; @@lookup; end
def self.lookup=(obj); @@lookup = obj; end
[:lookup, :google],
# ISO-639 language code
def self.language; @@language; end
def self.language=(obj); @@language = obj; end
[:language, :en],
# use HTTPS for lookup requests? (if supported)
[:use_https, false],
# app id (if using Yahoo geocoding service)
def self.yahoo_appid; @@yahoo_appid; end
def self.yahoo_appid=(obj); @@yahoo_appid = obj; end
[:yahoo_appid, nil],
# API key (if using Google geocoding service)
def self.google_api_key; @@google_api_key; end
def self.google_api_key=(obj); @@google_api_key = obj; end
[:google_api_key, nil],
# cache object (must respond to #[], #[]=, and #keys)
[:cache, nil],
# cache object (must respond to #[], #[]=, and #keys
def self.cache; @@cache; end
def self.cache=(obj); @@cache = obj; end
# prefix (string) to use for all cache keys
[:cache_prefix, "geocoder:"]
]
end
# cache object (must respond to #[], #[]=, and #keys
def self.cache_prefix; @@cache_prefix; end
def self.cache_prefix=(obj); @@cache_prefix = obj; end
# define getters and setters for all configuration settings
self.options_and_defaults.each do |o,d|
eval("def self.#{o}; @@#{o}; end")
eval("def self.#{o}=(obj); @@#{o} = obj; end")
end
# use HTTPS for lookup requests? (true or false)
def self.use_https; @@use_https; end
def self.use_https=(obj); @@use_https = obj; end
##
# Set all values to default.
#
def self.set_defaults
self.options_and_defaults.each do |o,d|
self.send("#{o}=", d)
end
end
end
end
Geocoder::Configuration.timeout = 3
Geocoder::Configuration.lookup = :google
Geocoder::Configuration.language = :en
Geocoder::Configuration.yahoo_appid = nil
Geocoder::Configuration.google_api_key = nil
Geocoder::Configuration.cache = nil
Geocoder::Configuration.cache_prefix = "geocoder:"
Geocoder::Configuration.use_https = false
Geocoder::Configuration.set_defaults
......@@ -3,8 +3,7 @@ require 'test_helper'
class GeocoderTest < Test::Unit::TestCase
def setup
Geocoder::Configuration.lookup = :google
Geocoder::Configuration.use_https = false
Geocoder::Configuration.set_defaults
end
......@@ -232,11 +231,10 @@ class GeocoderTest < Test::Unit::TestCase
assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
end
def test_has_to_query_with_google_api_key
def test_google_api_key
Geocoder::Configuration.google_api_key = "MY_KEY"
g = Geocoder::Lookup::Google.new
assert_match "key=MY_KEY", g.send(:query_url, {:a => 1, :b => 2})
Geocoder::Configuration.google_api_key = nil
assert_match "key=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment