From cdfb7e31e7fde285f1d5432e9e69f1cd8a2ae1ce Mon Sep 17 00:00:00 2001
From: Caue Guerra <caueguerra@gmail.com>
Date: Tue, 25 Feb 2014 17:05:53 -0500
Subject: [PATCH] adding over quota exception handling for nominatim

---
 lib/geocoder/lookups/nominatim.rb   | 8 ++++++++
 test/fixtures/nominatim_over_limit  | 1 +
 test/unit/lookups/nominatim_test.rb | 8 ++++++++
 3 files changed, 17 insertions(+)
 create mode 100644 test/fixtures/nominatim_over_limit

diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb
index 56bfa01e..4e594862 100644
--- a/lib/geocoder/lookups/nominatim.rb
+++ b/lib/geocoder/lookups/nominatim.rb
@@ -25,6 +25,14 @@ module Geocoder::Lookup
       doc.is_a?(Array) ? doc : [doc]
     end
 
+    def parse_raw_data(raw_data)
+      if raw_data.include?("Bandwidth limit exceeded")
+        raise_error(Geocoder::OverQueryLimitError) || warn("Over API query limit.")
+      else
+        super(raw_data)
+      end
+    end
+
     def query_url_params(query)
       params = {
         :format => "json",
diff --git a/test/fixtures/nominatim_over_limit b/test/fixtures/nominatim_over_limit
new file mode 100644
index 00000000..9b9f64d1
--- /dev/null
+++ b/test/fixtures/nominatim_over_limit
@@ -0,0 +1 @@
+<html>\n<head>\n<title>Bandwidth limit exceeded</title>\n</head>\n<body>\n<h1>Bandwidth limit exceeded</h1>\n\n<p>You have been temporarily blocked because you have been overusing OSM's geocoding service or because you have not provided sufficient identification of your application. This block will be automatically lifted after a while. Please take the time and adapt your scripts to reduce the number of requests and make sure that you send a valid UserAgent or Referer.</p>\n\n<p>For more information, consult the <a href=\"http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy\">usage policy</a> for the OSM Nominatim server.\n</body>\n</head>\n
diff --git a/test/unit/lookups/nominatim_test.rb b/test/unit/lookups/nominatim_test.rb
index ddab3d1f..ac89f86d 100644
--- a/test/unit/lookups/nominatim_test.rb
+++ b/test/unit/lookups/nominatim_test.rb
@@ -20,4 +20,12 @@ class NominatimTest < Test::Unit::TestCase
     query = Geocoder::Query.new("Bluffton, SC")
     assert_match %r(http://local\.com), query.url
   end
+
+  def test_raises_exception_when_over_query_limit
+    Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError])
+    l = Geocoder::Lookup.get(:nominatim)
+    assert_raises Geocoder::OverQueryLimitError do
+      l.send(:results, Geocoder::Query.new("over limit"))
+    end
+  end
 end
-- 
GitLab