diff --git a/lib/geocoder/lookups/bing.rb b/lib/geocoder/lookups/bing.rb index 0149d144e8b1087d7b1eb6ecf0d801e49fd1b810..e88495518aa9d6b62659eacdbb4d6d1d7a5aeef1 100644 --- a/lib/geocoder/lookups/bing.rb +++ b/lib/geocoder/lookups/bing.rb @@ -55,5 +55,19 @@ module Geocoder::Lookup key: configuration.api_key }.merge(super) end + + 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") + end + end end end diff --git a/test/fixtures/bing_over_limit b/test/fixtures/bing_over_limit new file mode 100644 index 0000000000000000000000000000000000000000..43135d615292b80f2776dffa35abd04b41400909 --- /dev/null +++ b/test/fixtures/bing_over_limit @@ -0,0 +1,16 @@ +{ + "authenticationResultCode":"ValidCredentials", + "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png", + "copyright":"Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.", + "resourceSets":[ + { + "estimatedTotal":0, + "resources":[ + + ] + } + ], + "statusCode":200, + "statusDescription":"OK", + "traceId":"907b76a307bc49129a489de3d4c992ea|CH1M001463|02.00.82.2800|CH1MSNVM001383, CH1MSNVM001358, CH1MSNVM001397" +} diff --git a/test/test_helper.rb b/test/test_helper.rb index fa73e12b4cf2d2ed1afd18907c40331d68f9c8b4..5ee0ede206179ec090f1937486669b7c6338b8fe 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -118,6 +118,19 @@ module Geocoder end end + class Bing + private + def read_fixture(file) + if file == "bing_over_limit" + 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}) + else + super + end + end + end + class GooglePremier private def fixture_prefix @@ -411,10 +424,11 @@ class GeocoderTestCase < Test::Unit::TestCase end class MockHttpResponse - attr_reader :code, :body + attr_reader :code, :body, :headers def initialize(options = {}) @code = options[:code].to_s @body = options[:body] + @headers = options[:headers] || {} end def [](key) diff --git a/test/unit/lookups/bing_test.rb b/test/unit/lookups/bing_test.rb index 9a63f90eb63c4f0c1bb8805061f98eecaf6a8eae..f7c988461b786e7f87efd0b01e466eca5a6b925e 100644 --- a/test/unit/lookups/bing_test.rb +++ b/test/unit/lookups/bing_test.rb @@ -65,4 +65,12 @@ class BingTest < GeocoderTestCase assert_match(/Locations\/uk\?q=manchester,%20lancashire/, url) assert_no_match(/query/, url) end + + def test_raises_exception_when_service_request_overloaded + Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError]) + l = Geocoder::Lookup.get(:bing) + assert_raises Geocoder::OverQueryLimitError do + l.send(:results, Geocoder::Query.new("over limit")) + end + end end