class Territorial

Expand regional territory shorthand codes to ISO 3166-1 alpha-2 codes and parse lists of string codes like "EU -FR"

Constants

EXPANSIONS

The default region expansions. EL and UK are European Union variants of the ISO codes GR and GB respectively.

VERSION

Public Class Methods

expand(*regions) click to toggle source

expand regions from shorthand codes to arrays of ISO codes @return [<String>] the expanded ISO codes

# File lib/territorial.rb, line 35
def self.expand(*regions)
  new.expand(*regions)
end
new(extra_expansions = {}, bounds = []) click to toggle source

@param extra_expansions [Hash] optional Hash of additional expansions

# File lib/territorial.rb, line 40
def initialize(extra_expansions = {}, bounds = [])
  @extra_expansions = Hash[extra_expansions.map { |key, values|
    [normalize_territory(key), normalize_territories(values)]
  }]
  if bounds.empty?
    @bounds = nil
  else
    @bounds = expanded_set(normalize_territories(bounds))
  end
end

Public Instance Methods

expand(*regions) click to toggle source

expand regions from shorthand codes to arrays of ISO codes @return [<String>] the expanded ISO codes

# File lib/territorial.rb, line 53
def expand(*regions)
  regions = normalize_territories(regions.flatten)
  expanded_set(regions).to_a
end
territories(territory_list) click to toggle source

Parse strings like 'EU -GB' or 'EU +RU' to fully expanded arrays of ISO codes. Expandable codes (i.e. EU) will be expanded, codes with a `-` prefix will be removed from the result and codes with a `+` prefix, or with no prefix will be added to the result. @param territory_list [String] the list of territories (and modifiers) as a string @return [<String>] the expanded ISO codes

# File lib/territorial.rb, line 64
def territories(territory_list)
  parser = Parser.new(territory_list)
  allowed = expanded_set(parser.accept)
  rejected = expanded_set(parser.reject)
  (allowed - rejected).to_a
end

Private Instance Methods

constrained(territories) click to toggle source
# File lib/territorial.rb, line 93
def constrained(territories)
  return territories if @bounds.nil?
  territories & @bounds
end
expand_set(territories, regions, already_expanded) click to toggle source
# File lib/territorial.rb, line 106
def expand_set(territories, regions, already_expanded)
  regions.inject(territories) do |territories, region|
    if expandable?(region) && !already_expanded.include?(region)
      territories.merge(expand_set(territories, expansions[region], already_expanded << region))
    else
      territories << region
    end
  end
end
expandable?(region) click to toggle source
# File lib/territorial.rb, line 89
def expandable?(region)
  expansions.has_key?(region.to_s)
end
expanded_set(regions) click to toggle source
# File lib/territorial.rb, line 98
def expanded_set(regions)
  territories = Set.new
  already_expanded = Set.new

  expand_set(territories, regions, already_expanded)
  constrained(territories)
end
expansion_regions() click to toggle source
# File lib/territorial.rb, line 85
def expansion_regions
  expansions.keys
end
expansions() click to toggle source
# File lib/territorial.rb, line 81
def expansions
  @expansions ||= EXPANSIONS.merge(@extra_expansions)
end
normalize_territories(territory_strings) click to toggle source
# File lib/territorial.rb, line 77
def normalize_territories(territory_strings)
  territory_strings.map { |ts| normalize_territory(ts) }
end
normalize_territory(territory_string) click to toggle source
# File lib/territorial.rb, line 73
def normalize_territory(territory_string)
  territory_string.to_s.upcase
end