From 2e4413ca4340c0adc67631767e947beb75ee01f1 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Tue, 6 Nov 2012 19:14:37 -0500
Subject: [PATCH] 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.
---
 lib/geocoder/lookups/base.rb  | 15 +++++++++++++--
 lib/geocoder/lookups/yahoo.rb | 15 +++++++++++++--
 test/error_handling_test.rb   |  1 +
 test/services_test.rb         |  2 ++
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index 7c80494a..2db8d11e 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 b4cd2dbd..548b217a 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 369613e6..002425b6 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 19898d69..1dbe8b16 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
 
-- 
GitLab