From a596eb22371ab1879e2427b15dd9e90576d42245 Mon Sep 17 00:00:00 2001 From: Dzmitry <dzmitry.kovalenok@thehamon.com> Date: Fri, 9 Dec 2016 22:28:23 +0300 Subject: [PATCH] Now result.country doesn't fail f result is not in a country In the sea for example [Fix #1131] --- lib/geocoder/results/yandex.rb | 12 +++++- test/fixtures/yandex_black_sea | 74 ++++++++++++++++++++++++++++++++ test/unit/lookups/yandex_test.rb | 5 +++ 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/yandex_black_sea diff --git a/lib/geocoder/results/yandex.rb b/lib/geocoder/results/yandex.rb index f14f8bfd..eb42548c 100644 --- a/lib/geocoder/results/yandex.rb +++ b/lib/geocoder/results/yandex.rb @@ -25,11 +25,19 @@ module Geocoder::Result end def country - address_details['CountryName'] + if address_details + address_details['CountryName'] + else + "" + end end def country_code - address_details['CountryNameCode'] + if address_details + address_details['CountryNameCode'] + else + "" + end end def state diff --git a/test/fixtures/yandex_black_sea b/test/fixtures/yandex_black_sea new file mode 100644 index 00000000..9f7c736f --- /dev/null +++ b/test/fixtures/yandex_black_sea @@ -0,0 +1,74 @@ +{ + "response": { + "GeoObjectCollection": { + "metaDataProperty": { + "GeocoderResponseMetaData": { + "request": "32,43", + "found": "2", + "results": "10", + "Point": { + "pos": "32.000000 43.000000" + } + } + }, + "featureMember": [ + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "kind": "hydro", + "text": "Black Sea", + "precision": "other", + "AddressDetails": { + "Locality": { + "Premise": { + "PremiseName": "Black Sea" + } + } + } + } + }, + "name": "Black Sea", + "boundedBy": { + "Envelope": { + "lowerCorner": "27.442409 40.908662", + "upperCorner": "41.777787 46.627275" + } + }, + "Point": { + "pos": "34.188281 43.229215" + } + } + }, + { + "GeoObject": { + "metaDataProperty": { + "GeocoderMetaData": { + "kind": "hydro", + "text": "Black Sea", + "precision": "other", + "AddressDetails": { + "Locality": { + "Premise": { + "PremiseName": "Black Sea" + } + } + } + } + }, + "name": "Black Sea", + "boundedBy": { + "Envelope": { + "lowerCorner": "27.628351 40.908893", + "upperCorner": "41.777796 46.627999" + } + }, + "Point": { + "pos": "33.383633 44.235582" + } + } + } + ] + } + } +} diff --git a/test/unit/lookups/yandex_test.rb b/test/unit/lookups/yandex_test.rb index fc232927..f6357ed0 100644 --- a/test/unit/lookups/yandex_test.rb +++ b/test/unit/lookups/yandex_test.rb @@ -14,4 +14,9 @@ class YandexTest < GeocoderTestCase result.viewport end + def test_yandex_empty_results + result = Geocoder.search('black sea').first + assert_equal "", result.country_code + assert_equal "", result.country + end end -- GitLab