diff --git a/README.md b/README.md index d8545990ee2caca05b2e9bb78e35b9748f67b9b9..af147a145422922bba03e3d1dd22561796183a9c 100644 --- a/README.md +++ b/README.md @@ -517,7 +517,7 @@ The [Google Places Details API](https://developers.google.com/places/documentati #### Here/Nokia (`:here`) -* **API key**: required +* **API key**: required (set `Geocoder.configure(:api_key => [app_id, app_code])`) * **Quota**: Depending on the API key * **Region**: world * **SSL support**: yes diff --git a/lib/geocoder/lookups/bing.rb b/lib/geocoder/lookups/bing.rb index 40f0d4692505d8d1d9d4e02893462f719efc5a14..53aaadf57e2b9030645de5b1e0f38c98d3a2979f 100644 --- a/lib/geocoder/lookups/bing.rb +++ b/lib/geocoder/lookups/bing.rb @@ -58,15 +58,23 @@ module Geocoder::Lookup 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. + if server_overloaded?(response) raise_error(Geocoder::ServiceUnavailable) || Geocoder.log(:warn, "Bing Geocoding API error: Service Unavailable") end end + + def valid_response?(response) + super(response) and not server_overloaded?(response) + end + + def server_overloaded?(response) + # 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. + response['x-ms-bm-ws-info'].to_i == 1 + end end end diff --git a/lib/geocoder/lookups/here.rb b/lib/geocoder/lookups/here.rb index 7083bedfabf2b11079014b39713fb627f7166c38..54148fbd426fe8eff62c458305fa6b6debedfd9b 100644 --- a/lib/geocoder/lookups/here.rb +++ b/lib/geocoder/lookups/here.rb @@ -9,7 +9,7 @@ module Geocoder::Lookup end def required_api_key_parts - [] + ["app_id", "app_code"] end def query_url(query) diff --git a/test/unit/cache_test.rb b/test/unit/cache_test.rb index 21954398eff513359a13aa58b611a0a9369cf096..0fac933b92c7caa7d4a7b951ad235b25509d0311 100644 --- a/test/unit/cache_test.rb +++ b/test/unit/cache_test.rb @@ -2,6 +2,17 @@ require 'test_helper' class CacheTest < GeocoderTestCase + def setup + @tempfile = Tempfile.new("log") + @logger = Logger.new(@tempfile.path) + Geocoder.configure(logger: @logger) + end + + def teardown + Geocoder.configure(logger: :kernel) + @logger.close + @tempfile.close + end def test_second_occurrence_of_request_is_cache_hit Geocoder.configure(:cache => {}) @@ -33,4 +44,16 @@ class CacheTest < GeocoderTestCase end assert_equal false, lookup.instance_variable_get(:@cache_hit) end + + def test_bing_service_unavailable_without_raising_does_not_hit_cache + Geocoder.configure(cache: {}, lookup: :bing, always_raise: []) + set_api_key!(:bing) + lookup = Geocoder::Lookup.get(:bing) + + Geocoder.search("service unavailable") + assert_false lookup.instance_variable_get(:@cache_hit) + + Geocoder.search("service unavailable") + assert_false lookup.instance_variable_get(:@cache_hit) + end end diff --git a/test/unit/geocoder_test.rb b/test/unit/geocoder_test.rb index 7dc4e0f5a4860fc44680a6aa500871970a8fd545..8dda2ba224b1e58fb92cdce005b0bc929ea96426 100644 --- a/test/unit/geocoder_test.rb +++ b/test/unit/geocoder_test.rb @@ -60,18 +60,18 @@ class GeocoderTest < GeocoderTestCase def test_geocode_with_custom_lookup_param v = PlaceWithCustomLookup.new(*geocoded_object_params(:msg)) v.geocode - assert_equal Geocoder::Result::Nominatim, v.result_class + assert_equal "Geocoder::Result::Nominatim", v.result_class.to_s end def test_geocode_with_custom_lookup_proc_param v = PlaceWithCustomLookupProc.new(*geocoded_object_params(:msg)) v.geocode - assert_equal Geocoder::Result::Nominatim, v.result_class + assert_equal "Geocoder::Result::Nominatim", v.result_class.to_s end def test_reverse_geocode_with_custom_lookup_param v = PlaceReverseGeocodedWithCustomLookup.new(*reverse_geocoded_object_params(:msg)) v.reverse_geocode - assert_equal Geocoder::Result::Nominatim, v.result_class + assert_equal "Geocoder::Result::Nominatim", v.result_class.to_s end end diff --git a/test/unit/lookup_test.rb b/test/unit/lookup_test.rb index 393a42bb8177dbf3a61aff570b6db7dc13cacdaf..5b6b4605d7d5a1e85d9341c169d3518c9e8629fc 100644 --- a/test/unit/lookup_test.rb +++ b/test/unit/lookup_test.rb @@ -12,10 +12,10 @@ class LookupTest < GeocoderTestCase end def test_search_returns_empty_array_when_no_results - silence_warnings do - Geocoder::Lookup.all_services_except_test.each do |l| - lookup = Geocoder::Lookup.get(l) - set_api_key!(l) + Geocoder::Lookup.all_services_except_test.each do |l| + lookup = Geocoder::Lookup.get(l) + set_api_key!(l) + silence_warnings do assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")), "Lookup #{l} does not return empty array when no results." end