Retrieve city, state, and time zone for a given ZIP code for those times when API's just aren’t doable 🎉
Note that time zones are only supported for US zip codes. Canadian zip codes do not support time zones.
Also only the first three characters of the Canadian postal code are used to look up the city and province. The remaining three characters should not be submitted in a call to ZipCodes.lookup. Documentation on why we only need to use the first three letters to determine the city and province is available at https://www.canadapost-postescanada.ca/cpc/en/support/articles/addressing-guidelines/postal-codes.page
- Download the latest
US.ymlfrom https://github.com/monterail/zip-codes/tree/master/lib/data and place it in thelib/data/sourcedirectory. - Run
ruby bin/convert_us_data.rbto convert the data and produce a new lib/data/US.csv file. - Run some manual tests using the bin/test_us_lookup.rb script.
- Commit the new
lib/data/US.csvandlib/data/source/US.ymlfiles.
- Download the latest
CA.zipfrom https://download.geonames.org/export/zip/CA.zip - Unzip it and place the resulting CA.txt file in the
lib/data/sourcedirectory. - Delete the CA.zip file.
- Run
ruby bin/convert_ca_data.rbto convert the data and produce a new lib/data/CA.csv file. - Run some manual tests using the bin/test_ca_lookup.rb script.
- Commit the new
lib/data/CA.csvandlib/data/source/CA.txtfiles.
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"}
ZipCodes.lookup('CA', 'V3B')
# => {:city=>"Port Coquitlam", :state_code=>"BC", :state_name=>"British Columbia", :time_zone=>nil}
# Case insensitive
ZipCodes.lookup('uS', '30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
ZipCodes.lookup('cA', 'V3B')
# => {:city=>"Port Coquitlam", :state_code=>"BC", :state_name=>"British Columbia", :time_zone=>nil}
ZipCodes.lookup('CA', 'V3b')
# => {:city=>"Port Coquitlam", :state_code=>"BC", :state_name=>"British Columbia", :time_zone=>nil}
# Symbols work too
ZipCodes.lookup(:US, '30301')
# => {:state_code=>"GA", :state_name=>"Georgia", :city=>"Atlanta", :time_zone=>"America/New_York"}
ZipCodes.lookup(:CA, 'V3B')
# => {:city=>"Port Coquitlam", :state_code=>"BC", :state_name=>"British Columbia", :time_zone=>nil}