postcode 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +11 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/lib/postcode/uk.rb +35 -0
- data/lib/postcode.rb +2 -0
- data/spec/fixtures/database.uk.txt +8338 -0
- data/spec/postcode_spec.rb +31 -0
- data/spec/spec.opts +2 -0
- metadata +71 -0
data/README
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
PostCode
|
2
|
+
|
3
|
+
*Warning:* Very alpha and subject to change.
|
4
|
+
|
5
|
+
Currently supports looking up UK postcode lat/long using the 'Free The Postcode' database: http://www.freethepostcode.org
|
6
|
+
|
7
|
+
> postcode = PostCode::UK.lookup("ME19 4YT")
|
8
|
+
> postcode.latitude
|
9
|
+
=> 51.278813
|
10
|
+
> postcode.longitude
|
11
|
+
=> -0.132300
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec/rake/spectask'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = "postcode"
|
7
|
+
gem.summary = "Library for mapping postcodes to lat/long"
|
8
|
+
gem.email = "Stephen Bartholomew"
|
9
|
+
gem.homepage = "http://github.com/curve21/post_code"
|
10
|
+
gem.description = ""
|
11
|
+
gem.authors = ["Stephen Bartholomew"]
|
12
|
+
gem.files = FileList["[A-Z]*", "{lib,spec}/**/*"]
|
13
|
+
gem.add_dependency("fastercsv")
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Update the postcode database"
|
20
|
+
namespace :db do
|
21
|
+
task :update do
|
22
|
+
`curl http://www.freethepostcode.org/currentlist > database/database.txt`
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
spec.spec_opts = ['--options', 'spec/spec.opts']
|
30
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/postcode/uk.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module PostCode
|
2
|
+
class UK
|
3
|
+
attr_accessor :postcode, :latitude, :longitude
|
4
|
+
|
5
|
+
def initialize(postcode, latitude, longitude)
|
6
|
+
self.postcode = postcode
|
7
|
+
self.latitude = latitude
|
8
|
+
self.longitude = longitude
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def database=(path)
|
13
|
+
@database = FasterCSV.read(path, :col_sep => " ")
|
14
|
+
end
|
15
|
+
|
16
|
+
def database
|
17
|
+
if !@database
|
18
|
+
self.database = File.dirname(__FILE__) + "/../../databases/database.uk.txt"
|
19
|
+
end
|
20
|
+
|
21
|
+
@database
|
22
|
+
end
|
23
|
+
|
24
|
+
def lookup(postcode)
|
25
|
+
postcode.gsub!(/\s+/, "")
|
26
|
+
|
27
|
+
data = @database.select {|row| row if "#{row[2]}#{row[3]}" == postcode }.first
|
28
|
+
|
29
|
+
if data.size > 0
|
30
|
+
self.new(postcode, data[0], data[1])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/postcode.rb
ADDED