geo_states 0.1.3 → 0.1.4
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/lib/geo_states/version.rb +1 -1
- data/lib/geo_states.rb +31 -11
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 23f5adf00ed40e770fefae1b152febf5de3f6cf0e3fd0db046f749040372582a
|
|
4
|
+
data.tar.gz: 9bd7ee1dcb2378dc300d8ac94a0aa08fb76bce1d74daa0629865607efe0e27cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f141eeec003f3b685b15ea5f1d56c0c529f167426cba1e989bbee07df14dcd539dc50dc8b2b37ab5ca45328d54ee5a95f9f8f944f1d0c8cc044626f56c8e1096
|
|
7
|
+
data.tar.gz: 0ea2455867a0d1e41eb3e82c2c5630aadb5b54fc62043af7c8bbd5ffe948d6f1f15d16ec41839f3ee97ec6e0f7c3351348d314d3f329e0f8d82b34a8472c8ceb
|
data/lib/geo_states/version.rb
CHANGED
data/lib/geo_states.rb
CHANGED
|
@@ -59,8 +59,9 @@ module GeoStates
|
|
|
59
59
|
country&.dig(:states) || []
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
# Find a state within a country. Accepts user YAML config for aliases (e.g. CDMX =>
|
|
62
|
+
# Find a state within a country. Accepts user YAML config for aliases (e.g. CDMX => DF).
|
|
63
63
|
# Config file: config/geo_states.yml or geo_states.yml, or GEO_STATES_CONFIG_PATH env.
|
|
64
|
+
# When resolved via alias (config or built-in), the returned hash uses the alias as state_code (e.g. "DF").
|
|
64
65
|
# @param country_id [String] ISO2, ISO3, M49, or country name (e.g. "MX", "Mexico")
|
|
65
66
|
# @param state_id [String] state_code, name, or alias from user config (e.g. "CMX", "CDMX", "Mexico City")
|
|
66
67
|
# @return [Hash, nil] state hash with :name, :state_code, :iso2, :iso3, or nil
|
|
@@ -68,28 +69,47 @@ module GeoStates
|
|
|
68
69
|
country = find_country(country_id)
|
|
69
70
|
return nil unless country
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
return nil if
|
|
72
|
+
lookup_key, preferred_state_code, from_alias = resolve_state_id(country[:iso2], state_id.to_s)
|
|
73
|
+
return nil if lookup_key.nil? || lookup_key.empty?
|
|
73
74
|
|
|
74
75
|
states = country[:states] || []
|
|
75
|
-
states.find do |s|
|
|
76
|
-
s[:state_code].to_s.casecmp(
|
|
77
|
-
s[:name].to_s.casecmp(
|
|
76
|
+
state = states.find do |s|
|
|
77
|
+
s[:state_code].to_s.casecmp(lookup_key) == 0 ||
|
|
78
|
+
s[:name].to_s.casecmp(lookup_key) == 0
|
|
78
79
|
end
|
|
80
|
+
return nil unless state
|
|
81
|
+
|
|
82
|
+
# Use alias value (e.g. DF) in result when resolved via config or built-in alias
|
|
83
|
+
if from_alias && preferred_state_code && preferred_state_code != state[:state_code]
|
|
84
|
+
state = state.merge(
|
|
85
|
+
state_code: preferred_state_code,
|
|
86
|
+
iso3: preferred_state_code,
|
|
87
|
+
iso2: preferred_state_code.length >= 2 ? preferred_state_code[0, 2].upcase : preferred_state_code.upcase
|
|
88
|
+
)
|
|
89
|
+
end
|
|
90
|
+
state
|
|
79
91
|
end
|
|
80
92
|
|
|
81
93
|
# Resolve state identifier: user config first, then built-in aliases (e.g. DF -> CMX for Mexico).
|
|
94
|
+
# Returns [lookup_key, preferred_state_code, from_alias].
|
|
82
95
|
# @param iso2 [String] country ISO2
|
|
83
96
|
# @param state_id [String] user input
|
|
84
|
-
# @return [
|
|
97
|
+
# @return [Array] [lookup_key, preferred_state_code, from_alias]
|
|
85
98
|
def self.resolve_state_id(iso2, state_id)
|
|
86
99
|
id = state_id.to_s.strip
|
|
87
|
-
return nil if id.empty?
|
|
100
|
+
return [nil, nil, false] if id.empty?
|
|
101
|
+
|
|
102
|
+
from_config = ConfigLoader.resolve_alias(iso2, id)
|
|
103
|
+
canonical = from_config || id
|
|
88
104
|
|
|
89
|
-
canonical = ConfigLoader.resolve_alias(iso2, id) || id
|
|
90
105
|
# Built-in: Mexico DF -> CMX (bundled data uses CMX; user config may map CDMX => DF)
|
|
91
|
-
|
|
106
|
+
if iso2.to_s.upcase == 'MX' && canonical.to_s.upcase == 'DF'
|
|
107
|
+
return ['CMX', 'DF', true]
|
|
108
|
+
end
|
|
92
109
|
|
|
93
|
-
|
|
110
|
+
# When from config, use that as preferred and mark as from_alias
|
|
111
|
+
from_alias = !from_config.nil?
|
|
112
|
+
preferred = from_config || canonical
|
|
113
|
+
[canonical, preferred, from_alias]
|
|
94
114
|
end
|
|
95
115
|
end
|