diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 7c80494a07050aaa74061c2681ff778d52a64328..2db8d11e7b0ff31fff3013dcd531a4ad15bd4dab 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -90,6 +90,16 @@ module Geocoder 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, + # timestamp, etc varies from one request to another, we need to use + # something else (like the URL before OAuth encoding). + # + def cache_key(query) + query_url(query) + end + ## # Class of the result objects # @@ -150,7 +160,8 @@ module Geocoder timeout(Geocoder::Configuration.timeout) do url = query_url(query) uri = URI.parse(url) - if cache and body = cache[url] + key = cache_key(query) + if cache and body = cache[key] @cache_hit = true else client = http_client.new(uri.host, uri.port) @@ -158,7 +169,7 @@ module Geocoder response = client.get(uri.request_uri, Geocoder::Configuration.http_headers) body = response.body if cache and (200..399).include?(response.code.to_i) - cache[url] = body + cache[key] = body end @cache_hit = false end diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index b4cd2dbd5e38df7adc9062992b48c562204b7abc..548b217ada9de1218b083885c0cdb5ef7eb539a0 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -34,9 +34,20 @@ module Geocoder::Lookup ) end + def cache_key(query) + raw_url(query) + end + + def base_url + "http://yboss.yahooapis.com/geo/placefinder?" + end + + def raw_url(query) + base_url + url_query_string(query) + end + def query_url(query) - base_url = "http://yboss.yahooapis.com/geo/placefinder?" - parsed_url = URI.parse(base_url + url_query_string(query)) + parsed_url = URI.parse(raw_url(query)) o = OauthUtil.new o.consumer_key = Geocoder::Configuration.api_key[0] o.consumer_secret = Geocoder::Configuration.api_key[1] diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb index 369613e69540d3c33a039fd8ccf35800ee2445a3..002425b63c182433487607d7aab20846671666fb 100644 --- a/test/error_handling_test.rb +++ b/test/error_handling_test.rb @@ -14,6 +14,7 @@ class ErrorHandlingTest < Test::Unit::TestCase Geocoder::Configuration.lookup = l assert_nothing_raised { Geocoder.search("timeout") } end + ensure $VERBOSE = orig end diff --git a/test/services_test.rb b/test/services_test.rb index 19898d699a8b38946628879bce76fba1e22ce0cf..1dbe8b16eba70b7a60832c90d3ac1dbebbb17976 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -87,6 +87,7 @@ class ServicesTest < Test::Unit::TestCase # keep test output clean: suppress timeout warning orig = $VERBOSE; $VERBOSE = nil assert_equal [], Geocoder.search("error") + ensure $VERBOSE = orig end @@ -110,6 +111,7 @@ class ServicesTest < Test::Unit::TestCase orig = $VERBOSE; $VERBOSE = nil Geocoder::Configuration.lookup = :yandex assert_equal [], Geocoder.search("invalid key") + ensure $VERBOSE = orig end