Last active
August 29, 2015 14:12
-
-
Save brandoncc/75caf472cd70e2b64fe9 to your computer and use it in GitHub Desktop.
country and state utility actions
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
# requires geonames_api gem | |
# config/initializers/geo_names.rb | |
GeoNamesAPI.username = ENV["GEONAMES_USERNAME"] | |
GeoNamesAPI.lang = :en | |
# utility routes | |
get '/utilities/country_list(.:format)', to: 'utilities#country_list', as: :country_list | |
get '/utilities/state_list/:country(.:format)', to: 'utilities#state_list', as: :state_list | |
# actions | |
# USAGE: /utilities/country_list.json | |
def country_list | |
countries = GeoNamesAPI::Country.all.map { |country| [country.country_name, country.country_code] } | |
respond_to do |format| | |
format.json { render json: countries } | |
end | |
end | |
# USAGE: /utilities/state_list/US.json | |
def state_list | |
country_id = GeoNamesAPI::Country.find(params[:country]).geoname_id | |
states = GeoNamesAPI::Children.all(country_id).map { |state| [state.admin_name1, state.alternate_names['abbr']] } | |
respond_to do |format| | |
format.json { render json: states } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment