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

Class Method Summary collapse

Instance Attribute Details

#previous_pathObject

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.

Parameters:

  • postcode (String)

    the string to be hyphenated.

Returns:

  • (String)

    a hyphenated string.



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.

Parameters:

  • raw_input (String)

    the data inputted by the user.

Returns:

  • (Parliament::Response::NTripleResponse, Parliament::ClientError, Parliament::ServerError)

    if successful, a response is returned, otherwise an error is returned.



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_pathString

Returns the previous path variable.

Returns:

  • (String)

    previous_path the path to redirect back to in the case of an error.



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.

Returns:

  • (String)

    previous_path the path to redirect back to in the case of an error.



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.

Parameters:

  • postcode (String)

    the string to be processed.

Returns:

  • (String)

    the restored postcode with hyphen replaced by whitespace.



38
39
40
# File 'lib/parliament/utils/helpers/postcode_helper.rb', line 38

def self.unhyphenate(postcode)
  postcode.tr('-', ' ')
end