From f52f655c56a029add875c32a25478888d1a9d833 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Mon, 1 Oct 2012 11:54:07 -0400 Subject: [PATCH] Update Yahoo lookup to reflect API changes. "Results" key is now "Result" and is not an array when only one result. Update code and response fixtures to reflect this. --- lib/geocoder/lookups/yahoo.rb | 11 ++- test/fixtures/yahoo_garbage.json | 50 ---------- .../fixtures/yahoo_madison_square_garden.json | 92 ++++++++++--------- test/fixtures/yahoo_no_results.json | 18 ++-- test/services_test.rb | 2 +- 5 files changed, 69 insertions(+), 104 deletions(-) delete mode 100644 test/fixtures/yahoo_garbage.json diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index dcd7a279..2ab3a070 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -12,8 +12,15 @@ module Geocoder::Lookup def results(query) return [] unless doc = fetch_data(query) - if doc = doc['ResultSet'] and doc['Error'] == 0 - return doc['Found'] > 0 ? Array(doc['Result']) : [] + doc = doc['ResultSet'] + # seems to have Error == 7 when no results, though this is not documented + if [0, 7].include?(doc['Error'].to_i) + if doc['Found'].to_i > 0 + r = doc['Result'] + return r.is_a?(Array) ? r : [r] + else + return [] + end else warn "Yahoo Geocoding API error: #{doc['Error']} (#{doc['ErrorMessage']})." return [] diff --git a/test/fixtures/yahoo_garbage.json b/test/fixtures/yahoo_garbage.json deleted file mode 100644 index 11e551c9..00000000 --- a/test/fixtures/yahoo_garbage.json +++ /dev/null @@ -1,50 +0,0 @@ -{ - "ResultSet":{ - "version":"1.0", - "Error":0, - "ErrorMessage":"No error", - "Locale":"us_US", - "Quality":87, - "Found":1, - "Result":[{ - "quality":9, - "latitude":"55.008390", - "longitude":"-5.822485", - "offsetlat":"54.314072", - "offsetlon":"-2.230010", - "radius":1145100, - "boundingbox":{ - "north":"60.854691", - "south":"49.162090", - "east":"1.768960", - "west":"-13.413930" - }, - "name":"", - "line1":"", - "line2":"", - "line3":"", - "line4":"United Kingdom", - "cross":"", - "house":"", - "street":"", - "xstreet":"", - "unittype":"", - "unit":"", - "postal":"", - "neighborhood":"", - "city":"", - "county":"", - "state":"", - "country":"United Kingdom", - "countrycode":"GB", - "statecode":"", - "countycode":"", - "timezone":"Europe/London", - "areacode":"", - "uzip":"", - "hash":"", - "woeid":23424975, - "woetype":12 - }] - } -} diff --git a/test/fixtures/yahoo_madison_square_garden.json b/test/fixtures/yahoo_madison_square_garden.json index 4befd7cf..f649afe2 100644 --- a/test/fixtures/yahoo_madison_square_garden.json +++ b/test/fixtures/yahoo_madison_square_garden.json @@ -1,46 +1,52 @@ { - "ResultSet":{ - "version":"1.0", - "Error":0, - "ErrorMessage":"No error", - "Locale":"us_US", - "Quality":90, - "Found":1, - "Result":[{ - "quality":90, - "latitude":"40.750381", - "longitude":"-73.993988", - "offsetlat":"40.750381", - "offsetlon":"-73.993988", - "radius":100, - "name":"Madison Square Garden", - "line1":"Madison Square Garden", - "line2":"New York, NY 10001", - "line3":"", - "line4":"United States", - "house":"", - "street":"", - "xstreet":"", - "unittype":"", - "unit":"", - "postal":"10001", - "neighborhood":"", - "city":"New York", - "county":"New York County", - "state":"New York", - "country":"United States", - "countrycode":"US", - "statecode":"NY", - "countycode":"", - "uzip":"10001", - "hash":"", - "woeid":23617041, - "woetype":20, - "cross":"", - "timezone":"America/New_York", - "neighborhood":"Garment District|Midtown|Midtown West|Manhattan", - "areacode":"212", - "boundingbox":{"north":"40.750832","south":"40.749931","east":"-73.993393","west":"-73.994591"} - }] + "@lang": "en-US", + "ResultSet": { + "@version": "2.0", + "@lang": "en-US", + "Error": "0", + "ErrorMessage": "No error", + "Locale": "en-US", + "Found": "1", + "Quality": "90", + "Result": { + "quality": "90", + "latitude": "40.750381", + "longitude": "-73.993988", + "offsetlat": "40.750381", + "offsetlon": "-73.993988", + "radius": "100", + "boundingbox": { + "north": "40.750832", + "south": "40.749931", + "east": "-73.993393", + "west": "-73.994591" + }, + "name": "Madison Square Garden", + "line1": "Madison Square Garden", + "line2": "New York, NY 10001", + "line3": "", + "line4": "United States", + "cross": "", + "house": "", + "street": "", + "xstreet": "", + "unittype": "", + "unit": "", + "postal": "10001", + "neighborhood": "Garment District|Midtown|Midtown West|Manhattan", + "city": "New York", + "county": "New York County", + "state": "New York", + "country": "United States", + "countrycode": "US", + "statecode": "NY", + "countycode": "", + "timezone": "America/New_York", + "areacode": "212", + "uzip": "10001", + "hash": "", + "woeid": "23617041", + "woetype": "20" + } } } diff --git a/test/fixtures/yahoo_no_results.json b/test/fixtures/yahoo_no_results.json index e97865df..b92d2b70 100644 --- a/test/fixtures/yahoo_no_results.json +++ b/test/fixtures/yahoo_no_results.json @@ -1,10 +1,12 @@ { - "ResultSet":{ - "version":"1.0", - "Error":0, - "ErrorMessage":"No error", - "Locale":"us_US", - "Quality":10, - "Found":0 - } + "@lang": "en-US", + "ResultSet": { + "@version": "2.0", + "@lang": "en-US", + "Error": "7", + "ErrorMessage": "No result", + "Locale": "en-US", + "Found": "0", + "Quality": "0" + } } diff --git a/test/services_test.rb b/test/services_test.rb index 07cb694c..6bda6b8f 100644 --- a/test/services_test.rb +++ b/test/services_test.rb @@ -76,7 +76,7 @@ class ServicesTest < Test::Unit::TestCase def test_yahoo_address_formatting Geocoder::Configuration.lookup = :yahoo result = Geocoder.search("Madison Square Garden, New York, NY").first - assert_equal "Madison Square Garden, New York, NY 10001, United States", + assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address end -- GitLab