uk_postcode 2.0.0.alpha → 2.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2918ca3cc4b63e92b62146b63e57897b31166f0
4
- data.tar.gz: c6689e38f4f8e4583d74f9bf06c5c617a16f66b9
3
+ metadata.gz: 8fa8f457586bb74272addb1cc2680a90718688c4
4
+ data.tar.gz: 232794c36025c24b7d9c7051c7bb37d20daa3ba2
5
5
  SHA512:
6
- metadata.gz: ea95753c01bc600400b6223b5da95957170db8d1e39f10b80f045e5379f248235a33e38b558c64f91b9c3548d616414dfac2f35172cc18b1d29da372418df458
7
- data.tar.gz: d34d691408d759e967cf626aecec25f92bbff1a10507bff46a082ca96a5ab37e44a2ad185116998a6f4d040c68bcdea1863cbf7487541dbf6928f40ec2b3a8f4
6
+ metadata.gz: 5512f266cbb5ecd1de324af09bb07b0eb0600214a75fbc04c4467eb3887a5fc9363479dde70722161e4f6c9667940bf2baf357ef6001f46e2f97e783e83460ab
7
+ data.tar.gz: 5631dca91102f422254fad3672836cb92ef54834284b32346eecaf90d90f38fb88ab807b9e84e7d2125d0a58aaf037a8b21b2639a766ae3f643b84207eee179e
data/COPYING.txt CHANGED
@@ -22,4 +22,7 @@ The regular expressions in lookup.rb are generated from the ONS Postcode
22
22
  Directory under the terms of the Open Government Licence (OGL). The following
23
23
  source accreditation is given to fulfil the terms of this licence.
24
24
 
25
+ Contains public sector information licensed under the Open Government Licence
26
+ v3.0.
27
+
25
28
  http://www.ons.gov.uk/ons/guide-method/geography/products/postcode-directories/-nspp-/index.html
@@ -0,0 +1,36 @@
1
+ module UKPostcode
2
+ class AbstractPostcode
3
+ NotImplemented = Class.new(StandardError)
4
+
5
+ def self.parse(_str)
6
+ raise NotImplemented
7
+ end
8
+
9
+ def initialize(*)
10
+ raise NotImplemented
11
+ end
12
+
13
+ def area; nil; end
14
+ def district; nil; end
15
+ def sector; nil; end
16
+ def unit; nil; end
17
+ def incode; nil; end
18
+ def outcode; nil; end
19
+
20
+ def to_s
21
+ raise NotImplemented
22
+ end
23
+
24
+ def full?
25
+ false
26
+ end
27
+
28
+ def valid?
29
+ false
30
+ end
31
+
32
+ def country
33
+ :unknown
34
+ end
35
+ end
36
+ end
@@ -1,4 +1,5 @@
1
1
  require "uk_postcode/country_finder"
2
+ require "uk_postcode/abstract_postcode"
2
3
 
3
4
  module UKPostcode
4
5
 
@@ -8,14 +9,14 @@ module UKPostcode
8
9
  # Despite the name, it also handles non-geographic postcodes that follow the
9
10
  # geographic format.
10
11
  #
11
- class GeographicPostcode
12
- PATTERN = %r{
13
- \A ( [A-PR-UWYZ01][A-HJ-Z0]? ) # area
12
+ class GeographicPostcode < AbstractPostcode
13
+ PATTERN = /
14
+ \A ( [A-PR-UWYZ01][A-HJ-Z0]? ) # area
14
15
  (?: ( [0-9IO][0-9A-HJKMNPR-YIO]? ) # district
15
16
  (?: \s* ( [0-9IO] ) # sector
16
17
  ( [ABD-HJLNPQ-Z]{2} )? )? )? # unit
17
18
  \Z
18
- }ix
19
+ /ix
19
20
 
20
21
  # Attempts to parse the postcode given in str, and returns an instance of
21
22
  # GeographicPostcode on success, or nil on failure.
@@ -74,7 +75,7 @@ module UKPostcode
74
75
  end
75
76
 
76
77
  # Find the country associated with the postcode. Possible values are
77
- # :england, :scotland, :wales, :northern_ireland, :isle_of_man,
78
+ # :england, :scotland, :wales, :northern_ireland, :isle_of_man,
78
79
  # :channel_islands, or :unknown.
79
80
  #
80
81
  # Note that, due to limitations in the underlying data, the country might
@@ -85,6 +86,7 @@ module UKPostcode
85
86
  end
86
87
 
87
88
  private
89
+
88
90
  def letters(str)
89
91
  return nil unless str
90
92
  str.upcase.tr("10", "IO")
@@ -1,4 +1,5 @@
1
1
  require "singleton"
2
+ require "uk_postcode/abstract_postcode"
2
3
 
3
4
  module UKPostcode
4
5
 
@@ -8,7 +9,7 @@ module UKPostcode
8
9
  # area, district, sector, and unit all return nil, because this postcode
9
10
  # does not meaningfully possess these distinctions.
10
11
  #
11
- class GiroPostcode
12
+ class GiroPostcode < AbstractPostcode
12
13
  include Singleton
13
14
 
14
15
  PATTERN = /\A G[I1]R \s* [0O]AA \z/ix
@@ -20,7 +21,8 @@ module UKPostcode
20
21
  PATTERN.match(str.strip) ? instance : nil
21
22
  end
22
23
 
