Module: CAPostalCode::RegionGuessing

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

Constant Summary collapse

REGIONS =
{
  NL: ?A,
  NS: ?B,
  PE: ?C,
  NB: ?E,
  QC: [?G, ?H, ?J],
  ON: [?K, ?L, ?M, ?N, ?P],
  MB: ?R,
  SK: ?S,
  AB: ?T,
  BC: ?V,
  NU: /^X0[A-C]/,
  NT: [/^X0[EG]/, /^X1A/],
  YT: ?Y,
}

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.



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

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

  entry[1] if entry
end

#region_patternsObject

Lists the patterns the library will use. It is made public only to make the implementation visible.



33
34
35
# File 'lib/ca_postal_code/region_guessing.rb', line 33

def region_patterns
  @@region_patterns
end