Retrieve city, state, and time zone for a given ZIP code for those times when API's just aren’t doable 🎉
- Download the latest
US.yml
from https://github.com/monterail/zip-codes/tree/master/lib/data - In a rails console:
zips = YAML.safe_load_file("/absolute/path/to/US.yml", permitted_classes: [Symbol]).to_h
zips.each do |zip, z|
puts "#{zip},#{z[:state_code]},#{z[:state_name]},#{z[:city]},#{z[:time_zone]}"
end; nil
- Update the existing
US.csv
file with the printed contents
This isn't the cleanest approach, ideally we should take the time to put this in a script to streamline updates.
Using bundler, add to the Gemfile
:
gem 'zip-codes'
Or standalone:
$ gem install zip-codes
ZipCodes.lookup('US', '30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
# Case insensitive
ZipCodes.lookup('uS', '30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
# Symbols work too
ZipCodes.lookup(:US, '30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}