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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b757132bddec9472e730189fa998a1ef255440bb12eec76f407c9242b07b685
4
- data.tar.gz: 0b5f64fde99b17656d2ced4965c5f766dbcc9b9f533d7ee14389ce83e0c93baa
3
+ metadata.gz: 23f5adf00ed40e770fefae1b152febf5de3f6cf0e3fd0db046f749040372582a
4
+ data.tar.gz: 9bd7ee1dcb2378dc300d8ac94a0aa08fb76bce1d74daa0629865607efe0e27cb
5
5
  SHA512:
6
- metadata.gz: 29f3e3ccc65d2b2f7b5c03f2ec807e754dc4f3c3a4d79b940a2bb91dd32b485e7d5da378a62d511c7e3999cb14babdc661f0b310c7c053870dc4e49a81d897fc
7
- data.tar.gz: b06009b62f7580a61cbaba751652c820963046b116d334c0208e45eec37316e45d031c8974caeffd6bb1051f88ed7f0ac28e2cb3bd25c03421526739099c3196
6
+ metadata.gz: f141eeec003f3b685b15ea5f1d56c0c529f167426cba1e989bbee07df14dcd539dc50dc8b2b37ab5ca45328d54ee5a95f9f8f944f1d0c8cc044626f56c8e1096
7
+ data.tar.gz: 0ea2455867a0d1e41eb3e82c2c5630aadb5b54fc62043af7c8bbd5ffe948d6f1f15d16ec41839f3ee97ec6e0f7c3351348d314d3f329e0f8d82b34a8472c8ceb
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GeoStates
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
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 => CMX).
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
- canonical = resolve_state_id(country[:iso2], state_id.to_s)
72
- return nil if canonical.nil? || canonical.empty?
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(canonical) == 0 ||
77
- s[:name].to_s.casecmp(canonical) == 0
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 [String] canonical state_code for lookup
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
- return 'CMX' if iso2.to_s.upcase == 'MX' && canonical.to_s.upcase == 'DF'
106
+ if iso2.to_s.upcase == 'MX' && canonical.to_s.upcase == 'DF'
107
+ return ['CMX', 'DF', true]
108
+ end
92
109
 
93
- canonical
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_states
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alberto Osnaya (@elosnaya)