postcode_software 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8fcd0a5548ed9196d57b763770a21255d771cf08
4
+ data.tar.gz: b4b02d5a0a8fcce3aadc336f9c8cbb6a09f2632c
5
+ SHA512:
6
+ metadata.gz: df7be95cc6bd8c7af85e8d09c457b5d3dbb27a2cd65d94ec3cb749b5770a253a914c1bab7b3812e4f3012bbda79cf1ae7080c95ba952097d053e76a285a0df72
7
+ data.tar.gz: 06813bab3a314ec7ccc9882b8bb78eced091a7ebfa69667102d221fff783d316a19e9d21067b7aecfc0924a53284056171e906ff411a38738932b42e264393ab
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Ian Fleeton, Your e Solutions Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ == README
2
+
3
+ Unofficial Ruby wrapper for using the web SDK provided by postcodesoftware.net.
4
+
5
+ === Installation
6
+
7
+ gem install postcode_software
8
+
9
+ === Usage
10
+
11
+ require 'postcode_software'
12
+ PostcodeSoftware.account = 'myaccount'
13
+ PostcodeSoftware.password = 'mypassword'
14
+ response = PostcodeSoftware.look_up('LS18 4AB')
15
+ response.address_1 # => "North Broadgate Lane"
16
+ response.address_2 # => "Horsforth"
17
+ response.town # => "Leeds"
18
+ response.postcode # => "LS18 4AB"
19
+ response.premises[0]
20
+ # => {:organisation=>nil, :building_details=>"Broadgate House", :number=>nil}
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+ RSpec::Core::RakeTask.new(:spec)
3
+
4
+ desc 'Run specs'
5
+ task default: :spec
@@ -0,0 +1,39 @@
1
+ require 'postcode_software/response'
2
+ require 'cgi'
3
+ require 'open-uri'
4
+
5
+ # Provides postcode look up service for postcodesoftware.net
6
+ #
7
+ # PostcodeSoftware.account = 'myaccount'
8
+ # PostcodeSoftware.password = 'mypassword'
9
+ # response = PostcodeSoftware.look_up('LS18 4AB')
10
+ module PostcodeSoftware
11
+
12
+ class << self
13
+ # Account for the service
14
+ attr_accessor :account
15
+ # Password for the service
16
+ attr_accessor :password
17
+ end
18
+
19
+ @account = 'test'
20
+ @password = 'test'
21
+
22
+ # Looks up the given +postcode+ and returns found addresses in a
23
+ # <tt>PostcodeSoftware::Response</tt>.
24
+ def self.look_up(postcode)
25
+ Response.new(open(sdk_url(postcode)))
26
+ end
27
+
28
+ # Returns the web SDK URL for the given postcode.
29
+ def self.sdk_url(postcode)
30
+ [
31
+ 'http://ws1.postcodesoftware.co.uk/lookup.asmx/getAddress?account=',
32
+ CGI::escape(account),
33
+ '&password=',
34
+ CGI::escape(password),
35
+ '&postcode=',
36
+ CGI.escape(postcode)
37
+ ].join
38
+ end
39
+ end
@@ -0,0 +1,91 @@
1
+ require 'nokogiri'
2
+
3
+ module PostcodeSoftware
4
+ # Groups together addresses and status information returned from the
5
+ # PostcodeSoftware.net web SDK.
6
+ class Response
7
+ # Initialise with the XML returned from the PostcodeSoftware service.
8
+ #
9
+ # The +xml+ parameter can be a filename, string or input stream as
10
+ # compatible with Nokogiri.
11
+ def initialize(xml)
12
+ @doc = Nokogiri::XML(xml)
13
+ @doc.remove_namespaces!
14
+ end
15
+
16
+ # Returns the error number.
17
+ def error_number
18
+ @doc.at_xpath('//Address//ErrorNumber').content.to_i
19
+ end
20
+
21
+ # Returns the error message.
22
+ def error_message
23
+ @doc.at_xpath('//Address//ErrorMessage').content
24
+ end
25
+
26
+ # Street name of the postcode.
27
+ def address_1
28
+ try_content '//Address//Address1'
29
+ end
30
+
31
+ # Locality of the postcode or dependent street name if exists.
32
+ def address_2
33
+ try_content '//Address//Address2'
34
+ end
35
+
36
+ # Dependent Locality of the postcode.
37
+ def address_3
38
+ try_content '//Address//Address3'
39
+ end
40
+
41
+ # Double Dependent Locality of the postcode.
42
+ def address_4
43
+ try_content '//Address//Address4'
44
+ end
45
+
46
+ # Town of the postcode.
47
+ def town
48
+ try_content '//Address//Town'
49
+ end
50
+
51
+ # County of the postcode.
52
+ def county
53
+ try_content '//Address//County'
54
+ end
55
+
56
+ # Postcode that has been searched.
57
+ def postcode
58
+ try_content '//Address//Postcode'
59
+ end
60
+
61
+ # Returns premise data for this postcode.
62
+ #
63
+ # [
64
+ # {
65
+ # :organisation=>'Organisation',
66
+ # :building_details=>'Bulding Details',
67
+ # :number=>'123'
68
+ # },
69
+ # ...
70
+ # ]
71
+ def premises
72
+ if @doc.at_xpath('//Address//PremiseData')
73
+ str = @doc.at_xpath('//Address//PremiseData').content
74
+ str
75
+ .split(';')
76
+ .map do |x|
77
+ y = x.split('|', -1)
78
+ {organisation:y[0], building_details:y[1], number:y[2]}
79
+ end
80
+ else
81
+ []
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def try_content(path)
88
+ @doc.at_xpath(path).content if @doc.at_xpath(path)
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'postcode_software/response'
3
+
4
+ module PostcodeSoftware
5
+ describe Response do
6
+ let(:xml) do
7
+ <<-xml
8
+ <?xml version="1.0" encoding="utf-8"?>
9
+ <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://postcodesoftware.co.uk/">
10
+ <ErrorNumber>5</ErrorNumber>
11
+ <ErrorMessage>Test accounts can only be used with LS18 postcodes</ErrorMessage>
12
+ </Address>
13
+ xml
14
+ end
15
+
16
+ subject(:response) { Response.new(xml) }
17
+
18
+ describe '#error_number' do
19
+ subject { response.error_number }
20
+
21
+ it { is_expected.to eq 5 }
22
+ end
23
+
24
+ describe '#error_message' do
25
+ subject { response.error_message }
26
+
27
+ it { is_expected.to eq 'Test accounts can only be used with LS18 postcodes' }
28
+ end
29
+
30
+ context 'without premise level data' do
31
+ let(:xml) { xml_fixture('LS184AF-no-premise.xml') }
32
+
33
+ it 'has no premises' do
34
+ expect(subject.premises).to eq []
35
+ end
36
+ end
37
+
38
+ context 'with LS18 4AA' do
39
+ let(:xml) { xml_fixture('LS184AA.xml') }
40
+
41
+ it 'has address_1 set' do
42
+ expect(response.address_1).to eq 'South Row'
43
+ end
44
+
45
+ it 'has address_2 set' do
46
+ expect(response.address_2).to eq 'Horsforth'
47
+ end
48
+
49
+ it 'has address_3 unset' do
50
+ expect(response.address_3).to be_nil
51
+ end
52
+
53
+ it 'has address_4 unset' do
54
+ expect(response.address_4).to be_nil
55
+ end
56
+
57
+ it 'has town set' do
58
+ expect(response.town).to eq 'Leeds'
59
+ end
60
+
61
+ it 'has county set' do
62
+ expect(response.county).to eq 'West Yorkshire'
63
+ end
64
+
65
+ it 'has postcode set' do
66
+ expect(response.postcode).to eq 'LS18 4AA'
67
+ end
68
+
69
+ describe 'premises' do
70
+ subject { response.premises }
71
+
72
+ it { expect(subject.length).to eq 4 }
73
+
74
+ it 'sets organisation' do
75
+ expect(subject[1][:organisation]).to eq 'Organisation'
76
+ end
77
+
78
+ it 'sets building details' do
79
+ expect(subject[2][:building_details]).to eq 'Building Details'
80
+ end
81
+
82
+ it 'sets number' do
83
+ expect(subject[0][:number]).to eq '1'
84
+ end
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def xml_fixture(filename)
91
+ File.open(
92
+ File.join [File.dirname(__FILE__), '..', '..', 'support', 'fixtures', filename]
93
+ )
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'postcode_software'
3
+
4
+ RSpec.describe PostcodeSoftware do
5
+ describe '.look_up' do
6
+ subject { PostcodeSoftware.look_up(postcode) }
7
+
8
+ context 'with account and password unset' do
9
+ context 'using example LS18 postcodes' do
10
+ let(:postcode) { 'LS18 4AA' }
11
+ it { is_expected.to be_kind_of(PostcodeSoftware::Response) }
12
+
13
+ it 'has error_number of 0' do
14
+ expect(subject.error_number).to eq 0
15
+ end
16
+
17
+ it 'has empty error message' do
18
+ expect(subject.error_message).to eq ''
19
+ end
20
+ end
21
+
22
+ context 'using example DN1 postcodes' do
23
+ let(:postcode) { 'DN1 2QP' }
24
+ it { is_expected.to be_kind_of(PostcodeSoftware::Response) }
25
+
26
+ it 'has error_number of 5' do
27
+ expect(subject.error_number).to eq 5
28
+ end
29
+
30
+ it 'has error_message set' do
31
+ expect(subject.error_message).to eq 'Test accounts can only be used with LS18 postcodes'
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'with valid account and password' do
37
+ before do
38
+ PostcodeSoftware.account = 'valid'
39
+ PostcodeSoftware.password = 'valid'
40
+ end
41
+
42
+ context 'using example LS18 postcodes' do
43
+ let(:postcode) { 'LS18 4AA' }
44
+
45
+ it { is_expected.to be_kind_of(PostcodeSoftware::Response) }
46
+ end
47
+ end
48
+
49
+ context 'with invalid account or password' do
50
+ before do
51
+ PostcodeSoftware.account = 'invalid'
52
+ end
53
+
54
+ let(:postcode) { 'LS18 4AA' }
55
+
56
+ it 'has error_number of 1' do
57
+ expect(subject.error_number).to eq 1
58
+ end
59
+
60
+ it 'has error_message set' do
61
+ expect(subject.error_message).to eq 'Account not active'
62
+ end
63
+ end
64
+ end
65
+
66
+ describe '.sdk_url' do
67
+ it 'returns the URL containing auth and postcode params' do
68
+ PostcodeSoftware.account = 'u'
69
+ PostcodeSoftware.password = 'p'
70
+ postcode = 'LS18 4AA'
71
+ expected = 'http://ws1.postcodesoftware.co.uk/lookup.asmx/getAddress?account=u&password=p&postcode=LS18+4AA'
72
+ expect(PostcodeSoftware.sdk_url(postcode)).to eq expected
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,8 @@
1
+ require 'webmock/rspec'
2
+ require 'support/fake_postcode_software'
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:each) do
6
+ stub_request(:any, /ws1.postcodesoftware.co.uk/).to_rack(FakePostcodeSoftware)
7
+ end
8
+ end
@@ -0,0 +1,37 @@
1
+ require 'sinatra/base'
2
+
3
+ class FakePostcodeSoftware < Sinatra::Base
4
+ get '/lookup.asmx/getAddress' do
5
+ case params[:account]
6
+ when 'test' then handle_test
7
+ when 'valid' then handle_valid
8
+ when 'invalid' then handle_invalid
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def handle_test
15
+ case params[:postcode]
16
+ when 'LS18 4AA' then xml_response('LS184AA.xml')
17
+ else
18
+ xml_response 'only-LS18.xml'
19
+ end
20
+ end
21
+
22
+ def handle_valid
23
+ case params[:postcode]
24
+ when 'LS18 4AA' then xml_response('LS184AA.xml')
25
+ end
26
+ end
27
+
28
+ def handle_invalid
29
+ xml_response('account-not-active.xml')
30
+ end
31
+
32
+ def xml_response(file_name)
33
+ content_type :xml
34
+ status 200
35
+ File.open(File.dirname(__FILE__) + '/fixtures/' + file_name, 'rb').read
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://postcodesoftware.co.uk/">
3
+ <Address1>South Row</Address1>
4
+ <Address2>Horsforth</Address2>
5
+ <Town>Leeds</Town>
6
+ <County>West Yorkshire</County>
7
+ <Postcode>LS18 4AA</Postcode>
8
+ <PremiseData>||1;Organisation||;|Building Details|;||15;</PremiseData>
9
+ <ErrorNumber>0</ErrorNumber>
10
+ <ErrorMessage />
11
+ </Address>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://postcodesoftware.co.uk/">
3
+ <Address1>Sylvan View</Address1>
4
+ <Address2>Horsforth</Address2>
5
+ <Town>Leeds</Town>
6
+ <County>West Yorkshire</County>
7
+ <Postcode>LS18 4AF</Postcode>
8
+ <ErrorNumber>0</ErrorNumber>
9
+ <ErrorMessage />
10
+ </Address>
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://postcodesoftware.co.uk/">
3
+ <ErrorNumber>1</ErrorNumber>
4
+ <ErrorMessage>Account not active</ErrorMessage>
5
+ </Address>
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://postcodesoftware.co.uk/">
3
+ <ErrorNumber>5</ErrorNumber>
4
+ <ErrorMessage>Test accounts can only be used with LS18 postcodes</ErrorMessage>
5
+ </Address>
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postcode_software
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ian Fleeton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sinatra
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.20'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.20'
69
+ description: Ruby wrapper for PostcodeSoftware's web SDK, see http://www.postcodesoftware.net/sdk_web.htm
70
+ email: ianfleeton@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE
76
+ - README.rdoc
77
+ - Rakefile
78
+ - lib/postcode_software.rb
79
+ - lib/postcode_software/response.rb
80
+ - spec/lib/postcode_software/response_spec.rb
81
+ - spec/lib/postcode_software_spec.rb
82
+ - spec/spec_helper.rb
83
+ - spec/support/fake_postcode_software.rb
84
+ - spec/support/fixtures/LS184AA.xml
85
+ - spec/support/fixtures/LS184AF-no-premise.xml
86
+ - spec/support/fixtures/account-not-active.xml
87
+ - spec/support/fixtures/only-LS18.xml
88
+ homepage: http://rubygems.org/gems/postcode_software
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Postcode Software
112
+ test_files: []