forked from carmen-ruby/carmen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_data.rb
151 lines (121 loc) · 3.86 KB
/
update_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'nokogiri'
require 'yaml'
require 'pathname'
YAML::ENGINE.yamler = 'psych'
begin
require 'ftools'
rescue LoadError
require 'fileutils' # ftools is now fileutils in Ruby 1.9
end
def write_file(data, path)
FileUtils.mkdir_p(File.dirname(path))
File.open(path + '.yml', 'w') { |f| f.write data.to_yaml }
end
def write_data_to_path_as_yaml(data, path)
data_keys = %w{alpha_2_code alpha_3_code numeric_code type}
locale_keys = %w{common_name name official_name}
locale_data = {}
data.each do |element|
locale = {}
locale_keys.each do |key|
locale[key] = element.delete(key) if element.key?(key)
end
parent_key = element['alpha_2_code'] || element['code']
locale_data[parent_key.downcase] = locale
end
path_segments = "en/#{path}".split('/').reverse
wrapped_locale_data = path_segments.inject(locale_data) { |hash, path|
{ path => hash }
}
write_file(data, 'iso_data/' + path)
write_file(wrapped_locale_data, 'locale/en/' + path)
end
def write_regions_to_path_as_yaml(regions_data, path)
regions_data.each do |subregion_data|
subregions = subregion_data.delete('subregions')
if subregions
subregion_path = path + "/#{subregion_data['code'].downcase}"
write_regions_to_path_as_yaml(subregions, subregion_path)
end
end
write_data_to_path_as_yaml(regions_data, path)
end
puts "Downloading data"
data_path = Pathname.new(File.expand_path('../../iso_data', __FILE__))
tmp_path = data_path + 'tmp'
FileUtils.mkdir_p(tmp_path)
files = {
'iso_3166.xml' => 'http://anonscm.debian.org/gitweb/?p=iso-codes/iso-codes.git;a=blob_plain;f=iso_3166/iso_3166.xml;hb=HEAD',
'iso_3166_2.xml' => 'http://anonscm.debian.org/gitweb/?p=iso-codes/iso-codes.git;a=blob_plain;f=iso_3166_2/iso_3166_2.xml;hb=HEAD' }
files.each_pair do |file, url|
`cd #{tmp_path.to_s} && curl -o #{file} "#{url}"`
end
# countries
puts "Importing countries"
country_data_path = tmp_path + 'iso_3166.xml'
file = File.open(country_data_path)
doc = Nokogiri::XML(file)
file.close
countries = []
doc.xpath('//iso_3166_entry').each do |country|
print '.'
countries << {
'alpha_2_code' => country['alpha_2_code'],
'alpha_3_code' => country['alpha_3_code'],
'numeric_code' => country['numeric_code'],
'common_name' => country['common_name'],
'name' => country['name'],
'official_name' => country['official_name'],
'type' => 'country'
}
end
puts
sorted_countries = countries.sort_by {|e| e['alpha_2_code'] }
write_data_to_path_as_yaml(sorted_countries, 'world')
# regions
puts "Importing regions"
region_data_path = tmp_path + 'iso_3166_2.xml'
file = File.open(region_data_path)
doc = Nokogiri::XML(file)
file.close
warnings = []
doc.css('iso_3166_country').each do |country|
code = country['code'].downcase
regions = []
country.css('iso_3166_subset').each do |subset|
type = subset['type'].downcase
subregions = subset.css('iso_3166_2_entry').map do |subregion|
data = {
'code' => subregion['code'].gsub(%r{^#{country['code']}-}, ''),
'name' => subregion['name'],
'type' => type
}
if subregion['parent']
parent = regions.find do |r|
parent_code = r['code']
parent_code = parent_code.split(/-| /)[1] if parent_code =~ /-| /
parent_code == subregion['parent']
end
if parent
parent['subregions'] ||= []
parent['subregions'] << data
else
warnings << "warning, did not find parent '#{subregion['parent']}'"
warnings << subregion
warnings << regions
warnings << ''
end
else
regions << data
end
end
end
sorted_regions = regions.sort_by {|e| e['code'] }
write_regions_to_path_as_yaml(sorted_regions, "world/#{code}")
print '.'
end
puts
unless warnings.empty?
puts warnings.join("\n")
end
FileUtils.rm_rf(tmp_path)