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

Fix caching for Yahoo lookup.

Allow lookups to define a cache key (defaults to the query URL) and
define Yahoo's as the non-OAuth-encoded request URL.
parent ef62bde0
No related branches found
No related tags found
No related merge requests found
...@@ -90,6 +90,16 @@ module Geocoder ...@@ -90,6 +90,16 @@ module Geocoder
fail fail
end 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 # Class of the result objects
# #
...@@ -150,7 +160,8 @@ module Geocoder ...@@ -150,7 +160,8 @@ module Geocoder
timeout(Geocoder::Configuration.timeout) do timeout(Geocoder::Configuration.timeout) do
url = query_url(query) url = query_url(query)
uri = URI.parse(url) uri = URI.parse(url)
if cache and body = cache[url] key = cache_key(query)
if cache and body = cache[key]
@cache_hit = true @cache_hit = true
else else
client = http_client.new(uri.host, uri.port) client = http_client.new(uri.host, uri.port)
...@@ -158,7 +169,7 @@ module Geocoder ...@@ -158,7 +169,7 @@ module Geocoder
response = client.get(uri.request_uri, Geocoder::Configuration.http_headers) response = client.get(uri.request_uri, Geocoder::Configuration.http_headers)
body = response.body body = response.body
if cache and (200..399).include?(response.code.to_i) if cache and (200..399).include?(response.code.to_i)
cache[url] = body cache[key] = body
end end
@cache_hit = false @cache_hit = false
end end
......
...@@ -34,9 +34,20 @@ module Geocoder::Lookup ...@@ -34,9 +34,20 @@ module Geocoder::Lookup
) )
end 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) def query_url(query)
base_url = "http://yboss.yahooapis.com/geo/placefinder?" parsed_url = URI.parse(raw_url(query))
parsed_url = URI.parse(base_url + url_query_string(query))
o = OauthUtil.new o = OauthUtil.new
o.consumer_key = Geocoder::Configuration.api_key[0] o.consumer_key = Geocoder::Configuration.api_key[0]
o.consumer_secret = Geocoder::Configuration.api_key[1] o.consumer_secret = Geocoder::Configuration.api_key[1]
......
...@@ -14,6 +14,7 @@ class ErrorHandlingTest < Test::Unit::TestCase ...@@ -14,6 +14,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
Geocoder::Configuration.lookup = l Geocoder::Configuration.lookup = l
assert_nothing_raised { Geocoder.search("timeout") } assert_nothing_raised { Geocoder.search("timeout") }
end end
ensure
$VERBOSE = orig $VERBOSE = orig
end end
......
...@@ -87,6 +87,7 @@ class ServicesTest < Test::Unit::TestCase ...@@ -87,6 +87,7 @@ class ServicesTest < Test::Unit::TestCase
# keep test output clean: suppress timeout warning # keep test output clean: suppress timeout warning
orig = $VERBOSE; $VERBOSE = nil orig = $VERBOSE; $VERBOSE = nil
assert_equal [], Geocoder.search("error") assert_equal [], Geocoder.search("error")
ensure
$VERBOSE = orig $VERBOSE = orig
end end
...@@ -110,6 +111,7 @@ class ServicesTest < Test::Unit::TestCase ...@@ -110,6 +111,7 @@ class ServicesTest < Test::Unit::TestCase
orig = $VERBOSE; $VERBOSE = nil orig = $VERBOSE; $VERBOSE = nil
Geocoder::Configuration.lookup = :yandex Geocoder::Configuration.lookup = :yandex
assert_equal [], Geocoder.search("invalid key") assert_equal [], Geocoder.search("invalid key")
ensure
$VERBOSE = orig $VERBOSE = orig
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