Class: UKPostcode::GeographicPostcode

Inherits:
AbstractPostcode show all
Defined in:
lib/uk_postcode/geographic_postcode.rb

Overview

GeographicPostcode models the majority of postcodes, containing an area, a district, a sector, and a unit.

Despite the name, it also handles non-geographic postcodes that follow the geographic format.

Constant Summary collapse

PATTERN =
/
  \A ( [A-PR-UWYZ01][A-HJ-Z0]? )     # area
  (?: ( [0-9IO][0-9A-HJKMNPR-YIO]? ) # district
    (?: \s* ( [0-9IO] )              # sector
      ( [ABD-HJLNPQ-Z]{2} )? )? )?   # unit
  \Z
/ix

Constants inherited from AbstractPostcode

AbstractPostcode::NotImplemented

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractPostcode

#full_valid?

Constructor Details

#initialize(area, district = nil, sector = nil, unit = nil) ⇒ GeographicPostcode

Initialise a new GeographicPostcode instance with the given area, district, sector, and unit. Only area is required.



36
37
38
39
40
41
# File 'lib/uk_postcode/geographic_postcode.rb', line 36

def initialize(area, district = nil, sector = nil, unit = nil)
  @area = letters(area)
  @district = digits(district)
  @sector = digits(sector)
  @unit = letters(unit)
end

Instance Attribute Details

#areaObject (readonly)

Returns the value of attribute area.



43
44
45
# File 'lib/uk_postcode/geographic_postcode.rb', line 43

def area
  @area
end

#districtObject (readonly)

Returns the value of attribute district.



43
44
45
# File 'lib/uk_postcode/geographic_postcode.rb', line 43

def district
  @district
end

#sectorObject (readonly)

Returns the value of attribute sector.



43
44
45
# File 'lib/uk_postcode/geographic_postcode.rb', line 43

def sector
  @sector
end

#unitObject (readonly)

Returns the value of attribute unit.



43
44
45
# File 'lib/uk_postcode/geographic_postcode.rb', line 43

def unit
  @unit
end

Class Method Details

.parse(str) ⇒ Object

Attempts to parse the postcode given in str, and returns an instance of GeographicPostcode on success, or nil on failure.



24
25
26
27
28
29
30
31
# File 'lib/uk_postcode/geographic_postcode.rb', line 24

def self.parse(str)
  matched = PATTERN.match(str.strip)
  if matched
    new(*(1..4).map { |i| matched[i] })
  else
    nil
  end
end

Instance Method Details

#countryObject

Find the country associated with the postcode. Possible values are :england, :scotland, :wales, :northern_ireland, :isle_of_man, :channel_islands, or :unknown.

Note that, due to limitations in the underlying data, the country might not always be correct in border regions.



84
85
86
# File 'lib/uk_postcode/geographic_postcode.rb', line 84

def country
  CountryFinder.country(self)
end

#full?Boolean

Returns true if the postcode is a valid full postcode (e.g. W1A 1AA)

Returns:

  • (Boolean)


67
68
69
# File 'lib/uk_postcode/geographic_postcode.rb', line 67

def full?
  area && district && sector && unit && true
end

#incodeObject

The right-hand part of the postcode, e.g. W1A 1AA -> 1AA



54
55
56
57
# File 'lib/uk_postcode/geographic_postcode.rb', line 54

def incode
  return nil unless sector && unit
  [sector, unit].join('')
end

#outcodeObject

The left-hand part of the postcode, e.g. W1A 1AA -> W1A



47
48
49
50
# File 'lib/uk_postcode/geographic_postcode.rb', line 47

def outcode
  return nil unless district
  [area, district].join('')
end

#to_sObject

Returns the canonical string representation of the postcode.



61
62
63
# File 'lib/uk_postcode/geographic_postcode.rb', line 61

def to_s
  [area, district, " ", sector, unit].compact.join('').strip
end

#valid?Boolean

Any geographic postcode is assumed to be valid

Returns:

  • (Boolean)


73
74
75
# File 'lib/uk_postcode/geographic_postcode.rb', line 73

def valid?
  true
end