diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb index 56bfa01e09d0320a91ac9daf0f05bd4f2e726322..4e594862bacf836b0433d9bfa20ddcd72a3e48e0 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 0000000000000000000000000000000000000000..9b9f64d101860b2e098573d63a815d06b8ce2cb7 --- /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 246dd54dfd75b805149aa2e6b194c772ed58472e..44351343cbe8c89945ea381e027a6dbfb3adef5c 100644 --- a/test/unit/lookups/nominatim_test.rb +++ b/test/unit/lookups/nominatim_test.rb @@ -20,4 +20,12 @@ class NominatimTest < GeocoderTestCase 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