alexreisner/geocoder — pull request #587 by twonegatives (2014-01-27 10:46:31+00:00)
Title: fix for yandex city fetching: case of no locality name

Sometimes we are getting back results with ``Locality`` key in ``SubAdministrativeArea``, but that ``Locality`` has no ``LocalityName``. Instead, it has only ``DependentLocality`` and ``DependentLocalityName`` keys, e.g.:
``ruby
"GeoObject":{
"metaDataProperty":{
"GeocoderMetaData":{
"kind":"house",
"text":"Canada, Quebec, Gatineau, Hull, Rue Hormidas-Dupuis, 14",
"precision":"exact",
"AddressDetails":{
"Country":{
"AddressLine":"Quebec, Gatineau, Hull, Rue Hormidas-Dupuis, 14",
"CountryNameCode":"CA",
"CountryName":"Canada",
"AdministrativeArea":{
"AdministrativeAreaName":"Quebec",
"SubAdministrativeArea":{
"SubAdministrativeAreaName":"Gatineau",
"Locality":{
"DependentLocality":{
"DependentLocalityName":"Hull",
"Thoroughfare":{
"ThoroughfareName":"Rue Hormidas-Dupuis",
"Premise":{
"PremiseNumber":"14"
}
}
}
}
}
}
}
}
}
}
}
``
According to bugtrackers of geocoding services, this is a known issue:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=3320

Here we fix a problem with raise of ``undefined method 'empty' for nil class`` when asking a result object for its city:
``ruby
def city
if state.empty? and address_details.has_key? 'Locality'
address_details['Locality']['LocalityName']
elsif sub_state.empty? and address_details['AdministrativeArea'].has_key? 'Locality'
address_details['AdministrativeArea']['Locality']['LocalityName']
elsif not sub_state_city.empty? # NOTE: THIS RAISED ERROR DUE TO NIL STATE OF SUB_STATE_CITY
sub_state_city
else
""
end
end
``