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
Branches
Tags
No related merge requests found
......@@ -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
......
......@@ -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]
......
......@@ -14,6 +14,7 @@ class ErrorHandlingTest < Test::Unit::TestCase
Geocoder::Configuration.lookup = l
assert_nothing_raised { Geocoder.search("timeout") }
end
ensure
$VERBOSE = orig
end
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment