diff --git a/lib/geocoder/cli.rb b/lib/geocoder/cli.rb index f564efa58dd1d97f8065aaf6d5331125da4e0470..74e5bb1ffe00fa771353b7efb946f92bfd4e7b59 100644 --- a/lib/geocoder/cli.rb +++ b/lib/geocoder/cli.rb @@ -79,7 +79,7 @@ module Geocoder if show_url q = Geocoder::Query.new(query) - out << q.lookup.send(:query_url, q) + "\n" + out << q.lookup.query_url(q) + "\n" exit 0 end diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 821a87b96a8e561e3cee28a7a7f95b5e0e1b2fe4..4120fc7041646ec1790557987060d03101e8b02e 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -66,6 +66,12 @@ module Geocoder [] end + ## + # URL to use for querying the geocoding engine. + # + def query_url(query) + fail + end private # ------------------------------------------------------------- @@ -113,13 +119,6 @@ module Geocoder ) end - ## - # URL to use for querying the geocoding engine. - # - def query_url(query) - fail - end - ## # Key to use for caching a geocoding result. Usually this will be the # request URL, but in cases where OAuth is used and the nonce, diff --git a/lib/geocoder/lookups/bing.rb b/lib/geocoder/lookups/bing.rb index 125dcdcf340e0e6913ebcfbdc86b4e73996703d1..441845086d3541dbeb060c4a745fb97fd8a187c2 100644 --- a/lib/geocoder/lookups/bing.rb +++ b/lib/geocoder/lookups/bing.rb @@ -16,6 +16,12 @@ module Geocoder::Lookup ["key"] end + def query_url(query) + "#{protocol}://dev.virtualearth.net/REST/v1/Locations" + + (query.reverse_geocode? ? "/#{query.sanitized_text}?" : "?") + + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -37,11 +43,5 @@ module Geocoder::Lookup :query => query.reverse_geocode? ? nil : query.sanitized_text ) end - - def query_url(query) - "#{protocol}://dev.virtualearth.net/REST/v1/Locations" + - (query.reverse_geocode? ? "/#{query.sanitized_text}?" : "?") + - url_query_string(query) - end end end diff --git a/lib/geocoder/lookups/freegeoip.rb b/lib/geocoder/lookups/freegeoip.rb index b94946a60a9f046ccfff54788cbaeabd4311247b..9efaaa4e377803e0b3c25a061debc6df4857c576 100644 --- a/lib/geocoder/lookups/freegeoip.rb +++ b/lib/geocoder/lookups/freegeoip.rb @@ -8,6 +8,10 @@ module Geocoder::Lookup "FreeGeoIP" end + def query_url(query) + "#{protocol}://freegeoip.net/json/#{query.sanitized_text}" + end + private # --------------------------------------------------------------- def parse_raw_data(raw_data) @@ -39,9 +43,5 @@ module Geocoder::Lookup "country_code" => "RD" } end - - def query_url(query) - "#{protocol}://freegeoip.net/json/#{query.sanitized_text}" - end end end diff --git a/lib/geocoder/lookups/geocoder_ca.rb b/lib/geocoder/lookups/geocoder_ca.rb index 01919a0f76cb60fd5b890f3f50fa96b2bab1df62..81e1f8506c641c512526f310624992cb46d5ab82 100644 --- a/lib/geocoder/lookups/geocoder_ca.rb +++ b/lib/geocoder/lookups/geocoder_ca.rb @@ -8,6 +8,10 @@ module Geocoder::Lookup "Geocoder.ca" end + def query_url(query) + "#{protocol}://geocoder.ca/?" + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -42,10 +46,6 @@ module Geocoder::Lookup params end - def query_url(query) - "#{protocol}://geocoder.ca/?" + url_query_string(query) - end - def parse_raw_data(raw_data) super raw_data[/^test\((.*)\)\;\s*$/, 1] end diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index 5f5e8fe69fb6f12d7bd627c69c0433086d906131..96387c45a7e0a469758c5a41e96188e0a203a5e3 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -12,6 +12,10 @@ module Geocoder::Lookup "http://maps.google.com/maps?q=#{coordinates.join(',')}" end + def query_url(query) + "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -54,10 +58,5 @@ module Geocoder::Lookup :key => configuration.api_key ) end - - def query_url(query) - "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query) - end end end - diff --git a/lib/geocoder/lookups/google_premier.rb b/lib/geocoder/lookups/google_premier.rb index 2bf63dc9d3b5098372fbf7bd7d512f6e43fbc598..d555c6325d2b19adb8da544fe928698727486480 100644 --- a/lib/geocoder/lookups/google_premier.rb +++ b/lib/geocoder/lookups/google_premier.rb @@ -14,6 +14,11 @@ module Geocoder::Lookup ["private key", "client", "channel"] end + def query_url(query) + path = "/maps/api/geocode/json?" + url_query_string(query) + "#{protocol}://maps.googleapis.com#{path}&signature=#{sign(path)}" + end + private # --------------------------------------------------------------- def query_url_params(query) @@ -24,11 +29,6 @@ module Geocoder::Lookup ) end - def query_url(query) - path = "/maps/api/geocode/json?" + url_query_string(query) - "#{protocol}://maps.googleapis.com#{path}&signature=#{sign(path)}" - end - def sign(string) raw_private_key = url_safe_base64_decode(configuration.api_key[0]) digest = OpenSSL::Digest::Digest.new('sha1') diff --git a/lib/geocoder/lookups/mapquest.rb b/lib/geocoder/lookups/mapquest.rb index 46cc8c233a29201ab9414d77804e155c56b9b096..a0e31f39a2204c62e34b14fd423542613ea26a10 100644 --- a/lib/geocoder/lookups/mapquest.rb +++ b/lib/geocoder/lookups/mapquest.rb @@ -13,8 +13,6 @@ module Geocoder::Lookup ["key"] end - private # --------------------------------------------------------------- - def query_url(query) key = configuration.api_key domain = key ? "www" : "open" @@ -22,6 +20,8 @@ module Geocoder::Lookup url + url_query_string(query) end + private # --------------------------------------------------------------- + def search_type(query) query.reverse_geocode? ? "reverse" : "address" end diff --git a/lib/geocoder/lookups/maxmind.rb b/lib/geocoder/lookups/maxmind.rb index 2e6cb3498f374ae1e768d42c49e19054347e29b4..ba41e40ed4c24fabc58f1f3eef1f4aefb00f6321 100644 --- a/lib/geocoder/lookups/maxmind.rb +++ b/lib/geocoder/lookups/maxmind.rb @@ -9,6 +9,10 @@ module Geocoder::Lookup "MaxMind" end + def query_url(query) + "#{protocol}://geoip3.maxmind.com/f?" + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -40,9 +44,5 @@ module Geocoder::Lookup :i => query.sanitized_text ) end - - def query_url(query) - "#{protocol}://geoip3.maxmind.com/f?" + url_query_string(query) - end end end diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb index 8fb7a6de32630c0bf668961e4ded5fce9d5e619e..aea86e4b7c36c81815e9fa66656e5e0b6ffbb16a 100644 --- a/lib/geocoder/lookups/nominatim.rb +++ b/lib/geocoder/lookups/nominatim.rb @@ -12,6 +12,12 @@ module Geocoder::Lookup "http://www.openstreetmap.org/?lat=#{coordinates[0]}&lon=#{coordinates[1]}&zoom=15&layers=M" end + def query_url(query) + method = query.reverse_geocode? ? "reverse" : "search" + host = configuration[:host] || "nominatim.openstreetmap.org" + "#{protocol}://#{host}/#{method}?" + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -35,11 +41,5 @@ module Geocoder::Lookup end params end - - def query_url(query) - method = query.reverse_geocode? ? "reverse" : "search" - host = configuration[:host] || "nominatim.openstreetmap.org" - "#{protocol}://#{host}/#{method}?" + url_query_string(query) - end end end diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index 77a658b4f3e0a05961eecce51d5e244cdf88036a..a271b776bf4afad90c30ce42ba3dd65531588896 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -17,6 +17,14 @@ module Geocoder::Lookup ["consumer key", "consumer secret"] end + def query_url(query) + parsed_url = URI.parse(raw_url(query)) + o = OauthUtil.new + o.consumer_key = configuration.api_key[0] + o.consumer_secret = configuration.api_key[1] + base_url + o.sign(parsed_url).query_string + end + private # --------------------------------------------------------------- def results(query) @@ -72,13 +80,5 @@ module Geocoder::Lookup def raw_url(query) base_url + url_query_string(query) end - - def query_url(query) - parsed_url = URI.parse(raw_url(query)) - o = OauthUtil.new - o.consumer_key = configuration.api_key[0] - o.consumer_secret = configuration.api_key[1] - base_url + o.sign(parsed_url).query_string - end end end diff --git a/lib/geocoder/lookups/yandex.rb b/lib/geocoder/lookups/yandex.rb index 8d76a434dada1b2efdd006f362f1e9fc4a4e91a5..9e93d50f8755b1f8761a561d672ac1eb76a875cb 100644 --- a/lib/geocoder/lookups/yandex.rb +++ b/lib/geocoder/lookups/yandex.rb @@ -12,6 +12,10 @@ module Geocoder::Lookup "http://maps.yandex.ru/?ll=#{coordinates.reverse.join(',')}" end + def query_url(query) + "#{protocol}://geocode-maps.yandex.ru/1.x/?" + url_query_string(query) + end + private # --------------------------------------------------------------- def results(query) @@ -46,9 +50,5 @@ module Geocoder::Lookup :key => configuration.api_key ) end - - def query_url(query) - "#{protocol}://geocode-maps.yandex.ru/1.x/?" + url_query_string(query) - end end end diff --git a/test/https_test.rb b/test/https_test.rb index 3a922bf4d63278a2608bf3c288d33f6c09b2f73d..dd6d02040aca3e132007d85ccd2b02e82a010098 100644 --- a/test/https_test.rb +++ b/test/https_test.rb @@ -6,11 +6,11 @@ class HttpsTest < Test::Unit::TestCase def test_uses_https_for_secure_query Geocoder.configure(:use_https => true) g = Geocoder::Lookup::Google.new - assert_match /^https:/, g.send(:query_url, Geocoder::Query.new("test")) + assert_match /^https:/, g.query_url(Geocoder::Query.new("test")) end def test_uses_http_by_default g = Geocoder::Lookup::Google.new - assert_match /^http:/, g.send(:query_url, Geocoder::Query.new("test")) + assert_match /^http:/, g.query_url(Geocoder::Query.new("test")) end end diff --git a/test/lookup_test.rb b/test/lookup_test.rb index 3fb9373b1e7b7fe0efa04ba32d853adf24f2d9cc..eeba7ba5d43e986a19ffa54b2855f3c566fcd114 100644 --- a/test/lookup_test.rb +++ b/test/lookup_test.rb @@ -24,7 +24,7 @@ class LookupTest < Test::Unit::TestCase Geocoder::Lookup.all_services_except_test.each do |l| next if l == :freegeoip # does not use query string set_api_key!(l) - url = Geocoder::Lookup.get(l).send(:query_url, Geocoder::Query.new( + url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new( "test", :params => {:one_in_the_hand => "two in the bush"} )) # should be "+"s for all lookups except Yahoo @@ -72,13 +72,13 @@ class LookupTest < Test::Unit::TestCase def test_google_api_key Geocoder.configure(:api_key => "MY_KEY") g = Geocoder::Lookup::Google.new - assert_match "key=MY_KEY", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States")) + assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States")) end def test_geocoder_ca_showpostal Geocoder.configure(:api_key => "MY_KEY") g = Geocoder::Lookup::GeocoderCa.new - assert_match "showpostal=1", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States")) + assert_match "showpostal=1", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States")) end def test_raises_configuration_error_on_missing_key diff --git a/test/services_test.rb b/test/services_test.rb index 3e2d596bfae75f8eae24b0a3fc340528b1338e03..30968707c102283dddbf4c7d837af518859e3866 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -35,7 +35,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_query_url_contains_bounds lookup = Geocoder::Lookup::Google.new - url = lookup.send(:query_url, Geocoder::Query.new( + url = lookup.query_url(Geocoder::Query.new( "Some Intersection", :bounds => [[40.0, -120.0], [39.0, -121.0]] )) @@ -44,7 +44,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_query_url_contains_region lookup = Geocoder::Lookup::Google.new - url = lookup.send(:query_url, Geocoder::Query.new( + url = lookup.query_url(Geocoder::Query.new( "Some Intersection", :region => "gb" )) @@ -53,7 +53,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_query_url_contains_components_when_given_as_string lookup = Geocoder::Lookup::Google.new - url = lookup.send(:query_url, Geocoder::Query.new( + url = lookup.query_url(Geocoder::Query.new( "Some Intersection", :components => "locality:ES" )) @@ -63,7 +63,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_query_url_contains_components_when_given_as_array lookup = Geocoder::Lookup::Google.new - url = lookup.send(:query_url, Geocoder::Query.new( + url = lookup.query_url(Geocoder::Query.new( "Some Intersection", :components => ["country:ES", "locality:ES"] )) @@ -84,7 +84,7 @@ class ServicesTest < Test::Unit::TestCase def test_google_premier_query_url Geocoder.configure(:api_key => ["deadbeef", "gme-test", "test-dev"]) assert_equal "http://maps.googleapis.com/maps/api/geocode/json?address=Madison+Square+Garden%2C+New+York%2C+NY&channel=test-dev&client=gme-test&language=en&sensor=false&signature=doJvJqX7YJzgV9rJ0DnVkTGZqTg=", - Geocoder::Lookup::GooglePremier.new.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY")) + Geocoder::Lookup::GooglePremier.new.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY")) end @@ -205,7 +205,7 @@ class ServicesTest < Test::Unit::TestCase Geocoder.configure(:lookup => :nominatim, :nominatim => {:host => "local.com"}) lookup = Geocoder::Lookup::Nominatim.new query = Geocoder::Query.new("Bluffton, SC") - assert_match %r(http://local\.com), lookup.send(:query_url, query) + assert_match %r(http://local\.com), lookup.query_url(query) end # --- MapQuest --- @@ -214,7 +214,7 @@ class ServicesTest < Test::Unit::TestCase Geocoder.configure(:lookup => :mapquest, :api_key => "abc123") lookup = Geocoder::Lookup::Mapquest.new query = Geocoder::Query.new("Bluffton, SC") - res = lookup.send(:query_url, query) + res = lookup.query_url(query) assert_equal "http://www.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC", res end