postcode_anywhere 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Rakefile +4 -29
- data/lib/postcode_anywhere.rb +21 -12
- data/lib/postcode_anywhere/address.rb +5 -0
- data/lib/postcode_anywhere/error_handler.rb +5 -0
- data/lib/postcode_anywhere/lookup.rb +18 -0
- data/lib/postcode_anywhere/validator.rb +10 -0
- data/postcode_anywhere.gemspec +25 -0
- data/spec/fixtures/no-results.xml +2 -0
- data/spec/fixtures/postcode-error.xml +9 -0
- data/spec/fixtures/success.xml +28 -0
- data/spec/fixtures/system_errors/unknown_error.xml +9 -0
- data/spec/postcode_anywhere/address_spec.rb +0 -0
- data/spec/postcode_anywhere/lookup_spec.rb +17 -0
- data/spec/postcode_anywhere/validator_spec.rb +11 -0
- data/spec/postcode_anywhere_spec.rb +47 -0
- data/spec/spec_helper.rb +7 -0
- metadata +82 -55
data/Gemfile
ADDED
data/Rakefile
CHANGED
@@ -1,29 +1,4 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
'lib/**/*',
|
6
|
-
'rails/**/*',
|
7
|
-
'tasks/**/*',
|
8
|
-
'test/**/*'
|
9
|
-
]
|
10
|
-
|
11
|
-
spec = Gem::Specification.new do |s|
|
12
|
-
s.name = "postcode_anywhere"
|
13
|
-
s.version = "0.2"
|
14
|
-
s.author = "Matthew Crouch"
|
15
|
-
s.email = "mcrouch@mobilezil.la"
|
16
|
-
s.homepage = "http://github.com/mobzilla/postcode_anywhere"
|
17
|
-
s.platform = Gem::Platform::RUBY
|
18
|
-
s.summary = "Find addresses by postcode and building number via Postcode Anywhere API"
|
19
|
-
s.files = PKG_FILES.to_a
|
20
|
-
s.require_path = "lib"
|
21
|
-
s.has_rdoc = false
|
22
|
-
s.extra_rdoc_files = ["README.markdown"]
|
23
|
-
s.add_dependency("httparty", "~> 0.7.7")
|
24
|
-
end
|
25
|
-
|
26
|
-
desc 'Turn this plugin into a gem.'
|
27
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
28
|
-
pkg.gem_spec = spec
|
29
|
-
end
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
RSpec::Core::RakeTask.new(:spec)
|
4
|
+
task :default => :spec
|
data/lib/postcode_anywhere.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
require "httparty"
|
2
|
+
require "postcode_anywhere/validator"
|
3
|
+
require "postcode_anywhere/lookup"
|
4
|
+
require "postcode_anywhere/address"
|
5
|
+
|
2
6
|
module PostcodeAnywhere
|
3
7
|
|
4
|
-
SERVICE_ADDRESS = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.00/xmle.ws"
|
5
|
-
|
6
|
-
include HTTParty
|
7
|
-
format :xml
|
8
|
-
|
9
8
|
class << self
|
10
9
|
|
11
10
|
attr_accessor :key
|
@@ -15,11 +14,14 @@ module PostcodeAnywhere
|
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
17
|
+
def self.lookup(options = {})
|
18
|
+
validate_key
|
19
|
+
validate_postcode(options[:postcode])
|
20
|
+
return find_postcode(options)
|
21
|
+
end
|
22
|
+
|
18
23
|
def self.find_by_number_and_postcode(number, postcode)
|
19
|
-
|
20
|
-
sanitised_postcode = postcode.gsub(/\s/, "")
|
21
|
-
data = PostcodeAnywhere.lookup(number, sanitised_postcode)
|
22
|
-
data["Table"]["Row"]
|
24
|
+
lookup(:number => number, :postcode => postcode)
|
23
25
|
end
|
24
26
|
|
25
27
|
protected
|
@@ -29,11 +31,18 @@ module PostcodeAnywhere
|
|
29
31
|
raise PostcodeAnywhereException, "Please provide a valid Postcode Anywhere License Key"
|
30
32
|
end
|
31
33
|
end
|
32
|
-
|
33
|
-
def self.
|
34
|
-
PostcodeAnywhere.
|
34
|
+
|
35
|
+
def self.validate_postcode(postcode = "")
|
36
|
+
unless PostcodeAnywhere::Validator.valid_postcode?(postcode)
|
37
|
+
raise PostcodeAnywhereException::InvalidPostCode
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.find_postcode(options)
|
42
|
+
PostcodeAnywhere::Lookup.lookup(:number => options[:number], :postcode => options[:postcode])
|
35
43
|
end
|
36
44
|
|
37
45
|
class PostcodeAnywhereException < StandardError;end
|
46
|
+
class PostcodeAnywhereException::InvalidPostCode < StandardError;end
|
38
47
|
|
39
48
|
end
|
@@ -0,0 +1,5 @@
|
|
1
|
+
module PostcodeAnywhere
|
2
|
+
class Address
|
3
|
+
attr_accessor :udprn, :company, :department, :line1, :line2, :line3, :line4, :line5, :town, :county, :postcode, :mailsort, :barcode, :address_type, :name, :number, :primary_street, :secondary_street, :double_dependent_locality, :dependent_locality, :po_box
|
4
|
+
end
|
5
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "cgi"
|
2
|
+
module PostcodeAnywhere
|
3
|
+
|
4
|
+
SERVICE_ADDRESS = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.00/xmle.ws"
|
5
|
+
|
6
|
+
include HTTParty
|
7
|
+
format :xml
|
8
|
+
|
9
|
+
class Lookup
|
10
|
+
def self.lookup(options)
|
11
|
+
results = PostcodeAnywhere.get(SERVICE_ADDRESS+"?Key=#{PostcodeAnywhere.key}&Postcode=#{CGI::escape(options[:postcode])}&Building=#{options[:number]}")
|
12
|
+
address = Address.new
|
13
|
+
address.line1 = results["Table"]["Row"]["Line1"]
|
14
|
+
address.company = results["Table"]["Row"]["Company"]
|
15
|
+
address
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module PostcodeAnywhere
|
2
|
+
class Validator
|
3
|
+
|
4
|
+
POSTCODE_REGEX = /(GIR 0AA)|(((A[BL]|B[ABDHLNRSTX]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[HNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTY]?|T[ADFNQRSW]|UB|W[ADFNRSV]|YO|ZE)[1-9]?[0-9]|((E|N|NW|SE|SW|W)1|EC[1-4]|WC[12])[A-HJKMNPR-Y]|(SW|W)([2-9]|[1-9][0-9])|EC[1-9][0-9]) [0-9][ABD-HJLNP-UW-Z]{2})/
|
5
|
+
|
6
|
+
def self.valid_postcode?(postcode)
|
7
|
+
true if postcode =~ POSTCODE_REGEX
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "postcode_anywhere"
|
6
|
+
s.version = "0.3"
|
7
|
+
s.authors = ["Matthew Crouch"]
|
8
|
+
s.email = ["matthew@springydevelopment.co.uk"]
|
9
|
+
s.homepage = ""
|
10
|
+
s.summary = %q{Postcode Anywhere - Wrapper for postcode lookups}
|
11
|
+
s.description = %q{Find addresses by postcode and building number via Postcode Anywhere API}
|
12
|
+
|
13
|
+
s.rubyforge_project = "postcode_anywhere"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency("httparty", "~> 0.7.7")
|
21
|
+
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_development_dependency "fakeweb"
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" ?>
|
2
|
+
<Table>
|
3
|
+
<Row>
|
4
|
+
<Udprn>51585066</Udprn>
|
5
|
+
<Company>Harrods</Company>
|
6
|
+
<Department />
|
7
|
+
<Line1>87-135 Brompton Road</Line1>
|
8
|
+
<Line2 />
|
9
|
+
<Line3 />
|
10
|
+
<Line4 />
|
11
|
+
<Line5 />
|
12
|
+
<PostTown>London</PostTown>
|
13
|
+
<County />
|
14
|
+
<Postcode>SW1X 7XL</Postcode>
|
15
|
+
<Mailsort>72114</Mailsort>
|
16
|
+
<Barcode>(SW1X7XL1AC)</Barcode>
|
17
|
+
<Type>SmallBusiness</Type>
|
18
|
+
<DeliveryPointSuffix>1A</DeliveryPointSuffix>
|
19
|
+
<SubBuilding />
|
20
|
+
<BuildingName />
|
21
|
+
<BuildingNumber>87-135</BuildingNumber>
|
22
|
+
<PrimaryStreet>Brompton Road</PrimaryStreet>
|
23
|
+
<SecondaryStreet />
|
24
|
+
<DoubleDependentLocality />
|
25
|
+
<DependentLocality />
|
26
|
+
<PoBox />
|
27
|
+
</Row>
|
28
|
+
</Table>
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" ?>
|
2
|
+
<Table>
|
3
|
+
<Row>
|
4
|
+
<Error>-1</Error>
|
5
|
+
<Description>Unknown error</Description>
|
6
|
+
<Cause>The cause of the error is unknown but details have been passed to our support staff who will investigate.</Cause>
|
7
|
+
<Resolution>These problems are typically short lived and often resolved by trying again in a few minutes.</Resolution>
|
8
|
+
</Row>
|
9
|
+
</Table>
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostcodeAnywhere::Lookup do
|
4
|
+
context "found valid postcode" do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
success_file = File.open(File.expand_path("../../fixtures/success.xml", __FILE__)).read
|
8
|
+
FakeWeb.register_uri(:get, "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.00/xmle.ws?Key=AAAA-BBBB-CCCC-DDDD&Postcode=SW1X+7XL&Building=87", :body => success_file, :content_type => "application/xml")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return address" do
|
12
|
+
address = PostcodeAnywhere::Lookup.lookup(:number => 87, :postcode => "SW1X 7XL")
|
13
|
+
address.company.should == "Harrods"
|
14
|
+
address.line1.should == "87-135 Brompton Road"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostcodeAnywhere::Validator do
|
4
|
+
it "should return false with invalid postcode" do
|
5
|
+
PostcodeAnywhere::Validator.valid_postcode?("S").should be_false
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return true with valid postcode" do
|
9
|
+
PostcodeAnywhere::Validator.valid_postcode?("S61 1LD").should be_true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe PostcodeAnywhere do
|
4
|
+
|
5
|
+
let(:fake_successful_request) {
|
6
|
+
success_file = File.open(File.expand_path("../fixtures/success.xml", __FILE__)).read
|
7
|
+
FakeWeb.register_uri(:get, "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/RetrieveByPostcodeAndBuilding/v1.00/xmle.ws?Key=AAAA-BBBB-CCCC-DDDD&Postcode=SW1X+7XL&Building=87", :body => success_file, :content_type => "application/xml")
|
8
|
+
}
|
9
|
+
|
10
|
+
context "Invalid API credentials provided" do
|
11
|
+
it "should raise if not API credientials are provided" do
|
12
|
+
expect { PostcodeAnywhere.lookup(:number => 87, :postcode => "SW1X 7XL") }.to raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "Valid API credentials provided" do
|
17
|
+
|
18
|
+
before(:each) do
|
19
|
+
PostcodeAnywhere.key = "AAAA-BBBB-CCCC-DDDD"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise if no postcode is provided" do
|
23
|
+
expect { PostcodeAnywhere.lookup }.to raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return an address if address is found" do
|
27
|
+
fake_successful_request
|
28
|
+
address = PostcodeAnywhere.lookup(:number => 87, :postcode => "SW1X 7XL")
|
29
|
+
address.company.should == "Harrods"
|
30
|
+
address.line1.should == "87-135 Brompton Road"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "Legacy API" do
|
35
|
+
|
36
|
+
before(:each) do
|
37
|
+
PostcodeAnywhere.key = "AAAA-BBBB-CCCC-DDDD"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should allow find_by_number_and_postcode to be used" do
|
41
|
+
fake_successful_request
|
42
|
+
address = PostcodeAnywhere.find_by_number_and_postcode(87, "SW1X 7XL")
|
43
|
+
address.company.should == "Harrods"
|
44
|
+
address.line1.should == "87-135 Brompton Road"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,80 +1,107 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: postcode_anywhere
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 2
|
8
|
-
version: "0.2"
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.3'
|
5
|
+
prerelease:
|
9
6
|
platform: ruby
|
10
|
-
authors:
|
7
|
+
authors:
|
11
8
|
- Matthew Crouch
|
12
9
|
autorequire:
|
13
10
|
bindir: bin
|
14
11
|
cert_chain: []
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
dependencies:
|
19
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-11-09 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
20
15
|
name: httparty
|
21
|
-
|
22
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2168666160 !ruby/object:Gem::Requirement
|
23
17
|
none: false
|
24
|
-
requirements:
|
18
|
+
requirements:
|
25
19
|
- - ~>
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 0
|
29
|
-
- 7
|
30
|
-
- 7
|
20
|
+
- !ruby/object:Gem::Version
|
31
21
|
version: 0.7.7
|
32
22
|
type: :runtime
|
33
|
-
|
34
|
-
|
35
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2168666160
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2168664860 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2168664860
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: fakeweb
|
38
|
+
requirement: &2168664220 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2168664220
|
47
|
+
description: Find addresses by postcode and building number via Postcode Anywhere
|
48
|
+
API
|
49
|
+
email:
|
50
|
+
- matthew@springydevelopment.co.uk
|
36
51
|
executables: []
|
37
|
-
|
38
52
|
extensions: []
|
39
|
-
|
40
|
-
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- Gemfile
|
41
56
|
- README.markdown
|
42
|
-
files:
|
43
|
-
- init.rb
|
44
57
|
- Rakefile
|
45
|
-
-
|
58
|
+
- init.rb
|
46
59
|
- lib/postcode_anywhere.rb
|
47
|
-
|
48
|
-
|
60
|
+
- lib/postcode_anywhere/address.rb
|
61
|
+
- lib/postcode_anywhere/error_handler.rb
|
62
|
+
- lib/postcode_anywhere/lookup.rb
|
63
|
+
- lib/postcode_anywhere/validator.rb
|
64
|
+
- postcode_anywhere.gemspec
|
65
|
+
- spec/fixtures/no-results.xml
|
66
|
+
- spec/fixtures/postcode-error.xml
|
67
|
+
- spec/fixtures/success.xml
|
68
|
+
- spec/fixtures/system_errors/unknown_error.xml
|
69
|
+
- spec/postcode_anywhere/address_spec.rb
|
70
|
+
- spec/postcode_anywhere/lookup_spec.rb
|
71
|
+
- spec/postcode_anywhere/validator_spec.rb
|
72
|
+
- spec/postcode_anywhere_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: ''
|
49
75
|
licenses: []
|
50
|
-
|
51
76
|
post_install_message:
|
52
77
|
rdoc_options: []
|
53
|
-
|
54
|
-
require_paths:
|
78
|
+
require_paths:
|
55
79
|
- lib
|
56
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
81
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
|
63
|
-
version: "0"
|
64
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
87
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
|
70
|
-
- 0
|
71
|
-
version: "0"
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
72
92
|
requirements: []
|
73
|
-
|
74
|
-
|
75
|
-
rubygems_version: 1.3.7
|
93
|
+
rubyforge_project: postcode_anywhere
|
94
|
+
rubygems_version: 1.8.10
|
76
95
|
signing_key:
|
77
96
|
specification_version: 3
|
78
|
-
summary:
|
79
|
-
test_files:
|
80
|
-
|
97
|
+
summary: Postcode Anywhere - Wrapper for postcode lookups
|
98
|
+
test_files:
|
99
|
+
- spec/fixtures/no-results.xml
|
100
|
+
- spec/fixtures/postcode-error.xml
|
101
|
+
- spec/fixtures/success.xml
|
102
|
+
- spec/fixtures/system_errors/unknown_error.xml
|
103
|
+
- spec/postcode_anywhere/address_spec.rb
|
104
|
+
- spec/postcode_anywhere/lookup_spec.rb
|
105
|
+
- spec/postcode_anywhere/validator_spec.rb
|
106
|
+
- spec/postcode_anywhere_spec.rb
|
107
|
+
- spec/spec_helper.rb
|