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