Module: Parliament::Utils::Helpers::PostcodeHelper
- Includes:
- TranslationHelper
- Defined in:
- lib/parliament/utils/helpers/postcode_helper.rb
Defined Under Namespace
Classes: PostcodeError
Instance Attribute Summary collapse
-
#previous_path ⇒ Object
Returns the value of attribute previous_path.
Class Method Summary collapse
-
.clean_input(raw_input) ⇒ Object
We decided to use a loose regex - this matches the different postcode formats, but doesn’t validate against invalid letters and numbers, only their positions.
-
.hyphenate(postcode) ⇒ String
Replaces whitespace with a hyphen.
-
.lookup(raw_input) ⇒ Parliament::Response::NTripleResponse, ...
Makes requests to the postcode_lookup endpoint.
-
.previous_path ⇒ String
Returns the previous path variable.
-
.previous_path=(path) ⇒ String
Sets the previous path variable.
-
.unhyphenate(postcode) ⇒ String
Replaces a hyphen with whitespace, restoring a postcode to its usual format.
Instance Attribute Details
#previous_path ⇒ Object
Returns the value of attribute previous_path.
11 12 13 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 11 def previous_path @previous_path end |
Class Method Details
.clean_input(raw_input) ⇒ Object
We decided to use a loose regex - this matches the different postcode formats, but doesn’t validate against invalid letters and numbers, only their positions.
57 58 59 60 61 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 57 def self.clean_input(raw_input) postcode = raw_input.gsub(/\s+/, '').upcase postcode.match(/[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/).nil? ? raise(PostcodeError, I18n.t('error.postcode_invalid').capitalize) : postcode end |
.hyphenate(postcode) ⇒ String
Replaces whitespace with a hyphen.
31 32 33 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 31 def self.hyphenate(postcode) postcode.strip.gsub(/\s+/, '-') end |
.lookup(raw_input) ⇒ Parliament::Response::NTripleResponse, ...
Makes requests to the postcode_lookup endpoint.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 16 def self.lookup(raw_input) postcode = clean_input(raw_input) begin Parliament::Utils::Helpers::ParliamentHelper.parliament_request.constituency_lookup_by_postcode.get(params: { postcode: postcode }) rescue Parliament::ClientError raise(PostcodeError, I18n.t('error.no_constituency').capitalize) rescue Parliament::ServerError raise(PostcodeError, I18n.t('error.lookup_unavailable').capitalize) end end |
.previous_path ⇒ String
Returns the previous path variable.
44 45 46 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 44 def self.previous_path @previous_path end |
.previous_path=(path) ⇒ String
Sets the previous path variable.
50 51 52 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 50 def self.previous_path=(path) @previous_path = path end |
.unhyphenate(postcode) ⇒ String
Replaces a hyphen with whitespace, restoring a postcode to its usual format.
38 39 40 |
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 38 def self.unhyphenate(postcode) postcode.tr('-', ' ') end |