diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index 43138b62d3ae06879126dd11fd5442871b387f60..77a658b4f3e0a05961eecce51d5e244cdf88036a 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -34,6 +34,23 @@ module Geocoder::Lookup end end + ## + # Yahoo returns errors as XML even when JSON format is specified. + # Handle that here, without parsing the XML + # (which would add unnecessary complexity). + # + def parse_raw_data(raw_data) + if raw_data.match /^<\?xml/ + if raw_data.include?("Rate Limit Exceeded") + raise_error(Geocoder::OverQueryLimitError) || warn("Over API query limit.") + elsif raw_data.include?("Please provide valid credentials") + raise_error(Geocoder::InvalidApiKey) || warn("Invalid API key.") + end + else + super(raw_data) + end + end + def query_url_params(query) super.merge( :location => query.sanitized_text, diff --git a/test/fixtures/yahoo_invalid_key.json b/test/fixtures/yahoo_invalid_key.json new file mode 100644 index 0000000000000000000000000000000000000000..c9b7319b05361287a7829d884d051fb397f36e4d --- /dev/null +++ b/test/fixtures/yahoo_invalid_key.json @@ -0,0 +1,2 @@ +<?xml version='1.0' encoding='UTF-8'?>\n<yahoo:error xmlns:yahoo='http://yahooapis.com/v1/base.rng'\n xml:lang='en-US'> + <yahoo:description>Please provide valid credentials. OAuth oauth_problem="consumer_key_unknown", realm="yahooapis.com"</yahoo:description>\n</yahoo:error> diff --git a/test/fixtures/yahoo_over_limit.json b/test/fixtures/yahoo_over_limit.json new file mode 100644 index 0000000000000000000000000000000000000000..7dddadf5f0d5e75bae7fd245d34dac41012e23aa --- /dev/null +++ b/test/fixtures/yahoo_over_limit.json @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="utf-8"?> <yahoo:error xmlns:yahoo="http://yahooapis.com/v1/base.rng\ <http://yahooapis.com/v1/base.rng%5C>" xml:lang="en-US"> + <yahoo:description>Rate Limit Exceeded</yahoo:description> <yahoo:detail>Key has exceeded its configured rate limit.</yahoo:detail> </yahoo:error> diff --git a/test/services_test.rb b/test/services_test.rb index 200e10e7624f3c0e02f8e86e8768fdf5c70546af..3681c0187742c2b823506ef0931c6d7de78035f2 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -105,6 +105,21 @@ class ServicesTest < Test::Unit::TestCase assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address end + def test_yahoo_raises_exception_when_over_query_limit + Geocoder.configure(:always_raise => [Geocoder::OverQueryLimitError]) + l = Geocoder::Lookup.get(:yahoo) + assert_raises Geocoder::OverQueryLimitError do + l.send(:results, Geocoder::Query.new("over limit")) + end + end + + def test_yahoo_raises_exception_on_invalid_key + Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey]) + l = Geocoder::Lookup.get(:yahoo) + assert_raises Geocoder::InvalidApiKey do + l.send(:results, Geocoder::Query.new("invalid key")) + end + end # --- Yandex --- diff --git a/test/test_helper.rb b/test/test_helper.rb index 95b7dedb3ee3c4cf3a1ebff72aa477bef77939c5..f1b526c13732da9397f1985ca1c1b2ec7f38d532 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -110,6 +110,8 @@ module Geocoder raise SocketError if query.text == "socket_error" file = case query.text when "no results"; :no_results + when "over limit"; :over_limit + when "invalid key"; :invalid_key when "error"; :error else :madison_square_garden end