diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index ed263c9782a16da9d359067491ed5689d4ee44d2..88764dceea69be2e0e1de1523b432b1656a6b257 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -8,7 +8,7 @@ module Geocoder::Lookup def result(query, reverse = false) doc = fetch_data(query, reverse) - case doc['status']; when "OK" + case doc['status']; when "OK" # OK status implies >0 results doc['results'].first when "OVER_QUERY_LIMIT" warn "Google Geocoding API error: over query limit." diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index e8665fa0465515e13d7b3d7305472527b99ba6fd..3d8e112fa0e6da62991eaa86d16c9ff034fe3d8e 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -9,7 +9,7 @@ module Geocoder::Lookup def result(query, reverse = false) doc = fetch_data(query, reverse) if doc = doc['ResultSet'] and doc['Error'] == 0 - doc['Results'].first + doc['Results'].first if doc['Found'] > 0 else warn "Yahoo Geocoding API error: #{doc['Error']} (#{doc['ErrorMessage']})." end diff --git a/test/fixtures/google_no_results.json b/test/fixtures/google_no_results.json new file mode 100644 index 0000000000000000000000000000000000000000..afa9ac144c535650c2131bc54426c885f599f93e --- /dev/null +++ b/test/fixtures/google_no_results.json @@ -0,0 +1,4 @@ +{ + "status": "ZERO_RESULTS", + "results": [ ] +} diff --git a/test/fixtures/yahoo_no_results.json b/test/fixtures/yahoo_no_results.json new file mode 100644 index 0000000000000000000000000000000000000000..e97865dfcf60fb4e970caac77d2187f3eadf640d --- /dev/null +++ b/test/fixtures/yahoo_no_results.json @@ -0,0 +1,10 @@ +{ + "ResultSet":{ + "version":"1.0", + "Error":0, + "ErrorMessage":"No error", + "Locale":"us_US", + "Quality":10, + "Found":0 + } +} diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 01cbf01147e40a0ebc7c5e506982c20164248f1a..f4b1d8488132359802558c0260c864aaf054aa08 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -95,6 +95,10 @@ class GeocoderTest < Test::Unit::TestCase assert_result_has_required_attributes(result) end + def test_google_returns_nil_when_no_results + assert_nil Geocoder.search("no results") + end + # --- Yahoo --- @@ -117,6 +121,11 @@ class GeocoderTest < Test::Unit::TestCase assert_result_has_required_attributes(result) end + def test_yahoo_returns_nil_when_no_results + Geocoder::Configuration.lookup = :yahoo + assert_nil Geocoder.search("no results") + end + # --- FreeGeoIp --- diff --git a/test/test_helper.rb b/test/test_helper.rb index 865ee7c252a2059190de5eeed9558c5549ec1e68..a283bb2db34731835bdf8de6dc4a7026aef423b4 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -50,14 +50,16 @@ module Geocoder class Google < Base private #----------------------------------------------------------------- def fetch_raw_data(query, reverse = false) - File.read(File.join("test", "fixtures", "google_madison_square_garden.json")) + file = query == "no results" ? :no_results : :madison_square_garden + File.read(File.join("test", "fixtures", "google_#{file}.json")) end end class Yahoo < Base private #----------------------------------------------------------------- def fetch_raw_data(query, reverse = false) - File.read(File.join("test", "fixtures", "yahoo_madison_square_garden.json")) + file = query == "no results" ? :no_results : :madison_square_garden + File.read(File.join("test", "fixtures", "yahoo_#{file}.json")) end end