Skip to content
Snippets Groups Projects
Commit 30a822e9 authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #794 from TrangPham/master

Handle Bing over limit when header 'X-MS-BM-WS-INFO' is set to 1
parents f42dbe37 62cfbe32
No related branches found
No related tags found
No related merge requests found
...@@ -26,4 +26,7 @@ module Geocoder ...@@ -26,4 +26,7 @@ module Geocoder
class InvalidApiKey < Error class InvalidApiKey < Error
end end
class ServiceUnavailable < Error
end
end end
...@@ -55,5 +55,18 @@ module Geocoder::Lookup ...@@ -55,5 +55,18 @@ module Geocoder::Lookup
key: configuration.api_key key: configuration.api_key
}.merge(super) }.merge(super)
end end
def check_response_for_errors!(response)
super
if response['x-ms-bm-ws-info'].to_i == 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::ServiceUnavailable) ||
warn("Bing Geocoding API error: Service Unavailable")
end
end
end end
end end
{
"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"
}
...@@ -118,6 +118,19 @@ module Geocoder ...@@ -118,6 +118,19 @@ module Geocoder
end end
end end
class Bing
private
def read_fixture(file)
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"})
else
super
end
end
end
class GooglePremier class GooglePremier
private private
def fixture_prefix def fixture_prefix
...@@ -415,9 +428,10 @@ class MockHttpResponse ...@@ -415,9 +428,10 @@ class MockHttpResponse
def initialize(options = {}) def initialize(options = {})
@code = options[:code].to_s @code = options[:code].to_s
@body = options[:body] @body = options[:body]
@headers = options[:headers] || {}
end end
def [](key) def [](key)
send key if respond_to?(key) @headers[key]
end end
end end
...@@ -65,4 +65,12 @@ class BingTest < GeocoderTestCase ...@@ -65,4 +65,12 @@ class BingTest < GeocoderTestCase
assert_match(/Locations\/uk\?q=manchester,%20lancashire/, url) assert_match(/Locations\/uk\?q=manchester,%20lancashire/, url)
assert_no_match(/query/, url) assert_no_match(/query/, url)
end end
def test_raises_exception_when_service_unavailable
Geocoder.configure(:always_raise => [Geocoder::ServiceUnavailable])
l = Geocoder::Lookup.get(:bing)
assert_raises Geocoder::ServiceUnavailable do
l.send(:results, Geocoder::Query.new("service unavailable"))
end
end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment