postcodeanywhere 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +54 -0
- data/VERSION +1 -0
- data/lib/postcodeanywhere.rb +114 -0
- data/test/helper.rb +10 -0
- data/test/test_postcodeanywhere.rb +7 -0
- metadata +86 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 basedotextend
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
= postcodeanywhere
|
|
2
|
+
|
|
3
|
+
Description goes here.
|
|
4
|
+
|
|
5
|
+
== Note on Patches/Pull Requests
|
|
6
|
+
|
|
7
|
+
* Fork the project.
|
|
8
|
+
* Make your feature addition or bug fix.
|
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
|
10
|
+
future version unintentionally.
|
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
|
14
|
+
|
|
15
|
+
== Copyright
|
|
16
|
+
|
|
17
|
+
Copyright (c) 2010 basedotextend. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'jeweler'
|
|
6
|
+
Jeweler::Tasks.new do |gem|
|
|
7
|
+
gem.name = "postcodeanywhere"
|
|
8
|
+
gem.summary = %Q{Interface with postcodeanywhere.co.uk}
|
|
9
|
+
gem.description = %Q{Interface with postcodeanywhere.co.uk}
|
|
10
|
+
gem.email = "sandeepvshetty@gmail.com"
|
|
11
|
+
gem.homepage = "http://github.com/basedotextend/postcodeanywhere"
|
|
12
|
+
gem.authors = ["basedotextend"]
|
|
13
|
+
#gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
|
14
|
+
gem.add_development_dependency "httparty"
|
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
16
|
+
end
|
|
17
|
+
Jeweler::GemcutterTasks.new
|
|
18
|
+
rescue LoadError
|
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
require 'rake/testtask'
|
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
|
24
|
+
test.libs << 'lib' << 'test'
|
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
|
26
|
+
test.verbose = true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
begin
|
|
30
|
+
require 'rcov/rcovtask'
|
|
31
|
+
Rcov::RcovTask.new do |test|
|
|
32
|
+
test.libs << 'test'
|
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
|
34
|
+
test.verbose = true
|
|
35
|
+
end
|
|
36
|
+
rescue LoadError
|
|
37
|
+
task :rcov do
|
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
task :test => :check_dependencies
|
|
43
|
+
|
|
44
|
+
task :default => :test
|
|
45
|
+
|
|
46
|
+
require 'rake/rdoctask'
|
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
|
49
|
+
|
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
51
|
+
rdoc.title = "postcodeanywhere #{version}"
|
|
52
|
+
rdoc.rdoc_files.include('README*')
|
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
54
|
+
end
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
require 'HTTParty'
|
|
2
|
+
|
|
3
|
+
module PostcodeAnywhere
|
|
4
|
+
|
|
5
|
+
ADDRESS_LOOKUP = "http://services.postcodeanywhere.co.uk/xml.aspx"
|
|
6
|
+
ADDRESS_FETCH = "http://services.postcodeanywhere.co.uk/dataset.aspx?action=fetch"
|
|
7
|
+
|
|
8
|
+
class Request
|
|
9
|
+
def self.account_code
|
|
10
|
+
@@account_code
|
|
11
|
+
end
|
|
12
|
+
def self.account_code=(account_code)
|
|
13
|
+
@@account_code = account_code
|
|
14
|
+
end
|
|
15
|
+
def self.license_code
|
|
16
|
+
@@license_code
|
|
17
|
+
end
|
|
18
|
+
def self.license_code=(license_code)
|
|
19
|
+
@@license_code = license_code
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class PostcodeSearch < Request
|
|
24
|
+
|
|
25
|
+
include HTTParty
|
|
26
|
+
format :xml
|
|
27
|
+
|
|
28
|
+
attr_accessor :postcode, :country_name, :fetch_id
|
|
29
|
+
|
|
30
|
+
def initialize(args={})
|
|
31
|
+
self.postcode = args[:postcode]
|
|
32
|
+
self.country_name = args[:country_name]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def lookup
|
|
36
|
+
data = PostcodeSearch.get self.lookup_url
|
|
37
|
+
formatted_data = []
|
|
38
|
+
unless data["PostcodeAnywhere"]["Schema"]["Field"][0]["Name"] == "error_number"
|
|
39
|
+
formatted_data = data["PostcodeAnywhere"]["Data"]["Item"]
|
|
40
|
+
end
|
|
41
|
+
formatted_data
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def fetch
|
|
45
|
+
data = PostcodeSearch.get self.fetch_url
|
|
46
|
+
formatted_data = data["NewDataSet"]["Data"]
|
|
47
|
+
@address_lookup = AddressLookup.new
|
|
48
|
+
if @postcode_search.country.name == 'United Kingdom'
|
|
49
|
+
@address_lookup.postcode = formatted_data["postcode"]
|
|
50
|
+
@address_lookup.address_line_1 = formatted_data["line1"]
|
|
51
|
+
@address_lookup.address_line_2 = formatted_data["line2"]
|
|
52
|
+
@address_lookup.address_line_3 = formatted_data["line3"]
|
|
53
|
+
@address_lookup.post_town = formatted_data["post_town"]
|
|
54
|
+
@address_lookup.county = formatted_data["county"].blank? ? formatted_data["post_town"] : formatted_data["county"]
|
|
55
|
+
elsif @postcode_search.country.name == 'United States'
|
|
56
|
+
@address_lookup.postcode = formatted_data["zip4"].blank? ? @postcode_search.postcode : formatted_data["zip4"]
|
|
57
|
+
@address_lookup.address_line_1 = formatted_data["line1"]
|
|
58
|
+
@address_lookup.address_line_2 = formatted_data["line2"]
|
|
59
|
+
@address_lookup.address_line_3 = formatted_data["line3"]
|
|
60
|
+
@address_lookup.post_town = formatted_data["city"]
|
|
61
|
+
@address_lookup.county = formatted_data["county_name"]+", "+formatted_data["state"]
|
|
62
|
+
else
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
@address_lookup
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def lookup_url
|
|
69
|
+
ADDRESS_LOOKUP+"?"+self.lookup_type+"&"+self.postcode_with_no_spaces+"&"+self.country_code+"&"+self.license_information
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def fetch_url
|
|
73
|
+
ADDRESS_FETCH+"&"+self.address_fetch_id+"&"+self.country_code+"&"+self.license_information
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def country
|
|
77
|
+
Country.find_by_name(self.country_name)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def address_fetch_id
|
|
81
|
+
"id="+self.fetch_id
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def lookup_type
|
|
85
|
+
if self.country.name == "United Kingdom"
|
|
86
|
+
"action=lookup&type=by_postcode"
|
|
87
|
+
elsif self.country.name == "United States"
|
|
88
|
+
"action=lookup&type=by_zip"
|
|
89
|
+
else
|
|
90
|
+
"action=international&type=fetch_streets"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def country_code
|
|
95
|
+
"country="+self.country.code
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def postcode_with_no_spaces
|
|
99
|
+
(self.country.name=="United States" ? "zip=" : "postcode=")+self.postcode.gsub(/\s/, '')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def license_information
|
|
103
|
+
"account_code="+self.account_code+"&license_code="+self.license_code
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
class AddressLookup
|
|
109
|
+
|
|
110
|
+
attr_accessor :postcode, :address_line_1, :address_line_2, :address_line_3, :post_town, :county
|
|
111
|
+
attr_accessor :city, :county_name, :zip4, :state
|
|
112
|
+
|
|
113
|
+
end
|
|
114
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: postcodeanywhere
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
prerelease: false
|
|
5
|
+
segments:
|
|
6
|
+
- 0
|
|
7
|
+
- 1
|
|
8
|
+
- 0
|
|
9
|
+
version: 0.1.0
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- basedotextend
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2010-08-31 00:00:00 +01:00
|
|
18
|
+
default_executable:
|
|
19
|
+
dependencies:
|
|
20
|
+
- !ruby/object:Gem::Dependency
|
|
21
|
+
name: httparty
|
|
22
|
+
prerelease: false
|
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
|
24
|
+
none: false
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
segments:
|
|
29
|
+
- 0
|
|
30
|
+
version: "0"
|
|
31
|
+
type: :development
|
|
32
|
+
version_requirements: *id001
|
|
33
|
+
description: Interface with postcodeanywhere.co.uk
|
|
34
|
+
email: sandeepvshetty@gmail.com
|
|
35
|
+
executables: []
|
|
36
|
+
|
|
37
|
+
extensions: []
|
|
38
|
+
|
|
39
|
+
extra_rdoc_files:
|
|
40
|
+
- LICENSE
|
|
41
|
+
- README.rdoc
|
|
42
|
+
files:
|
|
43
|
+
- .document
|
|
44
|
+
- .gitignore
|
|
45
|
+
- LICENSE
|
|
46
|
+
- README.rdoc
|
|
47
|
+
- Rakefile
|
|
48
|
+
- VERSION
|
|
49
|
+
- lib/postcodeanywhere.rb
|
|
50
|
+
- test/helper.rb
|
|
51
|
+
- test/test_postcodeanywhere.rb
|
|
52
|
+
has_rdoc: true
|
|
53
|
+
homepage: http://github.com/basedotextend/postcodeanywhere
|
|
54
|
+
licenses: []
|
|
55
|
+
|
|
56
|
+
post_install_message:
|
|
57
|
+
rdoc_options:
|
|
58
|
+
- --charset=UTF-8
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
none: false
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
segments:
|
|
67
|
+
- 0
|
|
68
|
+
version: "0"
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
none: false
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
segments:
|
|
75
|
+
- 0
|
|
76
|
+
version: "0"
|
|
77
|
+
requirements: []
|
|
78
|
+
|
|
79
|
+
rubyforge_project:
|
|
80
|
+
rubygems_version: 1.3.7
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 3
|
|
83
|
+
summary: Interface with postcodeanywhere.co.uk
|
|
84
|
+
test_files:
|
|
85
|
+
- test/helper.rb
|
|
86
|
+
- test/test_postcodeanywhere.rb
|