Module: CAPostalCode::RegionGuessing

Included in:
CAPostalCode
Defined in:
lib/ca_postal_code/region_guessing.rb

Constant Summary collapse

PATTERNS =
{
  /^A/ => "NL",
  /^B/ => "NS",
  /^C/ => "PE",
  /^E/ => "NB",
  /^[GHJ]/ => "QC",
  /^[KLMNP]/ => "ON",
  /^R/ => "MB",
  /^S/ => "SK",
  /^T/ => "AB",
  /^V/ => "BC",
  /^X0[A-C]/ => "NU",
  /^X0[EG]|^X1A/ => "NT",
  /^Y/ => "YT",
}

Instance Method Summary collapse

Instance Method Details

#guess_region(string) ⇒ Object

Guesses province or territory based on postal code first characters.

Output is defined for valid, normalized postal codes only.



22
23
24
25
26
27
28
# File 'lib/ca_postal_code/region_guessing.rb', line 22

def guess_region(string)
  entry = PATTERNS.detect do |pattern, region|
    pattern.match?(string)
  end

  entry[1] if entry
end