Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add update task #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require 'yaml'
task :convert do
codes = {}
File.open('lib/data/US.txt').each_line do |line|
array = line.split('\t')
array = line.split("\t")
codes[array[1]] = {
state_code: array[4],
state_name: array[3],
Expand All @@ -16,3 +16,24 @@ task :convert do

File.write('lib/data/US.yml', codes.to_yaml)
end

task :update do
path = File.join(__dir__, 'lib', 'data', 'US.yml')
codes = ::YAML.safe_load_file(path, permitted_classes: [Symbol])

# Data comes from https://download.geonames.org/export/zip/
File.open('lib/data/US.txt').each_line do |line|
array = line.split("\t")

# Only add codes that are not in the list already and that parse out a state code
if codes[array[1]].nil? && array[4] != ''
codes[array[1]] = {
state_code: array[4],
state_name: array[3],
city: array[2]
}
end
end

File.write('lib/data/US.yml', codes.to_yaml)
end
Loading