-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significant restructuring for version 2
Note changes to API as documented in README.md.
- Loading branch information
1 parent
ba71cd6
commit a37c0ee
Showing
20 changed files
with
737 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,19 @@ | ||
require 'uk_postcode/country' | ||
|
||
class UKPostcode | ||
MATCH = /\A \s* (?: | ||
( G[I1]R \s* [0O]AA ) # special postcode | ||
| | ||
( [A-PR-UWYZ01][A-Z01]? ) # area | ||
( [0-9IO][0-9A-HJKMNPR-YIO]? ) # district | ||
(?: \s* | ||
( [0-9IO] ) # sector | ||
( [ABD-HJLNPQ-Z10]{2} ) # unit | ||
)? | ||
) \s* \Z/x | ||
|
||
attr_reader :raw | ||
|
||
# Initialise a new UKPostcode instance from the given postcode string | ||
# | ||
def initialize(postcode_as_string) | ||
@raw = postcode_as_string | ||
end | ||
|
||
# Return the country corresponding to a full or partial postcode | ||
# Note that some outcodes (e.g. CA6) are shared between countries, in which | ||
# case only a full postcode will return the actual country. | ||
# | ||
# Country is one of :england, :scotland, :wales, :northern_ireland, | ||
# :channel_islands, or :isle_of_man. | ||
# | ||
def country | ||
Country.new(self).country | ||
end | ||
|
||
# Returns true if the postcode is a valid full postcode (e.g. W1A 1AA) or outcode (e.g. W1A) | ||
# | ||
def valid? | ||
!!outcode | ||
end | ||
|
||
# Returns true if the postcode is a valid full postcode (e.g. W1A 1AA) | ||
# | ||
def full? | ||
!!(outcode && incode) | ||
end | ||
|
||
# The left-hand part of the postcode, e.g. W1A 1AA -> W1A | ||
# | ||
def outcode | ||
area && district && [area, district].join | ||
end | ||
|
||
# The right-hand part of the postcode, e.g. W1A 1AA -> 1AA | ||
# | ||
def incode | ||
sector && unit && [sector, unit].join | ||
end | ||
|
||
# The first part of the outcode, e.g. W1A 2AB -> W | ||
# | ||
def area | ||
parts[0] | ||
end | ||
|
||
# The second part of the outcode, e.g. W1A 2AB -> 1A | ||
# | ||
def district | ||
parts[1] | ||
end | ||
|
||
# The first part of the incode, e.g. W1A 2AB -> 2 | ||
# | ||
def sector | ||
parts[2] | ||
end | ||
require "uk_postcode/version" | ||
require "uk_postcode/geographic_postcode" | ||
require "uk_postcode/giro_postcode" | ||
require "uk_postcode/invalid_postcode" | ||
|
||
# The second part of the incode, e.g. W1A 2AB -> AB | ||
# | ||
def unit | ||
parts[3] | ||
end | ||
module UKPostcode | ||
module_function | ||
|
||
# Render the postcode as a normalised string, i.e. in upper case and with spacing. | ||
# Returns an empty string if the postcode is not valid. | ||
# Attempt to parse the string str as a postcode. Returns an object | ||
# representing the postcode, or an InvalidPostcode if the string cannot be | ||
# parsed. | ||
# | ||
def norm | ||
[outcode, incode].compact.join(" ") | ||
end | ||
alias_method :normalise, :norm | ||
alias_method :normalize, :norm | ||
|
||
alias_method :to_s, :raw | ||
alias_method :to_str, :raw | ||
|
||
def inspect(*args) | ||
"<#{self.class.to_s} #{raw}>" | ||
end | ||
|
||
private | ||
def parts | ||
return @parts if @matched | ||
|
||
@matched = true | ||
matches = raw.upcase.match(MATCH) || [] | ||
if matches[1] | ||
@parts = %w[ G IR 0 AA ] | ||
else | ||
a, b, c, d = (2..5).map{ |i| matches[i] } | ||
if a =~ /^[A-Z][I1]$/ | ||
a = a[0, 1] | ||
b = "1" + b | ||
end | ||
@parts = [letters(a), digits(b), digits(c), letters(d)] | ||
def parse(str) | ||
[GiroPostcode, GeographicPostcode, InvalidPostcode].each do |klass| | ||
pc = klass.parse(str) | ||
return pc if pc | ||
end | ||
end | ||
|
||
def letters(s) | ||
s && s.tr("10", "IO") | ||
end | ||
|
||
def digits(s) | ||
s && s.tr("IO", "10") | ||
end | ||
end | ||
|
||
require "uk_postcode/version" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'uk_postcode/country_lookup' | ||
|
||
module UKPostcode | ||
module CountryFinder | ||
module_function | ||
|
||
def country(postcode) | ||
normalized = [postcode.outcode, postcode.incode].compact.join | ||
COUNTRY_LOOKUP.each do |name, regexp| | ||
return name if normalized.match(regexp) | ||
end | ||
:unknown | ||
end | ||
end | ||
end |
Oops, something went wrong.