From 296132cb89aea33679d9dad086c1b996f8d37391 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Fri, 22 Apr 2011 01:52:49 -0400 Subject: [PATCH] Properly handle Yandex.ru errors. --- lib/geocoder/lookups/yandex.rb | 4 ++++ test/fixtures/yandex_invalid_key.json | 1 + test/geocoder_test.rb | 11 +++++++++++ test/test_helper.rb | 16 +++++++++++++--- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/yandex_invalid_key.json diff --git a/lib/geocoder/lookups/yandex.rb b/lib/geocoder/lookups/yandex.rb index e999a378..25c31015 100644 --- a/lib/geocoder/lookups/yandex.rb +++ b/lib/geocoder/lookups/yandex.rb @@ -8,6 +8,10 @@ module Geocoder::Lookup def results(query, reverse = false) return [] unless doc = fetch_data(query, reverse) + if err = doc['error'] + warn "Yandex Geocoding API error: #{err['status']} (#{err['message']})." + return [] + end if doc = doc['response']['GeoObjectCollection'] meta = doc['metaDataProperty']['GeocoderResponseMetaData'] return meta['found'].to_i > 0 ? doc['featureMember'] : [] diff --git a/test/fixtures/yandex_invalid_key.json b/test/fixtures/yandex_invalid_key.json new file mode 100644 index 00000000..c6174d7f --- /dev/null +++ b/test/fixtures/yandex_invalid_key.json @@ -0,0 +1 @@ +{"error":{"status":401,"message":"invalid key"}} diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 264647e2..1d18853e 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -369,6 +369,17 @@ class GeocoderTest < Test::Unit::TestCase end + # --- Yandex --- + + def test_yandex_with_invalid_key + # keep test output clean: suppress timeout warning + orig = $VERBOSE; $VERBOSE = nil + Geocoder::Configuration.lookup = :yandex + assert_equal [], Geocoder.search("invalid key") + $VERBOSE = orig + end + + # --- Geocoder.ca --- def test_geocoder_ca_result_components diff --git a/test/test_helper.rb b/test/test_helper.rb index e638e780..958fad52 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -75,7 +75,10 @@ module Geocoder private #----------------------------------------------------------------- def fetch_raw_data(query, reverse = false) raise TimeoutError if query == "timeout" - file = query == "no results" ? :no_results : :madison_square_garden + file = case query + when "no results"; :no_results + else :madison_square_garden + end read_fixture "yahoo_#{file}.json" end end @@ -84,7 +87,11 @@ module Geocoder private #----------------------------------------------------------------- def fetch_raw_data(query, reverse = false) raise TimeoutError if query == "timeout" - file = query == "no results" ? :no_results : :kremlin + file = case query + when "no results"; :no_results + when "invalid key"; :invalid_key + else :kremlin + end read_fixture "yandex_#{file}.json" end end @@ -96,7 +103,10 @@ module Geocoder if reverse read_fixture "geocoder_ca_reverse.json" else - file = query == "no results" ? :no_results : :madison_square_garden + file = case query + when "no results"; :no_results + else :madison_square_garden + end read_fixture "geocoder_ca_#{file}.json" end end -- GitLab