diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index dcd7a2791796b00e638cfcf49160b98c9d814119..2ab3a070281e6b3031c3f0785de92c3a90616f5a 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 11e551c97adeee116dd8756143a53c5a39dac52d..0000000000000000000000000000000000000000 --- 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 4befd7cf8e6fffc0cddd8c9229cf652dabd3f334..f649afe24527d605c75db55c4a228154f473873e 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 e97865dfcf60fb4e970caac77d2187f3eadf640d..b92d2b70e34ee718eaebcb341baa663b5bebabb0 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 07cb694c728458b88f1ac04ab7654aa11bcf05ac..6bda6b8f4c3a445b749d00ca7e7509abab08038a 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