From dee81642eac9613c9a1de99aba5e39b7e1035876 Mon Sep 17 00:00:00 2001 From: Jeff Keen <jeff@keen.me> Date: Sat, 7 May 2011 20:57:58 -0500 Subject: [PATCH] Return nil when Google provides no city. Fixes #56 --- lib/geocoder/results/google.rb | 1 + test/fixtures/google_no_city_data.json | 44 ++++++++++++++++++++++++++ test/geocoder_test.rb | 4 +++ test/test_helper.rb | 7 ++-- 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/google_no_city_data.json diff --git a/lib/geocoder/results/google.rb b/lib/geocoder/results/google.rb index de99a002..cb45785f 100644 --- a/lib/geocoder/results/google.rb +++ b/lib/geocoder/results/google.rb @@ -20,6 +20,7 @@ module Geocoder::Result return entity['long_name'] end end + return nil # no appropriate components found end def state diff --git a/test/fixtures/google_no_city_data.json b/test/fixtures/google_no_city_data.json new file mode 100644 index 00000000..72c52808 --- /dev/null +++ b/test/fixtures/google_no_city_data.json @@ -0,0 +1,44 @@ +{ + "status": "OK", + "results": [ { + "types": [ "postal_code" ], + "formatted_address": "55100, Turkey", + "address_components": [{ + "long_name": "55100", + "short_name": "55100", + "types": ["postal_code"] + }, + { + "long_name": "Turkey", + "short_name": "TR", + "types": ["country", "political"] + }], + "geometry": { + "location": { + "lat": 41.3112221, + "lng": 36.3322118 + }, + "location_type": "APPROXIMATE", + "viewport": { + "southwest": { + "lat": 41.2933411, + "lng": 36.3066331 + }, + "northeast": { + "lat": 41.3291031, + "lng": 36.3577906 + } + }, + "bounds": { + "southwest": { + "lat": 41.2933411, + "lng": 36.3066331 + }, + "northeast": { + "lat": 41.3291031, + "lng": 36.3577906 + } + } + } + } ] +} \ No newline at end of file diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 67bc298e..dacfd8e3 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -370,6 +370,10 @@ class GeocoderTest < Test::Unit::TestCase assert_equal "Haram", result.city end + def test_google_city_results_returns_nil_if_no_matching_component_types + result = Geocoder.search("no city data").first + assert_equal nil, result.city + end # --- Yahoo --- diff --git a/test/test_helper.rb b/test/test_helper.rb index bddff7d1..7364498a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -63,9 +63,10 @@ module Geocoder def fetch_raw_data(query, reverse = false) raise TimeoutError if query == "timeout" file = case query - when "no results"; :no_results - when "no locality"; :no_locality - else :madison_square_garden + when "no results"; :no_results + when "no locality"; :no_locality + when "no city data"; :no_city_data + else :madison_square_garden end read_fixture "google_#{file}.json" end -- GitLab