worldwide 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/db/data/regions/US.yml +2 -0
- data/lib/worldwide/region.rb +5 -1
- data/lib/worldwide/regions.rb +3 -11
- data/lib/worldwide/regions_loader.rb +34 -1
- data/lib/worldwide/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dca8f095b6020792ecd6186606db9cd6159d03414b735b0c2d4b30f0746b680
|
4
|
+
data.tar.gz: 7e40cbbd862960354faf241a3320c03b1a081fa005f42063bf27951ca94f3c1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c385197a1a3239d1d1251a0cb7b45fe3f04f4d9e75dc3ed02227fe9692184b12dc5754c4c5292bfbd24e4a079df48b2ea63689e63b11511ec1f7017627ac15a
|
7
|
+
data.tar.gz: 666f0e760f82cfab270d47328a9600204e7c93c04bd16106b5f6d46cadefcd15f90dc64ea5663f0eeefc2827458aa2b47147468b45da059a57c37dd146893444
|
data/CHANGELOG.md
CHANGED
@@ -28,6 +28,11 @@ Nil.
|
|
28
28
|
|
29
29
|
---
|
30
30
|
|
31
|
+
[0.4.0] - 2023-11-08
|
32
|
+
- Add region name alternates [#32](https://github.com/Shopify/worldwide/pull/32)
|
33
|
+
- Cache `Region#parent_name` [#33](https://github.com/Shopify/worldwide/pull/33)
|
34
|
+
- Use hash tables to look up regions by code [#36](https://github.com/Shopify/worldwide/pull/36)
|
35
|
+
|
31
36
|
[0.3.0] - 2023-11-03
|
32
37
|
- Add code alternates for Japan [#23](https://github.com/Shopify/worldwide/pull/23)
|
33
38
|
- Add code alternates for Puerto Rico [#24](https://github.com/Shopify/worldwide/pull/24)
|
data/Gemfile.lock
CHANGED
data/db/data/regions/US.yml
CHANGED
@@ -7,6 +7,8 @@ unit_system: imperial
|
|
7
7
|
tax_name: Federal Tax
|
8
8
|
group: North American Countries
|
9
9
|
group_name: North America
|
10
|
+
name_alternates:
|
11
|
+
- United States of America
|
10
12
|
zip_label: Zip code
|
11
13
|
zip_regex: "(?-mix:\\A\\d{5}(-\\d{4})?\\z)|(?i-mx:\\AFPO\\s*A[A-Z]\\s*\\d{5}-\\d{4}\\z)"
|
12
14
|
zip_example: '90210'
|
data/lib/worldwide/region.rb
CHANGED
@@ -110,6 +110,10 @@ module Worldwide
|
|
110
110
|
# This name is the name that was traditionally returned by "country_db".
|
111
111
|
attr_reader :legacy_name
|
112
112
|
|
113
|
+
# Other names that may be used to refer to this region.
|
114
|
+
# E.g., "Czech Republic" is also known as "Czechia".
|
115
|
+
attr_accessor :name_alternates
|
116
|
+
|
113
117
|
# iso_code values of regions (subdivisions) within the same country that border this region.
|
114
118
|
# E.g., for CA-ON, the neighbouring zones are CA-MB, CA-NU and CA-QC.
|
115
119
|
attr_accessor :neighbours
|
@@ -438,7 +442,7 @@ module Worldwide
|
|
438
442
|
end
|
439
443
|
|
440
444
|
def parent_country
|
441
|
-
parents.find(&:country?)
|
445
|
+
@parent_country ||= parents.find(&:country?)
|
442
446
|
end
|
443
447
|
|
444
448
|
# Checks whether the given value is acceptable according to the regular expression defined for the country.
|
data/lib/worldwide/regions.rb
CHANGED
@@ -16,7 +16,7 @@ module Worldwide
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def initialize
|
19
|
-
@regions = RegionsLoader.new.load_regions
|
19
|
+
@regions, @regions_by_cldr_code, @regions_by_iso_code = RegionsLoader.new.load_regions
|
20
20
|
end
|
21
21
|
|
22
22
|
def all
|
@@ -29,17 +29,9 @@ module Worldwide
|
|
29
29
|
end
|
30
30
|
|
31
31
|
result = if cldr
|
32
|
-
|
33
|
-
|
34
|
-
@regions.find do |r|
|
35
|
-
r.send(:answers_to_cldr_code, search_code)
|
36
|
-
end
|
32
|
+
@regions_by_cldr_code[cldr.to_s.upcase]
|
37
33
|
elsif code
|
38
|
-
|
39
|
-
|
40
|
-
@regions.find do |r|
|
41
|
-
r.send(:answers_to_iso_code, search_code) || r.alpha_three == search_code || r.numeric_three == search_code
|
42
|
-
end
|
34
|
+
@regions_by_iso_code[code.to_s.upcase]
|
43
35
|
else # search by name
|
44
36
|
search_name = name.upcase
|
45
37
|
|
@@ -9,6 +9,9 @@ module Worldwide
|
|
9
9
|
WORLD_CODE = "001" # UN code for the whole world
|
10
10
|
|
11
11
|
def load_regions
|
12
|
+
@regions_by_cldr_code = {}
|
13
|
+
@regions_by_iso_code = {}
|
14
|
+
|
12
15
|
# Load country/region definitions out of the db/data/regions/??.yml files
|
13
16
|
@regions = Dir["#{Worldwide::Paths::REGIONS_ROOT}/*.yml"].map do |filename|
|
14
17
|
load_territory(filename)
|
@@ -25,11 +28,39 @@ module Worldwide
|
|
25
28
|
|
26
29
|
construct_unknown
|
27
30
|
|
28
|
-
@regions.
|
31
|
+
@regions.each do |region|
|
32
|
+
construct_lookup_info(region)
|
33
|
+
end
|
34
|
+
|
35
|
+
[@regions.freeze, @regions_by_cldr_code, @regions_by_iso_code]
|
29
36
|
end
|
30
37
|
|
31
38
|
private
|
32
39
|
|
40
|
+
def construct_lookup_info(region)
|
41
|
+
pc = region.send(:parent_country)
|
42
|
+
|
43
|
+
# Remember CLDR code(s) for later use during lookup
|
44
|
+
|
45
|
+
search_code = region.cldr_code.to_s.upcase
|
46
|
+
@regions_by_cldr_code[search_code] = region if Util.present?(search_code)
|
47
|
+
|
48
|
+
@regions_by_cldr_code["#{pc.cldr_code.upcase}#{search_code}"] = region if Util.present?(pc&.cldr_code)
|
49
|
+
|
50
|
+
# Remember ISO 3166 code(s) for later use during lookup
|
51
|
+
|
52
|
+
iso_code = region.iso_code
|
53
|
+
@regions_by_iso_code[iso_code] = region if Util.present?(iso_code)
|
54
|
+
|
55
|
+
@regions_by_iso_code["#{pc.iso_code}-#{iso_code}"] = region if Util.present?(pc)
|
56
|
+
|
57
|
+
alpha_three = region.alpha_three
|
58
|
+
@regions_by_iso_code[alpha_three] = region if Util.present?(alpha_three)
|
59
|
+
|
60
|
+
numeric_three = region.numeric_three
|
61
|
+
@regions_by_iso_code[numeric_three] = region if Util.present?(numeric_three)
|
62
|
+
end
|
63
|
+
|
33
64
|
def apply_hierarchy(parent:, code:, children:)
|
34
65
|
current_region = find_region(code: code)
|
35
66
|
if current_region.nil?
|
@@ -82,10 +113,12 @@ module Worldwide
|
|
82
113
|
region.zip_requirement = spec["zip_requirement"]
|
83
114
|
region.zip_regex = spec["zip_regex"]
|
84
115
|
region.zips_crossing_provinces = spec["zips_crossing_provinces"]
|
116
|
+
region.name_alternates = spec["name_alternates"] || []
|
85
117
|
end
|
86
118
|
|
87
119
|
def apply_zone_attributes(region, zone)
|
88
120
|
region.code_alternates = zone["code_alternates"] || []
|
121
|
+
region.name_alternates = zone["name_alternates"] || []
|
89
122
|
region.example_city = zone["example_city"]
|
90
123
|
region.neighbours = zone["neighboring_zones"]
|
91
124
|
region.zip_prefixes = zone["zip_prefixes"] || []
|
data/lib/worldwide/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worldwide
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shopify
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|