Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Last active August 29, 2015 14:17
Show Gist options
  • Save guilleiguaran/ab5fe50f48801fe24e04 to your computer and use it in GitHub Desktop.
Save guilleiguaran/ab5fe50f48801fe24e04 to your computer and use it in GitHub Desktop.
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
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