postkode 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/postkode.rb +73 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 742e3f8d4c57bb303470cb594686b30740c6383f
|
4
|
+
data.tar.gz: 50571a36fcca7c7d17af15d9a1e31dc826b4fcbd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd024b1159a87ee8fde54d37bda5ab38334ac45728c2eaa8e7117372c4a7cfbf7a949d6c344f6624105d8861cab9d12de7904a607e73e16840316a132867ae12
|
7
|
+
data.tar.gz: 05b33cc24a2f3738aa9d7b32f328f0452093a1a44680ac26930e2c5ed433574de88c521e97641ef411fd4ed26d7f11771884d71b745f8bf7671e4a2b863c1d79
|
data/lib/postkode.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Handles validation of UK-style postcodes
|
4
|
+
class Postkode
|
5
|
+
|
6
|
+
# Validating UK Postcodes
|
7
|
+
# Correct as at 2014-09-15
|
8
|
+
# References below
|
9
|
+
# https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/283357/ILRSpecification2013_14Appendix_C_Dec2012_v1.pdf
|
10
|
+
# http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
|
11
|
+
# Areas with only single-digit districts: BR, FY, HA, HD, HG, HR, HS, HX, JE, LD, SM, SR, WC, WN, ZE (although WC is always subdivided by a further letter, e.g. WC1A).
|
12
|
+
# Areas with only double-digit districts: AB, LL, SO.
|
13
|
+
# Areas with a district '0' (zero): BL, BS, CM, CR, FY, HA, PR, SL, SS (BS is the only area to have both a district 0 and a district 10).
|
14
|
+
# The following central London single-digit districts have been further divided by inserting a letter after the digit and before the space: EC1–EC4 (but not EC50), SW1, W1, WC1, WC2, and part of E1 (E1W), N1 (N1C and N1P), NW1 (NW1W) and SE1 (SE1P).
|
15
|
+
# The letters QVX are not used in the first position.
|
16
|
+
# The letters IJZ are not used in the second position.
|
17
|
+
# The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A.
|
18
|
+
# The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with AA9A.
|
19
|
+
# The final two letters do not use the letters CIKMOV, so as not to resemble digits or each other when hand-written.
|
20
|
+
# Post code sectors are one of ten digits: 0 to 9 with 0 only used once 9 has been used in a post town, save for Croydon and Newport (see above).
|
21
|
+
|
22
|
+
A1 = "[A-PR-UWYZ]"
|
23
|
+
A2 = "[A-HK-Y]"
|
24
|
+
A3 = "[A-HJKPS-UW]"
|
25
|
+
A4 = "[ABEHMNPRV-XY]"
|
26
|
+
A5 = "[ABD-HJLN-UW-Z]"
|
27
|
+
N = "[0-9]"
|
28
|
+
AANN = A1 + A2 + N + N # the six possible first-part combos
|
29
|
+
AANA = A1 + A2 + N + A4
|
30
|
+
ANA = A1 + N + A3
|
31
|
+
ANN = A1 + N + N
|
32
|
+
AAN = A1 + A2 + N
|
33
|
+
AN = A1 + N
|
34
|
+
PART_ONE = [AANN, AANA, ANA, ANN, AAN, AN].join('|')
|
35
|
+
PART_TWO = N + A5 + A5
|
36
|
+
|
37
|
+
NORMAL_POSTCODE_VALID = "^(#{PART_ONE})[ ]*(#{PART_TWO})$"
|
38
|
+
NORMAL_POSTCODE_PATTERN = /(#{PART_ONE})[ ]*(#{PART_TWO})/
|
39
|
+
NORMAL_PART_POSTCODE_PATTERN = /[ ]*(#{PART_ONE})[ ]*/
|
40
|
+
NORMAL_POSTCODE_RE = Regexp.new(NORMAL_POSTCODE_VALID, Regexp::IGNORECASE)
|
41
|
+
|
42
|
+
def self.validate(string, return_parts=false)
|
43
|
+
return false if string.nil? || string == "" || string == false
|
44
|
+
result = string.match(NORMAL_POSTCODE_RE)
|
45
|
+
return false if result.nil?
|
46
|
+
|
47
|
+
return_parts ? result[1..2] : true
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.get_first_section(string)
|
51
|
+
self.get_outcode(string)
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.get_outcode(string)
|
55
|
+
found = PostcodeHelper.validate(string, true)
|
56
|
+
found ? found[0] : nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.get_inward(string)
|
60
|
+
found = PostcodeHelper.validate(string, true)
|
61
|
+
found ? found[1] : nil
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.find_in_string(string)
|
65
|
+
res = string.scan(NORMAL_POSTCODE_PATTERN)
|
66
|
+
res.length>0 ? res : nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.find_partial_in_string(string)
|
70
|
+
res = string.scan(NORMAL_PART_POSTCODE_PATTERN)
|
71
|
+
res.length>0 ? res : nil
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postkode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- K M Lawrence
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Postcode validation module
|
14
|
+
email: keith.lawrence@upbeatproductions.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/postkode.rb
|
20
|
+
homepage: http://rubygems.org/gems/postkode
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: postkode
|
44
|
+
test_files: []
|