Last active
August 29, 2015 14:17
-
-
Save guilleiguaran/ab5fe50f48801fe24e04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cities_in_region(region_id) | |
Regions.lookup(region_id) | |
.or(Left("Couldn't find region #{region_id}")) | |
>-> region { region.bounding_box } | |
.or(Left("Region #{region_id} has no bounding box")) | |
>-> bounding_box { GeoNames.search_cities(bounding_box) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cities_in_region(region_id) | |
begin | |
region = Regions.lookup(region_id) | |
rescue GeoNames::ApiError => e | |
raise "Error looking up region #{region_id}: #{e.message}" | |
end | |
if region | |
if bounding_box = region.bounding_box | |
begin | |
response = GeoNames.search_cities(bounding_box) | |
response.fetch(“cities”, []).map { |hash| | |
City.new(hash) | |
} | |
rescue GeoNames::ApiError => e | |
raise "Error searching cities in #{bounding_box}: #{e.message}" | |
end | |
else | |
raise "Region #{region_id} doesn't have a bounding box" | |
end | |
else | |
raise "Couldn't find region #{region_id}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment