postcodes-norway 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e3861331f84d0c758a22530aecf954efe4cbbf1
4
+ data.tar.gz: 3e2491f1ddef57901818350aac11281dfd1ff033
5
+ SHA512:
6
+ metadata.gz: 23606f575671ddd6d4df7763aef77bf514bcdc011c21e55c1c328e88aa44a8c7536e0ea11e36ac8376124ecd28de806bd076ad17af9424ae307cfd8913677e90
7
+ data.tar.gz: 931f601d6acf5841bcc4d0cdec1b68d348706e49ef6d6e448ec48e1bf9520fcb6906387090165070a67125152ab864321628478a8665d6c677b6b4d034198a8c
@@ -0,0 +1,8 @@
1
+ load 'lib/tasks/norwegian-postcodes.rake'
2
+
3
+ task :default => %w[test]
4
+
5
+ desc "Run unit tests"
6
+ task "test" do
7
+ sh 'ruby -Itest/ test/*.rb'
8
+ end
@@ -0,0 +1,117 @@
1
+ module PostCodes
2
+
3
+ require 'norwegian-postcodes/railtie' if defined?(Rails)
4
+
5
+ # Correspond to the Norwegian 'Fylke'
6
+ Counties = [
7
+ "ØSTFOLD",
8
+ "AKERSHUS",
9
+ "OSLO",
10
+ "HEDMARK",
11
+ "OPPLAND",
12
+ "BUSKERUD",
13
+ "VESTFOLD",
14
+ "TELEMARK",
15
+ "AUST-AGDER",
16
+ "VEST-AGDER",
17
+ "ROGALAND",
18
+ "HORDALAND",
19
+ "(BERGEN)",
20
+ "SOGN OG FJORDANE",
21
+ "MØRE OG ROMSDAL",
22
+ "SØR-TRØNDELAG",
23
+ "NORD-TRØNDELAG",
24
+ "NORDLAND",
25
+ "TROMS",
26
+ "FINNMARK",
27
+ "SVALBARD",
28
+ "JAN MAYEN",
29
+ "KONTINENTALSOKKELEN"
30
+ ]
31
+
32
+ # Class for holding postcode data
33
+ class PostCode
34
+ # Four digit post code
35
+ attr_reader :postcode
36
+
37
+ # Name of city
38
+ attr_reader :city
39
+
40
+ # Four digit municipality ('kommune') id
41
+ attr_reader :municipality
42
+
43
+ # Name of municipality ('kommune')
44
+ attr_reader :municipality_name
45
+
46
+ # Category
47
+ # * 'G' = street address
48
+ # * 'P' = Postboxes
49
+ # * 'B' = Both street addresses and postboxes
50
+ # * 'S' = Service addresses
51
+ # * 'K' = Customer with its own post code
52
+ # * 'F' = Multiple uses
53
+ attr_reader :cat
54
+
55
+ # Create a new post code.
56
+ # This method should generally not be used by users of the library.
57
+ def initialize(postcode, city, muni, muni_name, cat)
58
+ @postcode, @city, @municipality, @municipality_name, @cat = postcode, city, muni, muni_name, cat
59
+ end
60
+
61
+ # Return the County ('kommune') from the postcode data.
62
+ # The returned format is `[county_id, county_name]`
63
+ def county
64
+ code = @municipality[0..1].to_i
65
+ [code, PostCodes.county(code)]
66
+ end
67
+
68
+ # Output postcode data in the same format as in the original postcode database.
69
+ def to_s
70
+ [@postcode, @city, @municipality, @municipality_name, @cat].join("\t")
71
+ end
72
+ end
73
+
74
+ class << self
75
+
76
+ # Load the postcode data into memory.
77
+ #
78
+ # +file+ is a file name or IO object to read the data from.
79
+ def load(file)
80
+ @postcodes = []
81
+ if file.is_a?(String)
82
+ f = File.open(file, :encoding => Encoding::ISO_8859_15)
83
+ else
84
+ f = file
85
+ end
86
+
87
+ f.each_line do |l|
88
+ a = l.chomp().split("\t").map{|s| s.encode(Encoding::UTF_8)}
89
+ @postcodes << PostCode.new(*a)
90
+ end
91
+ end
92
+
93
+ # Search for a given postcode.
94
+ #
95
+ # Takes a 4 digit postcode as a string, and returns an object of
96
+ # type PostCodes::PostCode, or +nil+ if the postcode was not found.
97
+ #
98
+ def search(postcode)
99
+ res = @postcodes.bsearch {|x| x.postcode.to_i >= postcode.to_i}
100
+ unless res.nil?
101
+ res.postcode.to_i == postcode.to_i ? res : nil
102
+ end
103
+ end
104
+
105
+ # Return the county by number.
106
+ #
107
+ # Takes a number between 1 and 23 as input. This corresponds to the
108
+ # first two digits in the municipality number.
109
+ #
110
+ # The county name as a string is returned.
111
+ #
112
+ def county(index)
113
+ return nil unless index > 0 && index <= Counties.size
114
+ Counties[index - 1]
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,11 @@
1
+ require 'norwegian-postcodes'
2
+ require 'rails'
3
+
4
+ module PostCodes
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :norwegian_postcodes
7
+ rake_tasks do
8
+ load 'tasks/norwegian-postcodes.rake'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+
4
+ namespace :postcodes do
5
+ desc "Fetch postcode database from Bring"
6
+ task :fetch, [:data_path] do |t, args|
7
+ args.with_defaults(:data_path => 'data')
8
+ uri = URI.parse('http://www.bring.no/hele-bring/forside/_attachment/159761')
9
+ filename = File.join(args.data_path, 'Postnummerregister_ansi.txt')
10
+ puts "Downloading postcodes to: #{filename}..."
11
+ IO.write(filename, uri.read)
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postcodes-norway
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Harald Eilertsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Parses the Norwegian postcode database as published by Bring, and lets
14
+ you search and use the data from Ruby.
15
+ email: haraldei@anduin.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - lib/postcodes-norway.rb
22
+ - lib/postcodes-norway/railtie.rb
23
+ - lib/tasks/postcodes-norway.rake
24
+ homepage: https://github.com/snake66/postcodes-norway
25
+ licenses:
26
+ - GPLv3
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A simple interface to the Norwegian postcode database.
48
+ test_files: []