geo_states 0.1.4 → 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 +7 -11
- 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
|
@@ -90,7 +90,8 @@ module GeoStates
|
|
|
90
90
|
state
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
# 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).
|
|
94
95
|
# Returns [lookup_key, preferred_state_code, from_alias].
|
|
95
96
|
# @param iso2 [String] country ISO2
|
|
96
97
|
# @param state_id [String] user input
|
|
@@ -100,16 +101,11 @@ module GeoStates
|
|
|
100
101
|
return [nil, nil, false] if id.empty?
|
|
101
102
|
|
|
102
103
|
from_config = ConfigLoader.resolve_alias(iso2, id)
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
# Built-in: Mexico DF -> CMX (bundled data uses CMX; user config may map CDMX => DF)
|
|
106
|
-
if iso2.to_s.upcase == 'MX' && canonical.to_s.upcase == 'DF'
|
|
107
|
-
return ['CMX', 'DF', true]
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# When from config, use that as preferred and mark as from_alias
|
|
104
|
+
preferred = from_config || id
|
|
111
105
|
from_alias = !from_config.nil?
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
|
|
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]
|
|
114
110
|
end
|
|
115
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.
|