23
- attr_reader :area, :district, :sector, :unit
24
+ def initialize
25
+ end
24
26
 
25
27
  # The left-hand part of the postcode, always "GIR".
26
28
  #
@@ -1,3 +1,5 @@
1
+ require "uk_postcode/abstract_postcode"
2
+
1
3
  module UKPostcode
2
4
 
3
5
  # InvalidPostcode is a singleton null object returned by UKPostcode.parse
@@ -5,7 +7,7 @@ module UKPostcode
5
7
  #
6
8
  # The sub-fields of the postcode (outcode, area, etc.) are all nil.
7
9
  #
8
- class InvalidPostcode
10
+ class InvalidPostcode < AbstractPostcode
9
11
  def self.parse(str)
10
12
  new(str)
11
13
  end
@@ -14,25 +16,11 @@ module UKPostcode
14
16
  @input = input
15
17
  end
16
18
 
17
- attr_reader :area, :district, :sector, :unit, :incode, :outcode
18
-
19
19
  # Returns the literal string supplied at initialisation. This may be
20
20
  # helpful when returning erroneous input to the user.
21
21
  #
22
22
  def to_s
23
23
  @input
24
24
  end
25
-
26
- def full?
27
- false
28
- end
29
-
30
- def valid?
31
- false
32
- end
33
-
34
- def country
35
- :unknown
36
- end
37
25
  end
38
26
  end
@@ -0,0 +1,15 @@
1
+ module UKPostcode
2
+ class ParserChain
3
+ def initialize(*parsers)
4
+ @parsers = parsers
5
+ end
6
+
7
+ def parse(str)
8
+ @parsers.each do |klass|
9
+ parsed = klass.parse(str)
10
+ return parsed if parsed
11
+ end
12
+ nil
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module UKPostcode
2
- VERSION = "2.0.0.alpha"
2
+ VERSION = "2.0.0.beta"
3
3
  end
data/lib/uk_postcode.rb CHANGED
@@ -2,18 +2,20 @@ require "uk_postcode/version"
2
2
  require "uk_postcode/geographic_postcode"
3
3
  require "uk_postcode/giro_postcode"
4
4
  require "uk_postcode/invalid_postcode"
5
+ require "uk_postcode/parser_chain"
5
6
 
6
7
  module UKPostcode
7
- module_function
8
+ DEFAULT_PARSER_CHAIN = ParserChain.new(
9
+ GiroPostcode, GeographicPostcode, InvalidPostcode
10
+ )
8
11
 
9
12
  # Attempt to parse the string str as a postcode. Returns an object
10
13
  # representing the postcode, or an InvalidPostcode if the string cannot be
11
14
  # parsed.
12
15
  #
13
16
  def parse(str)
14
- [GiroPostcode, GeographicPostcode, InvalidPostcode].each do |klass|
15
- pc = klass.parse(str)
16
- return pc if pc
17
- end
17
+ DEFAULT_PARSER_CHAIN.parse(str)
18
18
  end
19
+
20
+ module_function :parse
19
21
  end
@@ -15,21 +15,20 @@ describe "Full set of postcodes" do
15
15
  }
16
16
 
17
17
  it "should correctly parse and find the country of every extant postcode" do
18
- if File.exist?(CSV_PATH)
19
- CSV.foreach(CSV_PATH, headers: [:postcode, :country]) do |row|
20
- outcode = row[:postcode][0,4].strip
21
- incode = row[:postcode][4,3].strip
22
- country = COUNTRIES.fetch(row[:country])
18
+ skip "Skipping because SKIP_FULL_TEST was set" if ENV['SKIP_FULL_TEST']
19
+ skip "Skipping because #{CSV_PATH} does not exist" unless File.exist?(CSV_PATH)
23
20
 
24
- postcode = UKPostcode.parse(outcode + incode)
21
+ CSV.foreach(CSV_PATH, headers: [:postcode, :country]) do |row|
22
+ outcode = row[:postcode][0, 4].strip
23
+ incode = row[:postcode][4, 3].strip
24
+ country = COUNTRIES.fetch(row[:country])
25
25
 
26
- postcode.wont_be_nil
27
- postcode.outcode.must_equal outcode
28
- postcode.incode.must_equal incode
29
- postcode.country.must_equal country
30
- end
31
- else
32
- skip "Skipping because #{CSV_PATH} does not exist"
26
+ postcode = UKPostcode.parse(outcode + incode)
27
+
28
+ postcode.wont_be_nil
29
+ postcode.outcode.must_equal outcode
30
+ postcode.incode.must_equal incode
31
+ postcode.country.must_equal country
33
32
  end
34
33
  end
35
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uk_postcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha
4
+ version: 2.0.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Battley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-14 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -33,11 +33,13 @@ files:
33
33
  - COPYING.txt
34
34
  - README.md
35
35
  - lib/uk_postcode.rb
36
+ - lib/uk_postcode/abstract_postcode.rb
36
37
  - lib/uk_postcode/country_finder.rb
37
38
  - lib/uk_postcode/country_lookup.rb
38
39
  - lib/uk_postcode/geographic_postcode.rb
39
40
  - lib/uk_postcode/giro_postcode.rb
40
41
  - lib/uk_postcode/invalid_postcode.rb
42
+ - lib/uk_postcode/parser_chain.rb
41
43
  - lib/uk_postcode/tree.rb
42
44
  - lib/uk_postcode/version.rb
43
45
  - test/country_finder_test.rb