geo_states 0.1.3 → 0.1.5
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/README.md +9 -7
- data/config/geo_states.example.yml +9 -8
- data/lib/geo_states/config_loader.rb +31 -7
- data/lib/geo_states/version.rb +1 -1
- data/lib/geo_states.rb +29 -13
- 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: 8e48781ab93b649cc825612b4f963b053c0398bc7f658d33fff60857331ecf50
|
|
4
|
+
data.tar.gz: 749026242fb43ca2d91cc26670d8ec1670ec965468eb5e35530db729a1b888fa
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 36318233ab28afa96f3174ad0a893c9966e100b78790739758bdd49d69958d7a5937d8a68e446d76e52881d9ed3291286388c59593f9eda34722f81e0b0002d8
|
|
7
|
+
data.tar.gz: 1c63dd25eaba4ff9917b62d4ac9d10523e2da4c6e486ae8f77d39dbfd5bcb43c0898b9022cade367cdc43909b157e33f5daa7239942dc8c34f4b00f82b876400
|
data/README.md
CHANGED
|
@@ -107,15 +107,17 @@ Example `config/geo_states.yml`:
|
|
|
107
107
|
|
|
108
108
|
```yaml
|
|
109
109
|
MX:
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
CDMX: DF
|
|
111
|
+
DF: DF
|
|
112
|
+
DISTRITO FEDERAL: DF
|
|
113
|
+
CIUDAD DE MEXICO: DF
|
|
114
|
+
MEXICO CITY: DF
|
|
115
|
+
_lookup:
|
|
116
|
+
DF: CMX
|
|
116
117
|
```
|
|
117
118
|
|
|
118
|
-
|
|
119
|
+
- Aliases map to your preferred `state_code` (e.g. `DF`).
|
|
120
|
+
- `_lookup` maps that preferred value to the actual `state_code` in the bundled data when they differ (e.g. bundled data has `CMX` for Mexico City).
|
|
119
121
|
|
|
120
122
|
See `config/geo_states.example.yml` for a copy-paste template.
|
|
121
123
|
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
# Copy to config/geo_states.yml or geo_states.yml in your project root.
|
|
3
3
|
# Or set GEO_STATES_CONFIG_PATH to the config file path.
|
|
4
4
|
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
5
|
+
# Aliases map to preferred state_code. Use _lookup when preferred isn't in bundled data.
|
|
6
|
+
# Example: Mexico City has CMX in data; user wants DF in output.
|
|
7
7
|
|
|
8
8
|
MX:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
CDMX: DF
|
|
10
|
+
DF: DF
|
|
11
|
+
DISTRITO FEDERAL: DF
|
|
12
|
+
CIUDAD DE MEXICO: DF
|
|
13
|
+
MEXICO CITY: DF
|
|
14
|
+
_lookup:
|
|
15
|
+
DF: CMX
|
|
@@ -25,26 +25,50 @@ module GeoStates
|
|
|
25
25
|
@state_aliases = nil
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
# Resolve state identifier via user config. Returns
|
|
28
|
+
# Resolve state identifier via user config. Returns alias value or nil.
|
|
29
29
|
# @param country_iso2 [String] e.g. "MX"
|
|
30
30
|
# @param state_id [String] e.g. "CDMX", "DF"
|
|
31
|
-
# @return [String, nil] resolved
|
|
31
|
+
# @return [String, nil] resolved value (e.g. "DF") or nil
|
|
32
32
|
def resolve_alias(country_iso2, state_id)
|
|
33
33
|
return nil if country_iso2.nil? || state_id.nil?
|
|
34
34
|
|
|
35
35
|
iso2 = country_iso2.to_s.upcase
|
|
36
36
|
id = state_id.to_s.strip
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
map = alias_map_for(iso2)
|
|
39
|
+
return nil unless map
|
|
40
|
+
|
|
41
|
+
val = map[id] || map[id.upcase]
|
|
42
|
+
val.to_s if val
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Resolve preferred/alias to actual state_code for lookup when it's not in bundled data.
|
|
46
|
+
# E.g. _lookup: { DF: CMX } — DF (user preference) maps to CMX (in data).
|
|
47
|
+
# @param country_iso2 [String] e.g. "MX"
|
|
48
|
+
# @param preferred [String] e.g. "DF"
|
|
49
|
+
# @return [String, nil] lookup key for bundled data, or nil
|
|
50
|
+
def resolve_lookup(country_iso2, preferred)
|
|
51
|
+
return nil if country_iso2.nil? || preferred.nil?
|
|
52
|
+
|
|
53
|
+
aliases = state_aliases[country_iso2.to_s.upcase]
|
|
54
|
+
return nil unless aliases.is_a?(Hash)
|
|
55
|
+
|
|
56
|
+
lookup = aliases['_lookup'] || aliases['lookup']
|
|
57
|
+
return nil unless lookup.is_a?(Hash)
|
|
58
|
+
|
|
59
|
+
val = lookup[preferred] || lookup[preferred.to_s.upcase]
|
|
60
|
+
val.to_s if val
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Alias map for a country (excludes _lookup / lookup).
|
|
64
|
+
def alias_map_for(country_iso2)
|
|
65
|
+
aliases = state_aliases[country_iso2.to_s.upcase]
|
|
39
66
|
return nil unless aliases.is_a?(Hash)
|
|
40
67
|
|
|
41
|
-
# Support both flat and nested structure:
|
|
42
|
-
# MX: { CDMX: CMX } or MX: { state_aliases: { CDMX: CMX } }
|
|
43
68
|
map = aliases['state_aliases'] || aliases
|
|
44
69
|
return nil unless map.is_a?(Hash)
|
|
45
70
|
|
|
46
|
-
|
|
47
|
-
val.to_s if val
|
|
71
|
+
map.except('_lookup', 'lookup')
|
|
48
72
|
end
|
|
49
73
|
|
|
50
74
|
private
|
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,43 @@ 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
|
-
# Resolve state identifier: user config
|
|
93
|
+
# Resolve state identifier: user config aliases. Use _lookup in config to map
|
|
94
|
+
# preferred values (e.g. DF) to actual state_code in bundled data (e.g. CMX).
|
|
95
|
+
# Returns [lookup_key, preferred_state_code, from_alias].
|
|
82
96
|
# @param iso2 [String] country ISO2
|
|
83
97
|
# @param state_id [String] user input
|
|
84
|
-
# @return [
|
|
98
|
+
# @return [Array] [lookup_key, preferred_state_code, from_alias]
|
|
85
99
|
def self.resolve_state_id(iso2, state_id)
|
|
86
100
|
id = state_id.to_s.strip
|
|
87
|
-
return nil if id.empty?
|
|
101
|
+
return [nil, nil, false] if id.empty?
|
|
88
102
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
103
|
+
from_config = ConfigLoader.resolve_alias(iso2, id)
|
|
104
|
+
preferred = from_config || id
|
|
105
|
+
from_alias = !from_config.nil?
|
|
92
106
|
|
|
93
|
-
|
|
107
|
+
# When preferred isn't in bundled data, check _lookup (e.g. DF => CMX for MX)
|
|
108
|
+
lookup_key = ConfigLoader.resolve_lookup(iso2, preferred) || preferred
|
|
109
|
+
[lookup_key, preferred, from_alias]
|
|
94
110
|
end
|
|
95
111
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: geo_states
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Alberto Osnaya (@elosnaya)
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-02-
|
|
11
|
+
date: 2026-02-21 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Lists countries and their states (or provinces, regions) in a consistent
|
|
14
14
|
format. Data is bundled with the gem.
|