Module: Spree::IsoData

Defined in:
lib/spree/iso_data.rb

Overview

Reference data for countries and their subdivisions, sourced from the countries gem (ISO 3166). Backs Country and State, which are value objects rather than database records.

The gem's raw subdivision list is not usable as-is at checkout, so this module curates it in three ways:

  • SUBDIVISION_TYPES keeps only the subdivision kinds a customer would recognise as a "state" for that country.
  • SUBDIVISION_ADDITIONS restores entries the gem omits but real addresses still use — US military codes, and Italian provinces abolished in 2016.
  • SUBDIVISION_ALIASES maps codes that ISO has since renamed onto their successors, so an address stored years ago still resolves.

Without the last two, upgrading would invalidate addresses that were perfectly valid when they were entered.

Constant Summary collapse

EXCLUDED_COUNTRIES =

Territories, uninhabited islands and Antarctica — never orderable.

%w[AQ AX GS UM HM IO EH BV TF].freeze
SUBDIVISION_TYPES =

Subdivision types to keep, per country. A country absent from this hash keeps every type the gem lists for it.

The US would otherwise carry six outlying areas (Guam, Puerto Rico and friends) alongside the 50 states and DC; China would carry Hong Kong and Macao, which Spree treats as countries in their own right.

{
  'US' => %w[state district],
  'CN' => %w[province municipality autonomous_region]
}.freeze
SUBDIVISION_EXCLUSIONS =

Codes dropped after the type filter. Taiwan is an autonomous region in the gem's Chinese subdivision list and a country everywhere else.

{ 'CN' => %w[TW] }.freeze
SUBDIVISION_ADDITIONS =

Subdivisions the gem does not list but addresses legitimately use.

The US military codes are how APO/FPO mail is addressed and every US checkout accepts them. The Sardinian provinces were abolished in 2016 with no successor, so an address from before then has nowhere else to go.

{
  'US' => {
    'AA' => 'Armed Forces Americas',
    'AE' => 'Armed Forces Europe',
    'AP' => 'Armed Forces Pacific'
  },
  'IT' => {
    'CI' => 'Carbonia-Iglesias',
    'OG' => 'Ogliastra',
    'OT' => 'Olbia-Tempio',
    'VS' => 'Medio Campidano'
  }
}.freeze
LOCALE_TAGGED_NAME =

Some gem subdivision names are a placeholder the gem falls back to when it has no proper name for the entry: the value it would have translated, tagged with the locale it belongs to — Pakistan's "Sind (en)", "Baluchistan (en)". The clean name is the locale's own translation, so a name matching this is replaced rather than shown or matched against.

/\s\([a-z]{2}(?:-[a-z]{2})?\)\z/i
SUBDIVISION_ALIASES =

Retired ISO codes mapped to their current equivalent. Reading an address written under the old code still finds the right subdivision; writing always stores the successor.

{
  'IN' => {
    'CT' => 'CG',  # Chhattisgarh
    'OR' => 'OD',  # Odisha
    'TG' => 'TS',  # Telangana
    'UT' => 'UK',  # Uttarakhand
    'DD' => 'DH',  # Daman and Diu, merged in 2020
    'DN' => 'DH'   # Dadra and Nagar Haveli, merged in 2020
  },
  'IT' => { 'AO' => '23' }, # Valle d'Aosta, now coded as a region
  'ZA' => {
    'GT' => 'GP',  # Gauteng
    'NL' => 'ZN'   # KwaZulu-Natal
  }
}.freeze

Class Method Summary collapse

Class Method Details

.countries ⇒ Array<ISO3166::Country>

Every orderable country, memoized for the life of the process.

Returns:

  • (Array<ISO3166::Country>)


85
86
87
88
89
# File 'lib/spree/iso_data.rb', line 85

def countries
  @countries ||= ISO3166::Country.all.
                 reject { |country| EXCLUDED_COUNTRIES.include?(country.alpha2) }.
                 freeze
end

.country(code) ⇒ ISO3166::Country?

Parameters:

  • code (String) —

    alpha-2 or alpha-3 code, any case

Returns:

  • (ISO3166::Country, nil)


100
101
102
103
104
105
106
107
108
# File 'lib/spree/iso_data.rb', line 100

def country(code)
  return nil if code.blank?

  code = code.to_s.strip.upcase
  found = ISO3166::Country[code] || ISO3166::Country.find_country_by_alpha3(code)
  return nil if found.nil? || EXCLUDED_COUNTRIES.include?(found.alpha2)

  found
end

.country_codes ⇒ Array<String>

The alpha-2 code of every orderable country, the closed list the API types publish as CountryCode.

Returns:

  • (Array<String>)


94
95
96
# File 'lib/spree/iso_data.rb', line 94

def country_codes
  @country_codes ||= countries.map(&:alpha2).sort.freeze
end

.reset! ⇒ void

This method returns an undefined value.

Drops memoized data. Only needed when locale configuration changes after boot, which in practice means specs.



160
161
162
163
# File 'lib/spree/iso_data.rb', line 160

def reset!
  @countries = nil
  @subdivisions = nil
end

.subdivision_code(iso, value) ⇒ String?

Resolves whatever handle a caller has — a current code, a retired one, or a subdivision name — to a current code.

Parameters:

  • iso (String) —

    alpha-2 country code

  • value (String) —

    code or name

Returns:

  • (String, nil) —

    the current subdivision code



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/spree/iso_data.rb', line 130

def subdivision_code(iso, value)
  return nil if value.blank?

  iso = iso.to_s.upcase
  candidate = value.to_s.strip
  available = subdivisions(iso)

  upcased = candidate.upcase
  return upcased if available.key?(upcased)

  aliased = SUBDIVISION_ALIASES.dig(iso, upcased)
  return aliased if aliased && available.key?(aliased)

  by_name = available.find { |_code, name| name.casecmp?(candidate) }
  return by_name.first if by_name

  # Falls back to the gem, which also matches translated names.
  country(iso)&.find_subdivision_by_name(candidate)&.code.presence
end

.subdivision_name(iso, code) ⇒ String?

Returns the subdivision's name.

Parameters:

  • iso (String) —

    alpha-2 country code

  • code (String) —

    subdivision code

Returns:

  • (String, nil) —

    the subdivision's name



153
154
155
# File 'lib/spree/iso_data.rb', line 153

def subdivision_name(iso, code)
  subdivisions(iso)[code.to_s.upcase]
end

.subdivisions(iso) ⇒ Hash{String => String}

The curated subdivisions for a country, as { code => name }, ordered by name.

Keyed by locale as well as country: the names are localized, and this cache outlives a request, so keying on the country alone would serve whichever locale happened to warm it to everyone else.

Parameters:

  • iso (String) —

    alpha-2 country code

Returns:

  • (Hash{String => String})


118
119
120
121
122
# File 'lib/spree/iso_data.rb', line 118

def subdivisions(iso)
  iso = iso.to_s.upcase
  @subdivisions ||= {}
  @subdivisions[[I18n.locale, iso]] ||= build_subdivisions(iso).freeze
end