countries 4.2.1 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codeql-analysis.yml +70 -0
- data/.github/workflows/tests.yml +1 -5
- data/.rubocop.yml +40 -1
- data/.rubocop_todo.yml +8 -41
- data/CHANGELOG.md +33 -5
- data/Gemfile +5 -3
- data/LICENSE +3 -1
- data/README.markdown +34 -18
- data/Rakefile +15 -31
- data/bin/console +1 -0
- data/countries.gemspec +12 -10
- data/lib/countries/cache/countries.json +1 -1
- data/lib/countries/cache/locales/oc.json +1 -1
- data/lib/countries/cache/locales/ti.json +1 -1
- data/lib/countries/cache/locales/zu.json +1 -1
- data/lib/countries/configuration.rb +2 -0
- data/lib/countries/country/class_methods.rb +12 -97
- data/lib/countries/country/currency_methods.rb +2 -0
- data/lib/countries/country/emoji.rb +2 -3
- data/lib/countries/country/finder_methods.rb +81 -0
- data/lib/countries/country.rb +3 -40
- data/lib/countries/data/countries/AE.yaml +1 -1
- data/lib/countries/data/countries/KN.yaml +0 -1
- data/lib/countries/data/subdivisions/AD.yaml +21 -21
- data/lib/countries/data/subdivisions/AF.yaml +2 -2
- data/lib/countries/data/subdivisions/AG.yaml +1 -1
- data/lib/countries/data/subdivisions/AI.yaml +98 -98
- data/lib/countries/data/subdivisions/AL.yaml +82 -626
- data/lib/countries/data/subdivisions/AZ.yaml +5 -5
- data/lib/countries/data/subdivisions/BA.yaml +11 -11
- data/lib/countries/data/subdivisions/BD.yaml +8 -8
- data/lib/countries/data/subdivisions/BE.yaml +2 -2
- data/lib/countries/data/subdivisions/BF.yaml +13 -13
- data/lib/countries/data/subdivisions/BI.yaml +3 -3
- data/lib/countries/data/subdivisions/BM.yaml +77 -77
- data/lib/countries/data/subdivisions/BS.yaml +22 -22
- data/lib/countries/data/subdivisions/BW.yaml +7 -7
- data/lib/countries/data/subdivisions/BY.yaml +1 -1
- data/lib/countries/data/subdivisions/CD.yaml +19 -19
- data/lib/countries/data/subdivisions/CG.yaml +1 -1
- data/lib/countries/data/subdivisions/CI.yaml +14 -14
- data/lib/countries/data/subdivisions/CK.yaml +77 -77
- data/lib/countries/data/subdivisions/CN.yaml +0 -107
- data/lib/countries/data/subdivisions/CV.yaml +9 -9
- data/lib/countries/data/subdivisions/DK.yaml +1 -0
- data/lib/countries/data/subdivisions/DO.yaml +11 -11
- data/lib/countries/data/subdivisions/EE.yaml +78 -2
- data/lib/countries/data/subdivisions/EH.yaml +12 -12
- data/lib/countries/data/subdivisions/ES.yaml +17 -17
- data/lib/countries/data/subdivisions/FI.yaml +16 -16
- data/lib/countries/data/subdivisions/FJ.yaml +14 -14
- data/lib/countries/data/subdivisions/GB.yaml +21 -20
- data/lib/countries/data/subdivisions/GH.yaml +7 -7
- data/lib/countries/data/subdivisions/GL.yaml +6 -6
- data/lib/countries/data/subdivisions/GN.yaml +7 -7
- data/lib/countries/data/subdivisions/GQ.yaml +1 -0
- data/lib/countries/data/subdivisions/GR.yaml +1 -1
- data/lib/countries/data/subdivisions/GW.yaml +3 -3
- data/lib/countries/data/subdivisions/HT.yaml +1 -1
- data/lib/countries/data/subdivisions/ID.yaml +41 -41
- data/lib/countries/data/subdivisions/IE.yaml +1 -2
- data/lib/countries/data/subdivisions/IN.yaml +4 -3
- data/lib/countries/data/subdivisions/IQ.yaml +1 -1
- data/lib/countries/data/subdivisions/IR.yaml +2 -1
- data/lib/countries/data/subdivisions/IS.yaml +72 -0
- data/lib/countries/data/subdivisions/IT.yaml +7 -8
- data/lib/countries/data/subdivisions/KG.yaml +1 -1
- data/lib/countries/data/subdivisions/KH.yaml +1 -1
- data/lib/countries/data/subdivisions/KN.yaml +2 -2
- data/lib/countries/data/subdivisions/KP.yaml +12 -12
- data/lib/countries/data/subdivisions/KR.yaml +1 -1
- data/lib/countries/data/subdivisions/KZ.yaml +1 -1
- data/lib/countries/data/subdivisions/LA.yaml +1 -1
- data/lib/countries/data/subdivisions/LB.yaml +2 -2
- data/lib/countries/data/subdivisions/LC.yaml +10 -10
- data/lib/countries/data/subdivisions/LK.yaml +9 -9
- data/lib/countries/data/subdivisions/LR.yaml +2 -2
- data/lib/countries/data/subdivisions/LT.yaml +60 -60
- data/lib/countries/data/subdivisions/LU.yaml +1 -1
- data/lib/countries/data/subdivisions/LV.yaml +1 -1
- data/lib/countries/data/subdivisions/MA.yaml +35 -35
- data/lib/countries/data/subdivisions/MC.yaml +17 -17
- data/lib/countries/data/subdivisions/MD.yaml +740 -740
- data/lib/countries/data/subdivisions/ME.yaml +3 -3
- data/lib/countries/data/subdivisions/MH.yaml +2 -2
- data/lib/countries/data/subdivisions/ML.yaml +2 -2
- data/lib/countries/data/subdivisions/MM.yaml +1 -1
- data/lib/countries/data/subdivisions/MR.yaml +3 -3
- data/lib/countries/data/subdivisions/MV.yaml +8 -8
- data/lib/countries/data/subdivisions/MW.yaml +4 -4
- data/lib/countries/data/subdivisions/MX.yaml +1 -1
- data/lib/countries/data/subdivisions/NL.yaml +6 -6
- data/lib/countries/data/subdivisions/NO.yaml +1 -0
- data/lib/countries/data/subdivisions/NP.yaml +12 -12
- data/lib/countries/data/subdivisions/NZ.yaml +1 -1
- data/lib/countries/data/subdivisions/OM.yaml +6 -6
- data/lib/countries/data/subdivisions/PG.yaml +3 -3
- data/lib/countries/data/subdivisions/PH.yaml +16 -16
- data/lib/countries/data/subdivisions/PK.yaml +2 -2
- data/lib/countries/data/subdivisions/PS.yaml +16 -16
- data/lib/countries/data/subdivisions/QA.yaml +2 -2
- data/lib/countries/data/subdivisions/RS.yaml +2 -2
- data/lib/countries/data/subdivisions/SC.yaml +4 -2
- data/lib/countries/data/subdivisions/SH.yaml +1 -1
- data/lib/countries/data/subdivisions/SI.yaml +19 -19
- data/lib/countries/data/subdivisions/SJ.yaml +14 -14
- data/lib/countries/data/subdivisions/SL.yaml +1 -1
- data/lib/countries/data/subdivisions/SN.yaml +3 -3
- data/lib/countries/data/subdivisions/SS.yaml +10 -10
- data/lib/countries/data/subdivisions/ST.yaml +6 -0
- data/lib/countries/data/subdivisions/TD.yaml +6 -6
- data/lib/countries/data/subdivisions/TH.yaml +1 -1
- data/lib/countries/data/subdivisions/TJ.yaml +2 -2
- data/lib/countries/data/subdivisions/TK.yaml +21 -21
- data/lib/countries/data/subdivisions/TM.yaml +1 -1
- data/lib/countries/data/subdivisions/TT.yaml +2 -2
- data/lib/countries/data/subdivisions/TV.yaml +1 -1
- data/lib/countries/data/subdivisions/TW.yaml +3 -17
- data/lib/countries/data/subdivisions/TZ.yaml +5 -5
- data/lib/countries/data/subdivisions/UG.yaml +59 -52
- data/lib/countries/data/subdivisions/UM.yaml +0 -10
- data/lib/countries/data/subdivisions/WF.yaml +3 -13
- data/lib/countries/data/subdivisions/YE.yaml +3 -3
- data/lib/countries/data/subdivisions/ZA.yaml +2 -2
- data/lib/countries/data/subdivisions/ZM.yaml +1 -1
- data/lib/countries/data.rb +34 -37
- data/lib/countries/global.rb +2 -0
- data/lib/countries/iso3166.rb +3 -0
- data/lib/countries/kwarg_struct.rb +2 -0
- data/lib/countries/mongoid.rb +2 -0
- data/lib/countries/sources/cldr/downloader.rb +8 -8
- data/lib/countries/sources/cldr/subdivision.rb +3 -0
- data/lib/countries/sources/cldr/subdivision_updater.rb +23 -17
- data/lib/countries/sources/local/cached_loader.rb +3 -0
- data/lib/countries/sources/local/subdivision.rb +5 -2
- data/lib/countries/sources.rb +2 -0
- data/lib/countries/structure.rb +1 -1
- data/lib/countries/subdivision.rb +2 -0
- data/lib/countries/tasks/geocoding.rake +15 -6
- data/lib/countries/tasks/postal_codes.rake +5 -3
- data/lib/countries/timezones.rb +5 -2
- data/lib/countries/translations.rb +2 -0
- data/lib/countries/version.rb +3 -1
- data/lib/countries.rb +2 -0
- data/spec/00_global_spec.rb +2 -0
- data/spec/configuration_spec.rb +11 -5
- data/spec/country_spec.rb +71 -22
- data/spec/data_spec.rb +24 -18
- data/spec/mongoid_spec.rb +2 -2
- data/spec/perf_spec.rb +16 -16
- data/spec/spec_helper.rb +2 -0
- data/spec/subdivision_spec.rb +6 -4
- data/spec/thread_safety_spec.rb +4 -3
- data/spec/timezone_spec.rb +2 -0
- metadata +18 -45
- data/lib/countries/data/subdivisions/AS.yaml +0 -85
- data/lib/countries/data/subdivisions/AW.yaml +0 -9
- data/lib/countries/data/subdivisions/AX.yaml +0 -51
- data/lib/countries/data/subdivisions/BL.yaml +0 -11
- data/lib/countries/data/subdivisions/CP.yaml +0 -4
- data/lib/countries/data/subdivisions/CW.yaml +0 -9
- data/lib/countries/data/subdivisions/GF.yaml +0 -11
- data/lib/countries/data/subdivisions/GG.yaml +0 -155
- data/lib/countries/data/subdivisions/GP.yaml +0 -11
- data/lib/countries/data/subdivisions/GU.yaml +0 -275
- data/lib/countries/data/subdivisions/HK.yaml +0 -267
- data/lib/countries/data/subdivisions/IC.yaml +0 -4
- data/lib/countries/data/subdivisions/IM.yaml +0 -337
- data/lib/countries/data/subdivisions/JE.yaml +0 -169
- data/lib/countries/data/subdivisions/MF.yaml +0 -9
- data/lib/countries/data/subdivisions/MO.yaml +0 -9
- data/lib/countries/data/subdivisions/MP.yaml +0 -71
- data/lib/countries/data/subdivisions/MQ.yaml +0 -11
- data/lib/countries/data/subdivisions/MS.yaml +0 -43
- data/lib/countries/data/subdivisions/NC.yaml +0 -57
- data/lib/countries/data/subdivisions/PF.yaml +0 -81
- data/lib/countries/data/subdivisions/PM.yaml +0 -39
- data/lib/countries/data/subdivisions/PR.yaml +0 -1107
- data/lib/countries/data/subdivisions/RE.yaml +0 -11
- data/lib/countries/data/subdivisions/SX.yaml +0 -9
- data/lib/countries/data/subdivisions/TA.yaml +0 -4
- data/lib/countries/data/subdivisions/TF.yaml +0 -67
- data/lib/countries/data/subdivisions/VI.yaml +0 -57
- data/lib/countries/data/subdivisions/YT.yaml +0 -247
- data/lib/countries/setup.rb +0 -18
@@ -1140,7 +1140,7 @@ TA:
|
|
1140
1140
|
'no': Ta’izz
|
1141
1141
|
comments:
|
1142
1142
|
SA:
|
1143
|
-
name:
|
1143
|
+
name: صنعاء
|
1144
1144
|
code:
|
1145
1145
|
unofficial_names:
|
1146
1146
|
geo:
|
@@ -1233,7 +1233,7 @@ SA:
|
|
1233
1233
|
yo_BJ: Sana’a
|
1234
1234
|
comments:
|
1235
1235
|
RA:
|
1236
|
-
name:
|
1236
|
+
name: محافظة ريمة
|
1237
1237
|
code:
|
1238
1238
|
unofficial_names:
|
1239
1239
|
geo:
|
@@ -1293,7 +1293,7 @@ RA:
|
|
1293
1293
|
'no': Raymah
|
1294
1294
|
comments:
|
1295
1295
|
SU:
|
1296
|
-
name:
|
1296
|
+
name: محافظة أرخبيل سقطرى
|
1297
1297
|
code:
|
1298
1298
|
unofficial_names:
|
1299
1299
|
geo:
|
@@ -714,7 +714,7 @@ WC:
|
|
714
714
|
yo_BJ: Western Cape
|
715
715
|
comments:
|
716
716
|
GP:
|
717
|
-
name:
|
717
|
+
name: Gauteng
|
718
718
|
code:
|
719
719
|
unofficial_names:
|
720
720
|
geo:
|
@@ -722,7 +722,7 @@ GP:
|
|
722
722
|
en: Gauteng
|
723
723
|
comments:
|
724
724
|
KZN:
|
725
|
-
name:
|
725
|
+
name: KwaZulu-Natal
|
726
726
|
code:
|
727
727
|
unofficial_names:
|
728
728
|
geo:
|
data/lib/countries/data.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ISO3166
|
2
4
|
# Handles building the in memory store of countries data
|
3
5
|
class Data
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
@cache_dir = [File.dirname(__FILE__), 'cache']
|
7
|
+
@cache = {}
|
8
|
+
@registered_data = {}
|
9
|
+
@mutex = Mutex.new
|
8
10
|
|
9
11
|
def initialize(alpha2)
|
10
12
|
@alpha2 = alpha2.to_s.upcase
|
@@ -15,30 +17,23 @@ module ISO3166
|
|
15
17
|
end
|
16
18
|
|
17
19
|
class << self
|
18
|
-
def cache_dir
|
19
|
-
@@cache_dir
|
20
|
-
end
|
21
|
-
|
22
|
-
def cache_dir=(value)
|
23
|
-
@@cache_dir = value
|
24
|
-
end
|
25
20
|
|
26
21
|
# Registers a new Country with custom data.
|
27
22
|
# If you are overriding an existing country, this does not perform a deep merge so you will need to __bring in all data you wish to be available__.
|
28
23
|
# Overriding an existing country will also remove it from the internal management of translations.
|
29
24
|
def register(data)
|
30
25
|
alpha2 = data[:alpha2].upcase
|
31
|
-
|
32
|
-
|
26
|
+
@registered_data[alpha2] = deep_stringify_keys(data)
|
27
|
+
@registered_data[alpha2]['translations'] = \
|
33
28
|
Translations.new.merge(data['translations'] || {})
|
34
|
-
|
29
|
+
@cache = cache.merge(@registered_data)
|
35
30
|
end
|
36
31
|
|
37
32
|
# Removes a country from the loaded data
|
38
33
|
def unregister(alpha2)
|
39
34
|
alpha2 = alpha2.to_s.upcase
|
40
|
-
|
41
|
-
|
35
|
+
@cache.delete(alpha2)
|
36
|
+
@registered_data.delete(alpha2)
|
42
37
|
end
|
43
38
|
|
44
39
|
def cache
|
@@ -47,8 +42,8 @@ module ISO3166
|
|
47
42
|
|
48
43
|
# Resets the loaded data and cache
|
49
44
|
def reset
|
50
|
-
|
51
|
-
|
45
|
+
@cache = {}
|
46
|
+
@registered_data = {}
|
52
47
|
ISO3166.configuration.loaded_locales = []
|
53
48
|
end
|
54
49
|
|
@@ -60,16 +55,19 @@ module ISO3166
|
|
60
55
|
def update_cache
|
61
56
|
load_data!
|
62
57
|
sync_translations!
|
63
|
-
|
58
|
+
@cache
|
64
59
|
end
|
65
60
|
|
61
|
+
private
|
62
|
+
|
66
63
|
def load_data!
|
67
|
-
return
|
64
|
+
return @cache unless load_required?
|
65
|
+
|
68
66
|
synchronized do
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
67
|
+
@cache = load_cache %w[countries.json]
|
68
|
+
@_country_codes = @cache.keys
|
69
|
+
@cache = @cache.merge(@registered_data)
|
70
|
+
@cache
|
73
71
|
end
|
74
72
|
end
|
75
73
|
|
@@ -85,11 +83,9 @@ module ISO3166
|
|
85
83
|
end
|
86
84
|
end
|
87
85
|
|
88
|
-
private
|
89
|
-
|
90
86
|
def synchronized(&block)
|
91
87
|
if use_mutex?
|
92
|
-
|
88
|
+
@mutex.synchronize(&block)
|
93
89
|
else
|
94
90
|
block.call
|
95
91
|
end
|
@@ -102,17 +98,17 @@ module ISO3166
|
|
102
98
|
|
103
99
|
def load_required?
|
104
100
|
synchronized do
|
105
|
-
|
101
|
+
@cache.empty?
|
106
102
|
end
|
107
103
|
end
|
108
104
|
|
109
105
|
def loaded_codes
|
110
|
-
|
106
|
+
@cache.keys
|
111
107
|
end
|
112
108
|
|
113
109
|
# Codes that we have translations for in dataset
|
114
110
|
def internal_codes
|
115
|
-
|
111
|
+
@_country_codes - @registered_data.keys
|
116
112
|
end
|
117
113
|
|
118
114
|
def cache_flush_required?
|
@@ -139,9 +135,9 @@ module ISO3166
|
|
139
135
|
synchronized do
|
140
136
|
locale_names = load_cache(['locales', "#{locale}.json"])
|
141
137
|
internal_codes.each do |alpha2|
|
142
|
-
|
143
|
-
|
144
|
-
|
138
|
+
@cache[alpha2]['translations'] ||= Translations.new
|
139
|
+
@cache[alpha2]['translations'][locale] = locale_names[alpha2].freeze
|
140
|
+
@cache[alpha2]['translated_names'] = @cache[alpha2]['translations'].values.freeze
|
145
141
|
end
|
146
142
|
ISO3166.configuration.loaded_locales << locale
|
147
143
|
end
|
@@ -150,8 +146,8 @@ module ISO3166
|
|
150
146
|
def unload_translations(locale)
|
151
147
|
synchronized do
|
152
148
|
internal_codes.each do |alpha2|
|
153
|
-
|
154
|
-
|
149
|
+
@cache[alpha2]['translations'].delete(locale)
|
150
|
+
@cache[alpha2]['translated_names'] = @cache[alpha2]['translations'].values.freeze
|
155
151
|
end
|
156
152
|
ISO3166.configuration.loaded_locales.delete(locale)
|
157
153
|
end
|
@@ -163,7 +159,7 @@ module ISO3166
|
|
163
159
|
end
|
164
160
|
|
165
161
|
def datafile_path(file_array)
|
166
|
-
File.join([
|
162
|
+
File.join([@cache_dir] + file_array)
|
167
163
|
end
|
168
164
|
|
169
165
|
def deep_stringify_keys(data)
|
@@ -171,7 +167,8 @@ module ISO3166
|
|
171
167
|
data.transform_values! do |v|
|
172
168
|
v.is_a?(Hash) ? deep_stringify_keys(v) : v
|
173
169
|
end
|
174
|
-
|
170
|
+
|
171
|
+
data
|
175
172
|
end
|
176
173
|
end
|
177
174
|
end
|
data/lib/countries/global.rb
CHANGED
data/lib/countries/iso3166.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'yaml'
|
2
4
|
require 'json'
|
3
5
|
require 'i18n_data'
|
@@ -8,6 +10,7 @@ require 'countries/data'
|
|
8
10
|
require 'countries/structure'
|
9
11
|
require 'countries/translations'
|
10
12
|
require 'countries/country/class_methods'
|
13
|
+
require 'countries/country/finder_methods'
|
11
14
|
require 'countries/country/emoji'
|
12
15
|
require 'countries/country'
|
13
16
|
require 'countries/subdivision'
|
data/lib/countries/mongoid.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'uri'
|
2
4
|
require 'net/http'
|
3
5
|
require 'nokogiri'
|
@@ -19,19 +21,17 @@ module Sources
|
|
19
21
|
def download_folder(type)
|
20
22
|
folder = File.join(ISO3166_ROOT_PATH, 'tmp', 'cldr', 'trunk', 'common', type)
|
21
23
|
FileUtils.mkdir_p(folder)
|
22
|
-
url = URI.parse("https://api.github.com/repos/unicode-org/cldr/contents/common
|
24
|
+
url = URI.parse("https://api.github.com/repos/unicode-org/cldr/contents/common/#{type}")
|
23
25
|
path_listing = JSON.parse(Net::HTTP.get_response(url).body)
|
24
26
|
path_listing.each do |path|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
next unless path['name'] =~ /\.xml$/
|
28
|
+
|
29
|
+
File.open(File.join(folder, path['name']), 'w') do |f|
|
30
|
+
raw_url = URI.parse(path['download_url'])
|
31
|
+
f.write(Net::HTTP.get_response(raw_url).body)
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
33
|
-
|
34
35
|
end
|
35
36
|
end
|
36
37
|
end
|
37
|
-
|
@@ -1,8 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Sources
|
2
4
|
module CLDR
|
3
5
|
# Auxiliary Subdivision class to support loading Unicode CLDR data to update local files
|
4
6
|
class Subdivision
|
5
7
|
attr_reader :xml, :language_code
|
8
|
+
|
6
9
|
def initialize(language_code:, xml:)
|
7
10
|
@language_code = language_code
|
8
11
|
@xml = xml
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support'
|
2
4
|
require 'active_support/core_ext/hash/deep_merge'
|
3
5
|
require 'nokogiri'
|
@@ -9,25 +11,29 @@ module Sources
|
|
9
11
|
class SubdivisionUpdater
|
10
12
|
def call
|
11
13
|
d = Dir['./tmp/cldr/trunk/common/subdivisions/*.xml']
|
12
|
-
loader = Sources::Local::CachedLoader.new(Sources::Local::Subdivision)
|
13
|
-
d.each
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
@loader = Sources::Local::CachedLoader.new(Sources::Local::Subdivision)
|
15
|
+
d.each { |file_path| update_locale(file_path) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def update_locale(file_path)
|
19
|
+
language_data = Nokogiri::XML(File.read(file_path))
|
20
|
+
language_code = File.basename(file_path, '.*')
|
21
|
+
subdivisions = language_data.css('subdivision')
|
22
|
+
next if subdivisions.empty?
|
23
|
+
|
24
|
+
last_country_code_seen = nil
|
19
25
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
last_country_code_seen = subdivision.country_code
|
26
|
+
subdivisions.each_with_index do |subdivision, index|
|
27
|
+
subdivision = Sources::CLDR::Subdivision.new(language_code: language_code, xml: subdivision)
|
28
|
+
data = @loader.load(subdivision.country_code)
|
29
|
+
data[subdivision.code] ||= {}
|
30
|
+
data[subdivision.code] = data[subdivision.code].deep_merge(subdivision.to_h)
|
31
|
+
if (last_country_code_seen && last_country_code_seen != subdivision.country_code) ||
|
32
|
+
index == subdivisions.size - 1
|
33
|
+
puts "Updated #{subdivision.country_code} with language_code #{language_code}"
|
34
|
+
@loader.save(subdivision.country_code, data)
|
30
35
|
end
|
36
|
+
last_country_code_seen = subdivision.country_code
|
31
37
|
end
|
32
38
|
end
|
33
39
|
end
|
@@ -1,9 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Sources
|
2
4
|
# Support code to allow updating subdivision data from the Unicode CLDR repository
|
3
5
|
module Local
|
4
6
|
# Loader for locally-cached data, to allow merging Unicode CLDR data with existing local data
|
5
7
|
class CachedLoader
|
6
8
|
attr_reader :klass
|
9
|
+
|
7
10
|
def initialize(klass)
|
8
11
|
@klass = klass
|
9
12
|
@loaded_countries = {}
|
@@ -1,8 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Sources
|
2
4
|
module Local
|
3
5
|
# Auxiliary Subdivision class to support loading the local subdivision data to be updated with Unicode CLDR data
|
4
6
|
class Subdivision
|
5
7
|
attr_reader :code
|
8
|
+
|
6
9
|
def initialize(code)
|
7
10
|
@code = code
|
8
11
|
end
|
@@ -16,8 +19,8 @@ module Sources
|
|
16
19
|
end
|
17
20
|
|
18
21
|
def save(data)
|
19
|
-
File.
|
20
|
-
rescue
|
22
|
+
File.write(file_path, data.to_yaml)
|
23
|
+
rescue StandardError
|
21
24
|
puts "failed to read #{file}: #{$ERROR_INFO}"
|
22
25
|
end
|
23
26
|
|
data/lib/countries/sources.rb
CHANGED
data/lib/countries/structure.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'geocoder'
|
2
4
|
require 'retryable'
|
3
5
|
|
@@ -16,7 +18,7 @@ def geocode(query, params)
|
|
16
18
|
Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }) do
|
17
19
|
Geocoder.search(query, params: params).first
|
18
20
|
end
|
19
|
-
rescue => e
|
21
|
+
rescue Geocoder::Error, Geocoder::LookupTimeout => e
|
20
22
|
warn "Attempts exceeded for query #{query}, last error was #{e.message}"
|
21
23
|
nil
|
22
24
|
end
|
@@ -26,7 +28,7 @@ def load_country_yaml(alpha2)
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def save_country_yaml(alpha2, data)
|
29
|
-
File.
|
31
|
+
File.write(File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'countries', "#{alpha2}.yaml"), data.to_yaml)
|
30
32
|
end
|
31
33
|
|
32
34
|
def country_codes
|
@@ -44,7 +46,10 @@ namespace :geocode do
|
|
44
46
|
data = load_country_yaml(country.alpha2)
|
45
47
|
|
46
48
|
next unless (result = geocode(country.iso_short_name, {region: country.alpha2}))
|
47
|
-
|
49
|
+
|
50
|
+
unless result.types.include?('country')
|
51
|
+
puts "WARNING:: Geocoder returned something that was not a country for #{country.alpha2}"
|
52
|
+
end
|
48
53
|
geometry = result.geometry
|
49
54
|
|
50
55
|
# Extract center point data
|
@@ -55,6 +60,7 @@ namespace :geocode do
|
|
55
60
|
|
56
61
|
# Extract bounding box data
|
57
62
|
next unless geometry['bounds']
|
63
|
+
|
58
64
|
data[code]['geo']['bounds'] = geometry['bounds']
|
59
65
|
data[code]['geo']['min_latitude'] = geometry['bounds']['southwest']['lat']
|
60
66
|
data[code]['geo']['min_longitude'] = geometry['bounds']['southwest']['lng']
|
@@ -73,10 +79,10 @@ namespace :geocode do
|
|
73
79
|
ISO3166::Country.all.select(&:subdivisions?).each do |c|
|
74
80
|
# Iterate subdivisions
|
75
81
|
state_data = c.subdivisions.dup
|
76
|
-
state_data.reject { |_, data| data['geo'] }.each do |code,
|
82
|
+
state_data.reject { |_, data| data['geo'] }.each do |code, _|
|
77
83
|
location = "#{c.alpha2}-#{code}"
|
78
84
|
|
79
|
-
next unless (result = geocode(location, {region: c.alpha2}))
|
85
|
+
next unless (result = geocode(location, { region: c.alpha2 }))
|
80
86
|
|
81
87
|
geometry = result.geometry
|
82
88
|
if geometry['location']
|
@@ -84,14 +90,17 @@ namespace :geocode do
|
|
84
90
|
state_data[code]['geo']['latitude'] = geometry['location']['lat']
|
85
91
|
state_data[code]['geo']['longitude'] = geometry['location']['lng']
|
86
92
|
end
|
93
|
+
|
87
94
|
next unless geometry['bounds']
|
95
|
+
|
88
96
|
state_data[code]['geo']['min_latitude'] = geometry['bounds']['southwest']['lat']
|
89
97
|
state_data[code]['geo']['min_longitude'] = geometry['bounds']['southwest']['lng']
|
90
98
|
state_data[code]['geo']['max_latitude'] = geometry['bounds']['northeast']['lat']
|
91
99
|
state_data[code]['geo']['max_longitude'] = geometry['bounds']['northeast']['lng']
|
92
100
|
end
|
93
101
|
# Write updated YAML for current country
|
94
|
-
File.
|
102
|
+
out = File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'subdivisions', "#{c.alpha2}.yaml")
|
103
|
+
File.write(out, state_data.to_yaml)
|
95
104
|
end
|
96
105
|
end
|
97
106
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
namespace :postal_codes do
|
2
4
|
desc 'Retrieve and update postal codes and their format'
|
3
5
|
task :update do
|
@@ -20,13 +22,13 @@ namespace :postal_codes do
|
|
20
22
|
json = JSON.parse(response) rescue {}
|
21
23
|
puts ' - Returned empty data. Skipping ' and next if json.empty?
|
22
24
|
|
23
|
-
postal_code = ['postal_code',
|
25
|
+
postal_code = ['postal_code', !json['zip'].nil?]
|
24
26
|
postal_code_format = ['postal_code_format', json['zip']]
|
25
27
|
|
26
28
|
if postal_code_index
|
27
29
|
data[postal_code_index] = postal_code
|
28
30
|
else
|
29
|
-
postal_code_index = data.find_index { |d| d[0] == 'nationality' } + 1 || data.size
|
31
|
+
postal_code_index = (data.find_index { |d| d[0] == 'nationality' } + 1) || data.size
|
30
32
|
data.insert(postal_code_index, postal_code)
|
31
33
|
end
|
32
34
|
|
@@ -42,7 +44,7 @@ namespace :postal_codes do
|
|
42
44
|
|
43
45
|
yaml[country_code] = data.to_h
|
44
46
|
|
45
|
-
File.
|
47
|
+
File.write(country_file, yaml.to_yaml)
|
46
48
|
end
|
47
49
|
end
|
48
50
|
end
|
data/lib/countries/timezones.rb
CHANGED
@@ -1,13 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ISO3166
|
2
4
|
# Extend Country class with support for timezones. Requires the {tzinfo}[https://github.com/tzinfo/tzinfo] gem
|
3
5
|
#
|
4
6
|
# gem 'tzinfo'
|
5
7
|
#
|
6
8
|
module TimezoneExtensions
|
9
|
+
# TODO: rename method to tz_country or something similar
|
7
10
|
def timezones
|
8
|
-
@
|
11
|
+
@timezones ||= TZInfo::Country.get(alpha2)
|
9
12
|
end
|
10
13
|
end
|
11
14
|
end
|
12
15
|
|
13
|
-
ISO3166::Country.
|
16
|
+
ISO3166::Country.include ISO3166::TimezoneExtensions
|
data/lib/countries/version.rb
CHANGED
data/lib/countries.rb
CHANGED
data/spec/00_global_spec.rb
CHANGED
data/spec/configuration_spec.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'spec_helper'
|
3
4
|
require 'i18n'
|
4
5
|
|
@@ -16,14 +17,14 @@ describe 'ISO3166.configuration' do
|
|
16
17
|
end
|
17
18
|
|
18
19
|
it 'locales are assumed from I18n.available_locales' do
|
19
|
-
I18n.available_locales = [
|
20
|
+
I18n.available_locales = %i[de en]
|
20
21
|
ISO3166.reset
|
21
22
|
expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
|
22
23
|
expect(ISO3166::Country.new('DE').translation(:es)).to eq nil
|
23
24
|
end
|
24
25
|
|
25
26
|
it 'unsupported locales do not affect supported locales' do
|
26
|
-
I18n.available_locales = [
|
27
|
+
I18n.available_locales = %i[de en unsupported]
|
27
28
|
ISO3166.reset
|
28
29
|
expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
|
29
30
|
expect(ISO3166::Country.new('DE').translation(:es)).to eq nil
|
@@ -36,7 +37,7 @@ describe 'ISO3166.configuration' do
|
|
36
37
|
end
|
37
38
|
|
38
39
|
it 'locales can be changed' do
|
39
|
-
ISO3166.configuration.locales = [
|
40
|
+
ISO3166.configuration.locales = %i[es de en]
|
40
41
|
expect(ISO3166::Country.new('DE').translation(:es)).to eq 'Alemania'
|
41
42
|
expect(ISO3166::Country.new('DE').translation(:en)).to eq 'Germany'
|
42
43
|
expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
|
@@ -44,7 +45,12 @@ describe 'ISO3166.configuration' do
|
|
44
45
|
|
45
46
|
it 'locales can be changed with a block' do
|
46
47
|
ISO3166.configure do |config|
|
47
|
-
config.locales = [
|
48
|
+
config.locales = %i[af am ar as az be bg bn br bs ca cs cy da de dz el en
|
49
|
+
eo es et eu fa fi fo fr ga gl gu he hi hr hu hy ia id
|
50
|
+
is it ja ka kk km kn ko ku lt lv mi mk ml mn mr ms mt
|
51
|
+
nb ne nl nn oc or pa pl ps pt ro ru rw si sk sl so sq
|
52
|
+
sr sv sw ta te th ti tk tl tr tt ug uk ve vi wa wo xh
|
53
|
+
zh zu]
|
48
54
|
end
|
49
55
|
|
50
56
|
expect(ISO3166::Country.new('DE').translations.size).to eq 92
|