aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2015-01-15 23:41:11 +0100
committerHarald Eilertsen <haraldei@anduin.net>2015-01-15 23:41:11 +0100
commit9ecb661af9dedfd14ddacc6fd66839f8edf159f5 (patch)
tree082e4aa9247756304a2be01cdb7f705032bab4e1 /lib
downloadpostcodes-norway-9ecb661af9dedfd14ddacc6fd66839f8edf159f5.tar.gz
postcodes-norway-9ecb661af9dedfd14ddacc6fd66839f8edf159f5.tar.bz2
postcodes-norway-9ecb661af9dedfd14ddacc6fd66839f8edf159f5.zip
First commit.
Diffstat (limited to 'lib')
-rw-r--r--lib/norwegian-postcodes.rb68
-rw-r--r--lib/tasks/norwegian-postcodes.rake0
2 files changed, 68 insertions, 0 deletions
diff --git a/lib/norwegian-postcodes.rb b/lib/norwegian-postcodes.rb
new file mode 100644
index 0000000..22d67d5
--- /dev/null
+++ b/lib/norwegian-postcodes.rb
@@ -0,0 +1,68 @@
+module PostCodes
+
+ Counties = [
+ "ØSTFOLD",
+ "AKERSHUS",
+ "OSLO",
+ "HEDMARK",
+ "OPPLAND",
+ "BUSKERUD",
+ "VESTFOLD",
+ "TELEMARK",
+ "AUST-AGDER",
+ "VEST-AGDER",
+ "ROGALAND",
+ "HORDALAND",
+ "(BERGEN)",
+ "SOGN OG FJORDANE",
+ "MØRE OG ROMSDAL",
+ "SØR-TRØNDELAG",
+ "NORD-TRØNDELAG",
+ "NORDLAND",
+ "TROMS",
+ "FINNMARK",
+ "SVALBARD",
+ "JAN MAYEN",
+ "KONTINENTALSOKKELEN"
+ ]
+
+ class PostCode
+ attr_reader :postcode, :city, :municipality, :municipality_name, :cat
+
+ def initialize(postcode, city, muni, muni_name, cat)
+ @postcode, @city, @municipality, @municipality_name, @cat = postcode, city, muni, muni_name, cat
+ end
+
+ def county
+ code = @municipality[0..1].to_i
+ [code, PostCodes.county(code)]
+ end
+
+ def >=(postcode)
+ @postcode.to_i >= postcode.to_i
+ end
+
+ def to_s
+ [@postcode, @city, @municipality, @municipality_name, @cat].join("\t")
+ end
+ end
+
+ class << self
+ def load(f)
+ @postcodes = []
+ IO.foreach(f, :encoding => Encoding::ISO_8859_15) do |l|
+ a = l.chomp().split("\t").map{|s| s.encode(Encoding::UTF_8)}
+ @postcodes << PostCode.new(*a)
+ end
+ end
+
+ def search(pc)
+ @postcodes.bsearch {|x| x >= pc}
+ end
+
+ def county(c)
+ return nil unless c > 0 && c <= Counties.size
+ Counties[c - 1]
+ end
+ end
+end
diff --git a/lib/tasks/norwegian-postcodes.rake b/lib/tasks/norwegian-postcodes.rake
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/tasks/norwegian-postcodes.rake