From 0e157f6794d1639db4ceebd19628b8812872413a Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Wed, 14 Dec 2011 21:22:20 -0500
Subject: [PATCH] Only cache response if successful.

---
 lib/geocoder/lookups/base.rb | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index e87dd634..cbf3f828 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -146,15 +146,16 @@ module Geocoder
         timeout(Geocoder::Configuration.timeout) do
           url = query_url(query, reverse)
           uri = URI.parse(url)
-          unless cache and response = cache[url]
+          unless cache and body = cache[url]
             client = http_client.new(uri.host, uri.port)
             client.use_ssl = true if Geocoder::Configuration.use_https
-            response = client.get(uri.request_uri).body
-            if cache
-              cache[url] = response
+            response = client.get(uri.request_uri)
+            body = response.body
+            if cache and (200..399).include?(response.code.to_i)
+              cache[url] = body
             end
           end
-          response
+          body
         end
       end
 
-- 
GitLab