Module: CAPostalCode

Extended by:
RegionGuessing
Defined in:
lib/ca_postal_code.rb,
lib/ca_postal_code/version.rb,
lib/ca_postal_code/region_guessing.rb

Defined Under Namespace

Modules: RegionGuessing

Constant Summary collapse

PATTERN =

> Postal codes do not include the letters D, F, I, O, Q or U, > and the first position also does not make use of the letters W or Z.

%r{
  [ABCEGHJKLMNPRSTVXY]
  [0-9]
  [ABCEGHJKLMNPRSTVWXYZ]
  \ ?
  [0-9]
  [ABCEGHJKLMNPRSTVWXYZ]
  [0-9]
}x
VERSION =
"0.2.0"

Constants included from RegionGuessing

RegionGuessing::REGIONS

Class Method Summary collapse

Methods included from RegionGuessing

guess_region, region_patterns

Class Method Details

.normalize(string) ⇒ Object

Normalize postal code into “A1A 1A1” format.

Will return nil if the normalized string does not have 6 characters.



31
32
33
34
35
36
37
38
39
40
# File 'lib/ca_postal_code.rb', line 31

def self.normalize(string)
  string.strip!
  string.upcase!
  string.gsub!(/\W/, '')

  return unless string.length == 6

  string = string[0..2] + " " + string[3..5]
  string
end

.strict_valid?(string) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/ca_postal_code.rb', line 24

def self.strict_valid?(string)
  string.match? PATTERN
end

.valid?(string) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/ca_postal_code.rb', line 19

def self.valid?(string)
  return false unless string = normalize(string)
  strict_valid?(string)
end