Skip to content
Snippets Groups Projects
Commit 296132cb authored by Alex Reisner's avatar Alex Reisner
Browse files

Properly handle Yandex.ru errors.

parent a4d7908d
No related branches found
No related tags found
No related merge requests found
......@@ -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'] : []
......
{"error":{"status":401,"message":"invalid key"}}
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment