From 49647fa32c4b63c543fc3e7727bc10d1a288a9aa Mon Sep 17 00:00:00 2001
From: Thu Trang Pham <thuutrangpham@gmail.com>
Date: Tue, 27 Jan 2015 12:51:27 -0500
Subject: [PATCH] Change bing to raise ServiceUnavailable instead of
 OverQueryLimitError

---
 lib/geocoder/exceptions.rb                                | 3 +++
 lib/geocoder/lookups/bing.rb                              | 5 ++---
 .../{bing_over_limit => bing_service_unavailable}         | 0
 test/test_helper.rb                                       | 2 +-
 test/unit/lookups/bing_test.rb                            | 8 ++++----
 5 files changed, 10 insertions(+), 8 deletions(-)
 rename test/fixtures/{bing_over_limit => bing_service_unavailable} (100%)

diff --git a/lib/geocoder/exceptions.rb b/lib/geocoder/exceptions.rb
index 01153aaf..7f6a081c 100644
--- a/lib/geocoder/exceptions.rb
+++ b/lib/geocoder/exceptions.rb
@@ -26,4 +26,7 @@ module Geocoder
   class InvalidApiKey < Error
   end
 
+  class ServiceUnavailable < Error
+  end
+
 end
diff --git a/lib/geocoder/lookups/bing.rb b/lib/geocoder/lookups/bing.rb
index e8849551..02860123 100644
--- a/lib/geocoder/lookups/bing.rb
+++ b/lib/geocoder/lookups/bing.rb
@@ -58,15 +58,14 @@ module Geocoder::Lookup
 
     def check_response_for_errors!(response)
       super
-      puts response
       if response.headers['X-MS-BM-WS-INFO'] == 1
         # Occasionally, the servers processing service requests can be overloaded, 
         # and you may receive some responses that contain no results for queries that 
         # you would normally receive a result. To identify this situation, 
         # check the HTTP headers of the response. If the HTTP header X-MS-BM-WS-INFO is set to 1, 
         # it is best to wait a few seconds and try again.
-        raise_error(Geocoder::OverQueryLimitError) ||
-          warn("Bing Geocoding API error: Service Requests Overloaded")
+        raise_error(Geocoder::ServiceUnavailable) ||
+          warn("Bing Geocoding API error: Service Unavailable")
       end   
     end
   end
diff --git a/test/fixtures/bing_over_limit b/test/fixtures/bing_service_unavailable
similarity index 100%
rename from test/fixtures/bing_over_limit
rename to test/fixtures/bing_service_unavailable
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 5ee0ede2..e00638be 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -121,7 +121,7 @@ module Geocoder
     class Bing
       private
       def read_fixture(file)
-        if file == "bing_over_limit"
+        if file == "bing_service_unavailable"
           filepath = File.join("test", "fixtures", file)
           s = File.read(filepath).strip.gsub(/\n\s*/, "")
           MockHttpResponse.new(body: s, code: "200", headers: {'X-MS-BM-WS-INFO' => 1})
diff --git a/test/unit/lookups/bing_test.rb b/test/unit/lookups/bing_test.rb
index f7c98846..68e45792 100644
--- a/test/unit/lookups/bing_test.rb
+++ b/test/unit/lookups/bing_test.rb
@@ -66,11 +66,11 @@ class BingTest < GeocoderTestCase
     assert_no_match(/query/, url)
   end
 
-  def test_raises_exception_when_service_request_overloaded
-    Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
+  def test_raises_exception_when_service_unavailable
+    Geocoder.configure(:always_raise => [Geocoder::ServiceUnavailable])
     l = Geocoder::Lookup.get(:bing)
-    assert_raises Geocoder::OverQueryLimitError do
-      l.send(:results, Geocoder::Query.new("over limit"))
+    assert_raises Geocoder::ServiceUnavailable do
+      l.send(:results, Geocoder::Query.new("service unavailable"))
     end
   end
 end
-- 
GitLab