From 76b79440b28c7da0e9f8fee5fc9fee50ed913ec3 Mon Sep 17 00:00:00 2001
From: Sam Giffney <samuelgiffney@gmail.com>
Date: Wed, 16 May 2012 22:28:16 +1000
Subject: [PATCH] add cache_hit attribute to Geocoder Result to identify
 whether cache was used

---
 lib/geocoder/lookups/base.rb | 10 ++++++++--
 lib/geocoder/results/base.rb |  3 ++-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index d24b912a..c2a7528c 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -33,7 +33,11 @@ module Geocoder
         else
           reverse = false
         end
-        results(query, reverse).map{ |r| result_class.new(r) }
+        results(query, reverse).map{ |r| 
+          result = result_class.new(r)
+          result.cache_hit = @cache_hit if cache
+          result
+        }
       end
 
       ##
@@ -142,7 +146,9 @@ module Geocoder
         timeout(Geocoder::Configuration.timeout) do
           url = query_url(query, reverse)
           uri = URI.parse(url)
-          unless cache and body = cache[url]
+          if cache and body = cache[url]
+            @cache_hit = true
+          else
             client = http_client.new(uri.host, uri.port)
             client.use_ssl = true if Geocoder::Configuration.use_https
             response = client.get(uri.request_uri, Geocoder::Configuration.http_headers)
diff --git a/lib/geocoder/results/base.rb b/lib/geocoder/results/base.rb
index 645e3c2a..8a42413f 100644
--- a/lib/geocoder/results/base.rb
+++ b/lib/geocoder/results/base.rb
@@ -1,13 +1,14 @@
 module Geocoder
   module Result
     class Base
-      attr_accessor :data
+      attr_accessor :data, :cache_hit
 
       ##
       # Takes a hash of result data from a parsed Google result document.
       #
       def initialize(data)
         @data = data
+        @cache_hit = nil
       end
 
       ##
-- 
GitLab