Module: GovKit::CA::PostalCode
- Defined in:
- lib/gov_kit-ca/postal_code.rb,
lib/gov_kit-ca/postal_code/strategy_set.rb,
lib/gov_kit-ca/postal_code/strategy/base.rb,
lib/gov_kit-ca/postal_code/strategy/ndp_ca.rb,
lib/gov_kit-ca/postal_code/strategy/liberal_ca.rb,
lib/gov_kit-ca/postal_code/strategy/parl_gc_ca.rb,
lib/gov_kit-ca/postal_code/strategy/elections_ca.rb,
lib/gov_kit-ca/postal_code/strategy/greenparty_ca.rb,
lib/gov_kit-ca/postal_code/strategy/conservative_ca.rb,
lib/gov_kit-ca/postal_code/strategy/digital-copyright_ca.rb
Overview
A collection of postal code helpers.
Defined Under Namespace
Modules: Strategy, StrategySet
Class Method Summary collapse
-
.find_electoral_districts_by_postal_code(postal_code) ⇒ Array<Fixnum>
Returns the electoral districts within a postal code.
-
.find_province_by_postal_code(postal_code) ⇒ String
Returns the province that a postal code belongs to.
-
.format_postal_code(postal_code) ⇒ String
Formats a postal code as A1A1A1.
-
.valid?(postal_code) ⇒ Boolean
Whether the postal code is properly formatted.
Class Method Details
.find_electoral_districts_by_postal_code(postal_code) ⇒ Array<Fixnum>
Returns the electoral districts within a postal code.
Statistics Canada charges for its Postal Codes by Federal Ridings File (PCFRF). A free alternative requires scraping data from other sources.
23 24 25 26 27 28 29 |
# File 'lib/gov_kit-ca/postal_code.rb', line 23 def self.find_electoral_districts_by_postal_code(postal_code) if valid?(format_postal_code(postal_code)) StrategySet.run format_postal_code(postal_code) else raise InvalidRequest, "The postal code is not properly formatted" end end |
.find_province_by_postal_code(postal_code) ⇒ String
Returns the province that a postal code belongs to.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gov_kit-ca/postal_code.rb', line 36 def self.find_province_by_postal_code(postal_code) case format_postal_code(postal_code) when /\AA/ 'Newfoundland and Labrador' when /\AB/ 'Nova Scotia' when /\AC/ 'Prince Edward Island' when /\AE/ 'New Brunswick' when /\A[GHJ]/ 'Quebec' when /\A[KLMNP]/ 'Ontario' when /\AR/ 'Manitoba' when /\AS/ 'Saskatchewan' when /\AT/ 'Alberta' when /\AV/ 'British Columbia' # https://en.wikipedia.org/wiki/List_of_X_postal_codes_of_Canada when /\AX0[ABC]/ 'Nunavut' when /\AX0[EG]/, /\AX1A/ 'Northwest Territories' when /\AY/ 'Yukon' else raise ResourceNotFound, "The province cannot be determined from the postal code" end end |
.format_postal_code(postal_code) ⇒ String
Formats a postal code as A1A1A1. Removes non-alphanumeric characters.
73 74 75 |
# File 'lib/gov_kit-ca/postal_code.rb', line 73 def self.format_postal_code(postal_code) postal_code.upcase.gsub(/[^A-Z0-9]/, '') end |
.valid?(postal_code) ⇒ Boolean
Returns whether the postal code is properly formatted.
9 10 11 |
# File 'lib/gov_kit-ca/postal_code.rb', line 9 def self.valid?(postal_code) !!postal_code.match(/\A[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]\z/) end |