addressing 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +2 -2
- data/lib/addressing/address.rb +0 -1
- data/lib/addressing/address_field.rb +2 -1
- data/lib/addressing/address_format.rb +11 -15
- data/lib/addressing/address_validator.rb +5 -14
- data/lib/addressing/administrative_area_type.rb +2 -2
- data/lib/addressing/blank.rb +1 -4
- data/lib/addressing/country.rb +6 -8
- data/lib/addressing/data_source.rb +2 -0
- data/lib/addressing/default_formatter.rb +13 -16
- data/lib/addressing/dependent_locality_type.rb +2 -2
- data/lib/addressing/enum.rb +0 -6
- data/lib/addressing/exceptions.rb +0 -4
- data/lib/addressing/field_override.rb +5 -1
- data/lib/addressing/lazy_subdivisions.rb +2 -0
- data/lib/addressing/locale.rb +8 -20
- data/lib/addressing/locality_type.rb +2 -2
- data/lib/addressing/model.rb +3 -3
- data/lib/addressing/postal_code_type.rb +2 -2
- data/lib/addressing/postal_label_formatter.rb +5 -9
- data/lib/addressing/subdivision.rb +6 -19
- data/lib/addressing/version.rb +1 -1
- data/lib/addressing.rb +3 -10
- 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: 893f9179e07f53cfb3d7667fdb6431b5d1b7b96b0463e2a5f8ee116bbda4607b
|
|
4
|
+
data.tar.gz: 119b7884c278336e0945b4dabcd5ddf584c0e49e8fc19615a6d7597b38cc7d7c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 51c9afa56d6be4ef429c7479b639fbf3fd8fea986b7c7f369710a5e6462c06b7ceb0136a253c6c66f11594b002a23ce72403e4da855caab81975a871731ea46d
|
|
7
|
+
data.tar.gz: acca4e24686c9ea710511fefabc350db32c1127a406c87c77fd492f25a38693f8c346665c71636e28beabce51ce59f862fbd12ed03ebb3beb16adf62e3f8348c
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `addressing` will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [2.2.1](https://github.com/robinvdvleuten/addressing/compare/v2.2.0...v2.2.1) (2026-12-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* add the postal code prefix only to a postal code ([aeb8c0c](https://github.com/robinvdvleuten/addressing/commit/aeb8c0c489ee5e50c4dfb74eee07525a00867236))
|
|
11
|
+
* apply field overrides given as symbols ([ff5197d](https://github.com/robinvdvleuten/addressing/commit/ff5197d7bccf128b6255bd7ec7c27bb5b717806e))
|
|
12
|
+
* format nil address fields as blank ([cafbbd5](https://github.com/robinvdvleuten/addressing/commit/cafbbd5692a0220100880498355721b8d3c48c60))
|
|
13
|
+
* sync the tag in data/UPSTREAM_VERSION when no tag is given ([c4ea732](https://github.com/robinvdvleuten/addressing/commit/c4ea7327a2c7b64c10a10d42b12e4e56b168722d))
|
|
14
|
+
* use the format when an address format has no local format ([c992bad](https://github.com/robinvdvleuten/addressing/commit/c992bad497eee69003197aea4177143400b09c4b))
|
|
15
|
+
* verify child files under a subdivision written as [] ([b052cf9](https://github.com/robinvdvleuten/addressing/commit/b052cf9bc860992573dc634f9387a712c7cde1a9))
|
|
16
|
+
* verify the address formats of the directory being verified ([e33e0fc](https://github.com/robinvdvleuten/addressing/commit/e33e0fc7ff475fffb1abb8fb529d499ae62496cb))
|
|
17
|
+
|
|
5
18
|
## [2.2.0](https://github.com/robinvdvleuten/addressing/compare/v2.1.0...v2.2.0) (2026-10-01)
|
|
6
19
|
|
|
7
20
|
|
data/README.md
CHANGED
|
@@ -317,9 +317,9 @@ bundle install
|
|
|
317
317
|
bundle exec rake test
|
|
318
318
|
```
|
|
319
319
|
|
|
320
|
-
Refreshing the country and address data from upstream (`rake addressing:generate`) additionally requires PHP, since it reads the definitions out of the commerceguys library. It syncs the
|
|
320
|
+
Refreshing the country and address data from upstream (`rake addressing:generate`) additionally requires PHP, since it reads the definitions out of the commerceguys library. It syncs the tag in `data/UPSTREAM_VERSION`, the one the shipped data comes from, or another tag with `rake "addressing:generate[v2.3.2]"`, and writes only to `data/`. A sync of another tag updates `data/UPSTREAM_VERSION`.
|
|
321
321
|
|
|
322
|
-
Upstream maintains the subdivisions and address formats by hand, so the data files can disagree with each other. `rake addressing:verify` reports such discrepancies, and runs at the end of every refresh and in the
|
|
322
|
+
Upstream maintains the subdivisions and address formats by hand, so the data files can disagree with each other. `rake addressing:verify` reports such discrepancies, and runs at the end of every refresh, in the Verify workflow and in the test suite. Report a discrepancy upstream instead of editing the data files here, and add it to `tasks/known_discrepancies.yml` until the fix arrives.
|
|
323
323
|
|
|
324
324
|
Feel free to open an issue to get feedback on your idea before spending too much time on it.
|
|
325
325
|
|
data/lib/addressing/address.rb
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/address_field"
|
|
4
|
+
require "addressing/administrative_area_type"
|
|
5
|
+
require "addressing/dependent_locality_type"
|
|
6
|
+
require "addressing/locality_type"
|
|
7
|
+
require "addressing/postal_code_type"
|
|
8
|
+
|
|
3
9
|
module Addressing
|
|
4
10
|
# Provides address format information for countries.
|
|
5
11
|
#
|
|
@@ -30,10 +36,10 @@ module Addressing
|
|
|
30
36
|
"locality"
|
|
31
37
|
].freeze,
|
|
32
38
|
subdivision_fields: [].freeze,
|
|
33
|
-
administrative_area_type:
|
|
34
|
-
locality_type:
|
|
35
|
-
dependent_locality_type:
|
|
36
|
-
postal_code_type:
|
|
39
|
+
administrative_area_type: AdministrativeAreaType.default,
|
|
40
|
+
locality_type: LocalityType.default,
|
|
41
|
+
dependent_locality_type: DependentLocalityType.default,
|
|
42
|
+
postal_code_type: PostalCodeType.default
|
|
37
43
|
}.freeze
|
|
38
44
|
private_constant :GENERIC_DEFINITION
|
|
39
45
|
|
|
@@ -54,8 +60,6 @@ module Addressing
|
|
|
54
60
|
|
|
55
61
|
private
|
|
56
62
|
|
|
57
|
-
# Gets the address formats by country code.
|
|
58
|
-
#
|
|
59
63
|
# All are built when the data is loaded, so that every call returns
|
|
60
64
|
# the same instance for a country.
|
|
61
65
|
def address_formats
|
|
@@ -70,10 +74,8 @@ module Addressing
|
|
|
70
74
|
end
|
|
71
75
|
|
|
72
76
|
def process_definition(definition)
|
|
73
|
-
# Merge-in defaults.
|
|
74
77
|
definition = GENERIC_DEFINITION.merge(definition)
|
|
75
78
|
|
|
76
|
-
# Always require the given name and family name.
|
|
77
79
|
definition[:required_fields] = definition[:required_fields] | [AddressField::GIVEN_NAME, AddressField::FAMILY_NAME]
|
|
78
80
|
|
|
79
81
|
# The address formats are shared, so callers must not be able to
|
|
@@ -105,14 +107,12 @@ module Addressing
|
|
|
105
107
|
end
|
|
106
108
|
|
|
107
109
|
def initialize(definition = {})
|
|
108
|
-
# Validate the presence of required properties.
|
|
109
110
|
[:country_code, :format].each do |required_property|
|
|
110
111
|
if definition[required_property].nil?
|
|
111
112
|
raise ArgumentError, "Missing required property #{required_property}."
|
|
112
113
|
end
|
|
113
114
|
end
|
|
114
115
|
|
|
115
|
-
# Add defaults for properties that are allowed to be empty.
|
|
116
116
|
definition = {
|
|
117
117
|
locale: nil,
|
|
118
118
|
local_format: nil,
|
|
@@ -140,7 +140,6 @@ module Addressing
|
|
|
140
140
|
@default_values = definition[:default_values]
|
|
141
141
|
@subdivision_fields = definition[:subdivision_fields]
|
|
142
142
|
|
|
143
|
-
# A type is only set when the format uses its field.
|
|
144
143
|
{
|
|
145
144
|
administrative_area_type: [AddressField::ADMINISTRATIVE_AREA, AdministrativeAreaType],
|
|
146
145
|
locality_type: [AddressField::LOCALITY, LocalityType],
|
|
@@ -159,14 +158,11 @@ module Addressing
|
|
|
159
158
|
end
|
|
160
159
|
end
|
|
161
160
|
|
|
162
|
-
# Gets the list of used fields.
|
|
163
161
|
def used_fields
|
|
164
162
|
@used_fields ||= AddressField.all.values.select { |field| @format.include?("%#{field}") }
|
|
165
163
|
end
|
|
166
164
|
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
# Note that a country might use a subdivision field without having
|
|
165
|
+
# A country might use a subdivision field without having
|
|
170
166
|
# predefined subdivisions for it, see #subdivision_fields.
|
|
171
167
|
def used_subdivision_fields
|
|
172
168
|
SUBDIVISION_FIELDS & used_fields
|
|
@@ -33,19 +33,19 @@ module Addressing
|
|
|
33
33
|
# @param verify_postal_code [Boolean] Whether to check the postal code against its pattern
|
|
34
34
|
# @return [Array<FieldViolation>] The field violations, empty when the address is valid
|
|
35
35
|
def validate(address, field_overrides: FieldOverrides.new({}), verify_postal_code: true)
|
|
36
|
-
return [violation(:country_code, :blank)] if blank?(address.country_code)
|
|
36
|
+
return [violation(:country_code, :blank)] if Blank.blank?(address.country_code)
|
|
37
37
|
|
|
38
38
|
address_format = AddressFormat.get(address.country_code)
|
|
39
39
|
violations = []
|
|
40
40
|
|
|
41
41
|
required_fields(address_format, field_overrides).each do |field|
|
|
42
|
-
violations << violation(field, :blank) if blank?(address.send(field))
|
|
42
|
+
violations << violation(field, :blank) if Blank.blank?(address.send(field))
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
used_fields = address_format.used_fields - field_overrides.hidden_fields
|
|
46
46
|
|
|
47
47
|
(AddressField.all.values - used_fields).each do |field|
|
|
48
|
-
violations << violation(field, :present) unless blank?(address.send(field))
|
|
48
|
+
violations << violation(field, :present) unless Blank.blank?(address.send(field))
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
subdivision_fields = address_format.subdivision_fields
|
|
@@ -68,31 +68,22 @@ module Addressing
|
|
|
68
68
|
FieldViolation.new(field: field.to_sym, kind: kind)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
# Whether the postal code matches the pattern of the last subdivision in
|
|
72
|
-
# the chain that has one, or else the pattern of the address format.
|
|
73
71
|
def valid_postal_code?(postal_code, subdivisions, address_format)
|
|
74
|
-
|
|
75
|
-
return true if blank?(postal_code)
|
|
72
|
+
return true if Blank.blank?(postal_code)
|
|
76
73
|
|
|
77
74
|
pattern = subdivisions.inject(address_format.postal_code_pattern) do |pattern, subdivision|
|
|
78
|
-
blank?(subdivision.postal_code_pattern) ? pattern : subdivision.postal_code_pattern
|
|
75
|
+
Blank.blank?(subdivision.postal_code_pattern) ? pattern : subdivision.postal_code_pattern
|
|
79
76
|
end
|
|
80
77
|
return true unless pattern
|
|
81
78
|
|
|
82
|
-
# The pattern must match the provided value completely.
|
|
83
79
|
match = postal_code.match(Regexp.new(pattern, "i"))
|
|
84
80
|
!match.nil? && match[0] == postal_code
|
|
85
81
|
end
|
|
86
82
|
|
|
87
|
-
# Applies the field overrides to the required fields of the address format.
|
|
88
83
|
def required_fields(address_format, field_overrides)
|
|
89
84
|
required_fields = address_format.required_fields - field_overrides.optional_fields - field_overrides.hidden_fields
|
|
90
85
|
(required_fields + field_overrides.required_fields).uniq
|
|
91
86
|
end
|
|
92
|
-
|
|
93
|
-
def blank?(value)
|
|
94
|
-
Blank.blank?(value)
|
|
95
|
-
end
|
|
96
87
|
end
|
|
97
88
|
end
|
|
98
89
|
end
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/enum"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
|
-
# Enumerates available administrative area types.
|
|
5
6
|
class AdministrativeAreaType < Enum
|
|
6
7
|
AREA = "area"
|
|
7
8
|
CANTON = "canton"
|
|
@@ -17,7 +18,6 @@ module Addressing
|
|
|
17
18
|
REGION = "region"
|
|
18
19
|
STATE = "state"
|
|
19
20
|
|
|
20
|
-
# Gets the default value.
|
|
21
21
|
def self.default
|
|
22
22
|
PROVINCE
|
|
23
23
|
end
|
data/lib/addressing/blank.rb
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Addressing
|
|
4
|
-
# Decides whether a value is blank.
|
|
5
4
|
module Blank
|
|
6
|
-
# Matches a value that holds nothing but whitespace, Unicode included.
|
|
7
5
|
PATTERN = /\A[[:space:]]*\z/
|
|
8
6
|
private_constant :PATTERN
|
|
9
7
|
|
|
10
|
-
#
|
|
11
|
-
# when it is loaded.
|
|
8
|
+
# Follows ActiveSupport's blank?.
|
|
12
9
|
def self.blank?(value)
|
|
13
10
|
return value.blank? if value.respond_to?(:blank?)
|
|
14
11
|
|
data/lib/addressing/country.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module Addressing
|
|
4
4
|
# Provides country information including names, codes, currency, and timezones.
|
|
5
5
|
#
|
|
6
|
-
# Country names
|
|
6
|
+
# Country names come from CLDR, in many locales.
|
|
7
7
|
#
|
|
8
8
|
# @example Get a country by code
|
|
9
9
|
# brazil = Addressing::Country.get('BR')
|
|
@@ -73,12 +73,10 @@ module Addressing
|
|
|
73
73
|
)
|
|
74
74
|
end
|
|
75
75
|
|
|
76
|
-
# Loads the country definitions for the provided locale.
|
|
77
76
|
def load_definitions(locale)
|
|
78
77
|
Addressing.data_source.fetch("country/#{locale}")
|
|
79
78
|
end
|
|
80
79
|
|
|
81
|
-
# Gets the locales that have country names, one data file each.
|
|
82
80
|
def available_locales
|
|
83
81
|
Addressing.data_source.names("country")
|
|
84
82
|
end
|
|
@@ -94,7 +92,6 @@ module Addressing
|
|
|
94
92
|
attr_reader :country_code, :name, :three_letter_code, :numeric_code, :currency_code, :locale
|
|
95
93
|
|
|
96
94
|
def initialize(definition = {})
|
|
97
|
-
# Validate the presence of required properties.
|
|
98
95
|
[:country_code, :name, :locale].each do |required_property|
|
|
99
96
|
if definition[required_property].nil?
|
|
100
97
|
raise ArgumentError, "Missing required property #{required_property}."
|
|
@@ -109,15 +106,16 @@ module Addressing
|
|
|
109
106
|
@locale = definition[:locale]
|
|
110
107
|
end
|
|
111
108
|
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
# Note that a country can span more than one timezone.
|
|
109
|
+
# A country can span more than one timezone.
|
|
115
110
|
# For example, Germany has ["Europe/Berlin", "Europe/Busingen"].
|
|
111
|
+
#
|
|
112
|
+
# Needs the tzinfo gem, which is not a dependency of this gem.
|
|
116
113
|
def timezones
|
|
114
|
+
require "tzinfo"
|
|
115
|
+
|
|
117
116
|
@timezones ||= TZInfo::Country.get(@country_code).zone_identifiers
|
|
118
117
|
end
|
|
119
118
|
|
|
120
|
-
# Gets the string representation of the Country.
|
|
121
119
|
def to_s
|
|
122
120
|
@country_code
|
|
123
121
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "cgi"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
6
|
class DefaultFormatter
|
|
5
7
|
DEFAULT_LOCALE = "en"
|
|
@@ -30,7 +32,7 @@ module Addressing
|
|
|
30
32
|
|
|
31
33
|
# Add the country to the bottom or the top of the format string,
|
|
32
34
|
# depending on whether the format is minor-to-major or major-to-minor.
|
|
33
|
-
format_string = if Locale.match_candidates(address_format.locale, address.locale)
|
|
35
|
+
format_string = if address_format.local_format && Locale.match_candidates(address_format.locale, address.locale)
|
|
34
36
|
"%country\n" + address_format.local_format
|
|
35
37
|
else
|
|
36
38
|
address_format.format + "\n%country"
|
|
@@ -45,7 +47,6 @@ module Addressing
|
|
|
45
47
|
|
|
46
48
|
if options[:html]
|
|
47
49
|
output = output.gsub("\n", "<br>\n")
|
|
48
|
-
# Add the HTML wrapper element.
|
|
49
50
|
output = render_html_element(value: "\n#{output}\n", html_tag: options[:html_tag], html_attributes: options[:html_attributes])
|
|
50
51
|
end
|
|
51
52
|
|
|
@@ -54,10 +55,8 @@ module Addressing
|
|
|
54
55
|
|
|
55
56
|
protected
|
|
56
57
|
|
|
57
|
-
# Builds the view for the given address.
|
|
58
58
|
def build_view(address, address_format, options)
|
|
59
|
-
|
|
60
|
-
values = values(address, address_format).merge("country" => countries.fetch(address.country_code, address.country_code))
|
|
59
|
+
values = values(address, address_format).merge("country" => country_name(address.country_code, options[:locale]))
|
|
61
60
|
used_fields = address_format.used_fields + ["country"]
|
|
62
61
|
|
|
63
62
|
used_fields.to_h do |field|
|
|
@@ -65,12 +64,15 @@ module Addressing
|
|
|
65
64
|
end
|
|
66
65
|
end
|
|
67
66
|
|
|
68
|
-
# Gets the country list for a locale, with caching.
|
|
69
67
|
def country_list(locale)
|
|
70
68
|
@country_list_cache[locale] ||= Country.list(locale)
|
|
71
69
|
end
|
|
72
70
|
|
|
73
|
-
#
|
|
71
|
+
# The country code stands in for an unknown country.
|
|
72
|
+
def country_name(country_code, locale)
|
|
73
|
+
country_list(locale).fetch(country_code, country_code)
|
|
74
|
+
end
|
|
75
|
+
|
|
74
76
|
def render_view(view)
|
|
75
77
|
view.transform_values do |element|
|
|
76
78
|
next "" if element[:value].empty?
|
|
@@ -133,21 +135,19 @@ module Addressing
|
|
|
133
135
|
end.join("\n")
|
|
134
136
|
end
|
|
135
137
|
|
|
136
|
-
# Removes empty lines, leading punctuation, excess whitespace.
|
|
137
138
|
def clean_output(output)
|
|
138
139
|
output.split("\n").map { |line| line.gsub(LEADING_TRAILING_PUNCTUATION_PATTERN, "").strip.gsub(MULTIPLE_SPACES_PATTERN, " ") }.reject(&:empty?).join("\n")
|
|
139
140
|
end
|
|
140
141
|
|
|
141
|
-
# Gets the address values used to build the view.
|
|
142
142
|
def values(address, address_format)
|
|
143
143
|
values = extract_address_values(address)
|
|
144
144
|
resolve_subdivision_values(values, address, address_format)
|
|
145
145
|
values
|
|
146
146
|
end
|
|
147
147
|
|
|
148
|
-
# Extracts all address field values.
|
|
149
148
|
def extract_address_values(address)
|
|
150
|
-
|
|
149
|
+
# A nil field is blank, like an empty string.
|
|
150
|
+
AddressField.all.values.to_h { |field| [field, address.send(field).to_s] }
|
|
151
151
|
end
|
|
152
152
|
|
|
153
153
|
# Replaces the subdivision values with the codes of any predefined ones.
|
|
@@ -158,16 +158,13 @@ module Addressing
|
|
|
158
158
|
fields.zip(chain.subdivisions) do |field, subdivision|
|
|
159
159
|
break if subdivision.nil?
|
|
160
160
|
|
|
161
|
-
# Replace the value with the expected code.
|
|
162
161
|
use_local_name = Locale.match_candidates(address.locale, subdivision.locale)
|
|
163
162
|
values[field] = use_local_name ? subdivision.local_code : subdivision.code
|
|
164
163
|
end
|
|
165
164
|
end
|
|
166
165
|
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
# Ensures the absence of unknown keys, correct data types and values.
|
|
170
|
-
# A subclass with more options extends this and calls super.
|
|
166
|
+
# A subclass with more options adds them to DEFAULT_OPTIONS, extends this
|
|
167
|
+
# and calls super.
|
|
171
168
|
def assert_options(options)
|
|
172
169
|
options.each do |option, value|
|
|
173
170
|
unless self.class::DEFAULT_OPTIONS.key?(option)
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/enum"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
|
-
# Enumerates available dependent locality types.
|
|
5
6
|
class DependentLocalityType < Enum
|
|
6
7
|
DISTRICT = "district"
|
|
7
8
|
NEIGHBORHOOD = "neighborhood"
|
|
@@ -9,7 +10,6 @@ module Addressing
|
|
|
9
10
|
SUBURB = "suburb"
|
|
10
11
|
TOWNLAND = "townland"
|
|
11
12
|
|
|
12
|
-
# Gets the default value.
|
|
13
13
|
def self.default
|
|
14
14
|
SUBURB
|
|
15
15
|
end
|
data/lib/addressing/enum.rb
CHANGED
|
@@ -3,28 +3,22 @@
|
|
|
3
3
|
module Addressing
|
|
4
4
|
class Enum
|
|
5
5
|
class << self
|
|
6
|
-
# Gets all available values.
|
|
7
6
|
def all
|
|
8
|
-
# Each subclass has its own @all.
|
|
9
7
|
@all ||= constants.to_h { |constant| [constant, const_get(constant)] }
|
|
10
8
|
end
|
|
11
9
|
|
|
12
|
-
# Gets the key of the provided value.
|
|
13
10
|
def key(value)
|
|
14
11
|
all.key(value)
|
|
15
12
|
end
|
|
16
13
|
|
|
17
|
-
# Checks whether the provided value is defined.
|
|
18
14
|
def exists?(value)
|
|
19
15
|
all.has_value?(value)
|
|
20
16
|
end
|
|
21
17
|
|
|
22
|
-
# Asserts that the provided value is defined.
|
|
23
18
|
def assert_exists(value)
|
|
24
19
|
raise ArgumentError, "#{value} is not a valid #{name} value." unless exists?(value)
|
|
25
20
|
end
|
|
26
21
|
|
|
27
|
-
# Asserts that all provided values are defined.
|
|
28
22
|
def assert_all_exist(values)
|
|
29
23
|
values.each { |value| assert_exists(value.to_s) }
|
|
30
24
|
end
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Addressing
|
|
4
|
-
# Base error class for all Addressing errors
|
|
5
4
|
class Error < StandardError; end
|
|
6
5
|
|
|
7
|
-
# Raised when an unknown country code is provided
|
|
8
6
|
class UnknownCountryError < Error
|
|
9
7
|
def initialize(country_code)
|
|
10
8
|
super("Unknown country code: #{country_code}")
|
|
@@ -14,7 +12,6 @@ module Addressing
|
|
|
14
12
|
attr_reader :country_code
|
|
15
13
|
end
|
|
16
14
|
|
|
17
|
-
# Raised when an unknown locale is provided
|
|
18
15
|
class UnknownLocaleError < Error
|
|
19
16
|
def initialize(locale)
|
|
20
17
|
super("Unknown locale: #{locale}")
|
|
@@ -24,6 +21,5 @@ module Addressing
|
|
|
24
21
|
attr_reader :locale
|
|
25
22
|
end
|
|
26
23
|
|
|
27
|
-
# Raised when an invalid argument is provided
|
|
28
24
|
class InvalidArgumentError < Error; end
|
|
29
25
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/enum"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
6
|
class FieldOverride < Enum
|
|
5
7
|
HIDDEN = "hidden"
|
|
@@ -11,10 +13,12 @@ module Addressing
|
|
|
11
13
|
attr_reader :hidden_fields, :optional_fields, :required_fields
|
|
12
14
|
|
|
13
15
|
def initialize(definition)
|
|
16
|
+
# Fields and overrides are compared as strings, like the constants.
|
|
17
|
+
definition = definition.transform_keys(&:to_s).transform_values(&:to_s)
|
|
18
|
+
|
|
14
19
|
AddressField.assert_all_exist(definition.keys)
|
|
15
20
|
FieldOverride.assert_all_exist(definition.values)
|
|
16
21
|
|
|
17
|
-
# Group fields by their override type
|
|
18
22
|
grouped = definition.group_by { |_field, override| override }
|
|
19
23
|
.transform_values { |pairs| pairs.map(&:first) }
|
|
20
24
|
|
data/lib/addressing/locale.rb
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
module Addressing
|
|
4
4
|
class Locale
|
|
5
|
-
# Checks whether two locales match.
|
|
6
5
|
def self.match(first_locale, second_locale)
|
|
7
6
|
return false if first_locale.to_s.empty? || second_locale.to_s.empty?
|
|
8
7
|
|
|
@@ -12,47 +11,39 @@ module Addressing
|
|
|
12
11
|
# Checks whether two locales have at least one common candidate.
|
|
13
12
|
#
|
|
14
13
|
# For example, "de" and "de-AT" will match because they both have
|
|
15
|
-
# "de" in common.
|
|
14
|
+
# "de" in common.
|
|
16
15
|
def self.match_candidates(first_locale, second_locale)
|
|
17
16
|
return false if first_locale.to_s.empty? || second_locale.to_s.empty?
|
|
18
17
|
|
|
19
18
|
(candidates(canonicalize(first_locale)) & candidates(canonicalize(second_locale))).any?
|
|
20
19
|
end
|
|
21
20
|
|
|
22
|
-
#
|
|
21
|
+
# Gets the first candidate of the locale, then of the fallback locale,
|
|
22
|
+
# that is available.
|
|
23
23
|
#
|
|
24
|
-
#
|
|
25
|
-
# and fallback locale, searches for them in the available
|
|
26
|
-
# locale list. The first found locale is returned.
|
|
27
|
-
# If no candidate is found in the list, an error is raised.
|
|
24
|
+
# @raise [UnknownLocaleError] if none is available
|
|
28
25
|
def self.resolve(available_locales, locale, fallback_locale = nil)
|
|
29
26
|
locale = canonicalize(locale)
|
|
30
27
|
resolved_locale = candidates(locale, fallback_locale).find { |candidate| available_locales.include?(candidate) }
|
|
31
28
|
|
|
32
|
-
# No locale could be resolved, stop here.
|
|
33
29
|
raise UnknownLocaleError.new(locale) if resolved_locale.nil?
|
|
34
30
|
|
|
35
31
|
resolved_locale
|
|
36
32
|
end
|
|
37
33
|
|
|
38
|
-
# Canonicalizes the given locale.
|
|
39
|
-
#
|
|
40
34
|
# Standardizes separators and capitalization, turning
|
|
41
35
|
# a locale such as "sr_rs_latn" into "sr-RS-Latn".
|
|
42
36
|
def self.canonicalize(locale)
|
|
43
37
|
return locale if locale.to_s.empty?
|
|
44
38
|
|
|
45
|
-
# Lowercase the locale and replace all dashes with underscores.
|
|
46
39
|
locale_parts = locale.downcase.tr("_", "-").split("-")
|
|
47
40
|
|
|
48
41
|
locale_parts.map.with_index do |part, index|
|
|
49
42
|
# The language code should stay lowercase.
|
|
50
43
|
next part if index == 0
|
|
51
44
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
next part.capitalize
|
|
55
|
-
end
|
|
45
|
+
# A script code.
|
|
46
|
+
next part.capitalize if part.length == 4
|
|
56
47
|
|
|
57
48
|
# Country or variant code.
|
|
58
49
|
part.upcase
|
|
@@ -66,9 +57,8 @@ module Addressing
|
|
|
66
57
|
# 2) bs-Cyrl
|
|
67
58
|
# 3) bs
|
|
68
59
|
#
|
|
69
|
-
# The locale is de-aliased
|
|
70
|
-
#
|
|
71
|
-
# 2) sr
|
|
60
|
+
# The locale is de-aliased first: the candidates for "sh" start with
|
|
61
|
+
# "sr-Latn".
|
|
72
62
|
def self.candidates(locale, fallback_locale = nil)
|
|
73
63
|
candidates = lineage(replace_alias(locale))
|
|
74
64
|
candidates += lineage(fallback_locale) if fallback_locale
|
|
@@ -83,7 +73,6 @@ module Addressing
|
|
|
83
73
|
end
|
|
84
74
|
private_class_method :lineage
|
|
85
75
|
|
|
86
|
-
# Gets the parent for the given locale.
|
|
87
76
|
def self.parent(locale)
|
|
88
77
|
parent = definitions["parents"].fetch(locale) { locale.include?("-") ? locale.rpartition("-").first : nil }
|
|
89
78
|
|
|
@@ -99,7 +88,6 @@ module Addressing
|
|
|
99
88
|
definitions["aliases"].fetch(locale, locale)
|
|
100
89
|
end
|
|
101
90
|
|
|
102
|
-
# Gets the locale aliases and parents.
|
|
103
91
|
def self.definitions
|
|
104
92
|
Addressing.data_source.fetch("locale")
|
|
105
93
|
end
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/enum"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
|
-
# Enumerates available locality types.
|
|
5
6
|
class LocalityType < Enum
|
|
6
7
|
CITY = "city"
|
|
7
8
|
DISTRICT = "district"
|
|
@@ -9,7 +10,6 @@ module Addressing
|
|
|
9
10
|
SUBURB = "suburb"
|
|
10
11
|
TOWN_CITY = "town_city"
|
|
11
12
|
|
|
12
|
-
# Gets the default value.
|
|
13
13
|
def self.default
|
|
14
14
|
CITY
|
|
15
15
|
end
|
data/lib/addressing/model.rb
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
module Addressing
|
|
4
4
|
module Model
|
|
5
|
-
# The error message for each kind of field violation.
|
|
6
5
|
FIELD_VIOLATION_MESSAGES = {
|
|
7
6
|
blank: "should not be blank",
|
|
8
7
|
present: "should be blank",
|
|
@@ -14,7 +13,8 @@ module Addressing
|
|
|
14
13
|
fields: Address::FIELDS, field_overrides: nil, verify_postal_code: true, **options
|
|
15
14
|
)
|
|
16
15
|
fields = Array(fields)
|
|
17
|
-
|
|
16
|
+
# The validator has the default field overrides.
|
|
17
|
+
validator_options = {field_overrides: field_overrides}.compact.merge(verify_postal_code: verify_postal_code)
|
|
18
18
|
|
|
19
19
|
# Without change tracking, as on a plain ActiveModel class, validate every time.
|
|
20
20
|
options[:if] ||= -> { !respond_to?(:changes) || fields.any? { |f| changes.key?(f.to_s) } } unless options[:unless]
|
|
@@ -27,7 +27,7 @@ module Addressing
|
|
|
27
27
|
values = fields.each_with_object({}) { |f, v| v[f] = send(f) if respond_to?(f) }
|
|
28
28
|
address = Address.new(**values)
|
|
29
29
|
|
|
30
|
-
AddressValidator.validate(address,
|
|
30
|
+
AddressValidator.validate(address, **validator_options).each do |violation|
|
|
31
31
|
errors.add(violation.field, violation.kind, message: FIELD_VIOLATION_MESSAGES.fetch(violation.kind))
|
|
32
32
|
end
|
|
33
33
|
end
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/enum"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
|
-
# Enumerates available postal code types.
|
|
5
6
|
class PostalCodeType < Enum
|
|
6
7
|
EIR = "eircode"
|
|
7
8
|
PIN = "pin"
|
|
8
9
|
POSTAL = "postal"
|
|
9
10
|
ZIP = "zip"
|
|
10
11
|
|
|
11
|
-
# Gets the default value.
|
|
12
12
|
def self.default
|
|
13
13
|
POSTAL
|
|
14
14
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "addressing/default_formatter"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
6
|
class PostalLabelFormatter < DefaultFormatter
|
|
5
7
|
DEFAULT_OPTIONS = DefaultFormatter::DEFAULT_OPTIONS.merge(html: false, origin_country: "")
|
|
@@ -20,18 +22,12 @@ module Addressing
|
|
|
20
22
|
def build_view(address, address_format, options)
|
|
21
23
|
view = super
|
|
22
24
|
view.each do |key, element|
|
|
23
|
-
# Uppercase fields where required by the format.
|
|
24
25
|
element[:value] = element[:value].upcase if address_format.uppercase_fields.include?(key)
|
|
25
26
|
end
|
|
26
27
|
|
|
27
|
-
# Handle international mailing.
|
|
28
28
|
if address.country_code != options[:origin_country].upcase
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if view.key?(field)
|
|
33
|
-
view[field][:value] = [address_format.postal_code_prefix, view[field][:value]].join
|
|
34
|
-
end
|
|
29
|
+
postal_code = view[AddressField::POSTAL_CODE]
|
|
30
|
+
postal_code[:value] = "#{address_format.postal_code_prefix}#{postal_code[:value]}" if postal_code && !postal_code[:value].empty?
|
|
35
31
|
|
|
36
32
|
# Universal Postal Union says: "The name of the country of
|
|
37
33
|
# destination shall be written preferably in the language of the
|
|
@@ -39,7 +35,7 @@ module Addressing
|
|
|
39
35
|
# transit, it is desirable for the name of the country of
|
|
40
36
|
# destination to be added in an internationally known language.
|
|
41
37
|
country = view["country"][:value]
|
|
42
|
-
english_country =
|
|
38
|
+
english_country = country_name(address.country_code, "en")
|
|
43
39
|
|
|
44
40
|
if country != english_country
|
|
45
41
|
country += " - #{english_country}"
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
3
5
|
module Addressing
|
|
4
6
|
# Represents administrative subdivisions within countries.
|
|
5
7
|
#
|
|
@@ -32,7 +34,7 @@ module Addressing
|
|
|
32
34
|
# subdivision without children.
|
|
33
35
|
#
|
|
34
36
|
# @param country_code [String] Country code
|
|
35
|
-
# @param values [Array<String, nil>] Values
|
|
37
|
+
# @param values [Array<String, nil>] Values of the subdivision fields of the address format, in order
|
|
36
38
|
# @return [Chain]
|
|
37
39
|
#
|
|
38
40
|
# @example
|
|
@@ -40,10 +42,9 @@ module Addressing
|
|
|
40
42
|
# chain.subdivisions.map(&:code) # => ["CE", "Fortaleza"]
|
|
41
43
|
# chain.unmatched_level # => nil
|
|
42
44
|
def chain(country_code, values)
|
|
43
|
-
parents = [country_code
|
|
45
|
+
parents = [country_code]
|
|
44
46
|
subdivisions = []
|
|
45
47
|
|
|
46
|
-
# Nothing to match against.
|
|
47
48
|
return Chain.new(subdivisions: subdivisions, unmatched_level: nil) if load_definitions(parents).empty?
|
|
48
49
|
|
|
49
50
|
values.each_with_index do |value, level|
|
|
@@ -54,7 +55,6 @@ module Addressing
|
|
|
54
55
|
return Chain.new(subdivisions: subdivisions, unmatched_level: level) if subdivision.nil?
|
|
55
56
|
|
|
56
57
|
subdivisions << subdivision
|
|
57
|
-
# No predefined subdivisions below this level, stop here.
|
|
58
58
|
break unless subdivision.children?
|
|
59
59
|
|
|
60
60
|
parents += [value]
|
|
@@ -70,7 +70,6 @@ module Addressing
|
|
|
70
70
|
# @return [Subdivision, nil] Subdivision instance or nil if not found
|
|
71
71
|
def get(id, parents)
|
|
72
72
|
definitions = load_definitions(parents)
|
|
73
|
-
# No matching definition found.
|
|
74
73
|
return nil unless definitions.dig("subdivisions", id)
|
|
75
74
|
|
|
76
75
|
create_subdivision_from_definitions(id, definitions, load_parent(definitions))
|
|
@@ -84,7 +83,6 @@ module Addressing
|
|
|
84
83
|
definitions = load_definitions(parents)
|
|
85
84
|
return {} if definitions.empty?
|
|
86
85
|
|
|
87
|
-
# The siblings share one parent, so it is loaded once.
|
|
88
86
|
parent = load_parent(definitions)
|
|
89
87
|
definitions["subdivisions"].keys.to_h { |id| [id, create_subdivision_from_definitions(id, definitions, parent)] }
|
|
90
88
|
end
|
|
@@ -103,7 +101,7 @@ module Addressing
|
|
|
103
101
|
definitions = load_definitions(parents)
|
|
104
102
|
return {} if definitions.empty?
|
|
105
103
|
|
|
106
|
-
use_local_name = Locale.match_candidates(locale, definitions["locale"]
|
|
104
|
+
use_local_name = Locale.match_candidates(locale, definitions["locale"])
|
|
107
105
|
|
|
108
106
|
definitions["subdivisions"].transform_values { |definition| use_local_name ? definition["local_name"] : definition["name"] }
|
|
109
107
|
end
|
|
@@ -145,16 +143,11 @@ module Addressing
|
|
|
145
143
|
|
|
146
144
|
protected
|
|
147
145
|
|
|
148
|
-
# Loads the subdivision definitions for the provided parents.
|
|
149
|
-
#
|
|
150
|
-
# A subdivision group without a data file has no definitions.
|
|
151
146
|
def load_definitions(parents)
|
|
152
147
|
Addressing.data_source.fetch("subdivision/#{group_key(parents)}") { |definitions| process_definitions(definitions) } || {}
|
|
153
148
|
end
|
|
154
149
|
|
|
155
|
-
#
|
|
156
|
-
#
|
|
157
|
-
# Adds keys and values that were removed from the JSON files for brevity.
|
|
150
|
+
# Adds the keys and values that the JSON files leave out for brevity.
|
|
158
151
|
def process_definitions(definitions)
|
|
159
152
|
# Definitions of the wrong shape are treated as if they didn't exist.
|
|
160
153
|
# The data verifier reports them, malformed JSON raises in the data source.
|
|
@@ -164,7 +157,6 @@ module Addressing
|
|
|
164
157
|
# Upstream writes a definition without keys as an empty JSON array.
|
|
165
158
|
definition = definitions["subdivisions"][id] = {} unless definition.is_a?(Hash)
|
|
166
159
|
|
|
167
|
-
# Add common keys from the root level.
|
|
168
160
|
definition["country_code"] = definitions["country_code"]
|
|
169
161
|
definition["id"] = id
|
|
170
162
|
|
|
@@ -196,18 +188,15 @@ module Addressing
|
|
|
196
188
|
definitions
|
|
197
189
|
end
|
|
198
190
|
|
|
199
|
-
# Loads the parent of a subdivision group, if known.
|
|
200
191
|
def load_parent(definitions)
|
|
201
192
|
parents = parents_of(definitions)
|
|
202
193
|
get(parents[-1], parents[0...-1]) if parents.size > 1
|
|
203
194
|
end
|
|
204
195
|
|
|
205
|
-
# Creates a subdivision object from the provided definitions.
|
|
206
196
|
def create_subdivision_from_definitions(id, definitions, parent)
|
|
207
197
|
definition = definitions["subdivisions"][id]
|
|
208
198
|
parents = parents_of(definitions)
|
|
209
199
|
|
|
210
|
-
# Prepare children.
|
|
211
200
|
children = definition["has_children"] ? LazySubdivisions.new(parents + [id]) : {}
|
|
212
201
|
|
|
213
202
|
new(
|
|
@@ -228,14 +217,12 @@ module Addressing
|
|
|
228
217
|
attr_reader :id, :parent, :country_code, :locale, :code, :local_code, :name, :local_name, :postal_code_pattern, :children
|
|
229
218
|
|
|
230
219
|
def initialize(definition = {})
|
|
231
|
-
# Validate the presence of required properties.
|
|
232
220
|
[:country_code, :id, :code, :name].each do |required_property|
|
|
233
221
|
if definition[required_property].nil?
|
|
234
222
|
raise ArgumentError, "Missing required property #{required_property}."
|
|
235
223
|
end
|
|
236
224
|
end
|
|
237
225
|
|
|
238
|
-
# Add defaults for properties that are allowed to be empty.
|
|
239
226
|
definition = {
|
|
240
227
|
parent: nil,
|
|
241
228
|
locale: nil,
|
data/lib/addressing/version.rb
CHANGED
data/lib/addressing.rb
CHANGED
|
@@ -1,24 +1,17 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
# stdlib
|
|
4
|
-
require "cgi"
|
|
5
|
-
require "digest"
|
|
6
|
-
require "forwardable"
|
|
7
|
-
require "json"
|
|
8
|
-
|
|
9
|
-
# modules
|
|
10
|
-
require "addressing/exceptions"
|
|
11
|
-
require "addressing/enum"
|
|
12
|
-
require "addressing/blank"
|
|
13
3
|
require "addressing/address"
|
|
14
4
|
require "addressing/address_field"
|
|
15
5
|
require "addressing/address_format"
|
|
16
6
|
require "addressing/address_validator"
|
|
17
7
|
require "addressing/administrative_area_type"
|
|
8
|
+
require "addressing/blank"
|
|
18
9
|
require "addressing/country"
|
|
19
10
|
require "addressing/data_source"
|
|
20
11
|
require "addressing/default_formatter"
|
|
21
12
|
require "addressing/dependent_locality_type"
|
|
13
|
+
require "addressing/enum"
|
|
14
|
+
require "addressing/exceptions"
|
|
22
15
|
require "addressing/field_override"
|
|
23
16
|
require "addressing/field_violation"
|
|
24
17
|
require "addressing/lazy_subdivisions"
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: addressing
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.2.
|
|
4
|
+
version: 2.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Robin van der Vleuten
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description:
|
|
14
14
|
email: robinvdvleuten@gmail.com
|