From dd61291a6029906ed149d31c15b2c7b3b41580ff Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Mon, 7 Mar 2011 09:43:42 -0500 Subject: [PATCH] Prevent exception when no results found. Be sure to return nil. --- lib/geocoder/lookups/google.rb | 2 +- lib/geocoder/lookups/yahoo.rb | 2 +- test/fixtures/google_no_results.json | 4 ++++ test/fixtures/yahoo_no_results.json | 10 ++++++++++ test/geocoder_test.rb | 9 +++++++++ test/test_helper.rb | 6 ++++-- 6 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/google_no_results.json create mode 100644 test/fixtures/yahoo_no_results.json diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index ed263c97..88764dce 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 e8665fa0..3d8e112f 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 00000000..afa9ac14 --- /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 00000000..e97865df --- /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 01cbf011..f4b1d848 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 865ee7c2..a283bb2d 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 -- GitLab