diff options
-rw-r--r-- | Gemfile | 4 | ||||
-rw-r--r-- | Gemfile.lock | 10 | ||||
-rw-r--r-- | lib/norwegian-postcodes.rb | 68 | ||||
-rw-r--r-- | lib/tasks/norwegian-postcodes.rake | 0 | ||||
-rw-r--r-- | norwegian-postcodes.gemspec | 12 | ||||
-rw-r--r-- | test/test_postcodes.rb | 55 |
6 files changed, 149 insertions, 0 deletions
@@ -0,0 +1,4 @@ +# A sample Gemfile +source "https://rubygems.org" + +gem "minitest" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..099acd2 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,10 @@ +GEM + remote: https://rubygems.org/ + specs: + minitest (5.5.1) + +PLATFORMS + ruby + +DEPENDENCIES + minitest 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 diff --git a/norwegian-postcodes.gemspec b/norwegian-postcodes.gemspec new file mode 100644 index 0000000..ff4036b --- /dev/null +++ b/norwegian-postcodes.gemspec @@ -0,0 +1,12 @@ +Gem::Specification.new do |s| + s.name = 'norwegian-postcodes' + s.version = '0.0.1' + s.date = '2010-04-28' + s.summary = "Find city and fylke from norwegian postcodes" + s.description = "Find city and fylke from norwegian postcodes." + s.authors = ["Harald Eilertsen"] + s.email = 'haraldei@anduin.net' + s.files = ["lib/norwegian-postcodes.rb"] + s.homepage = '' + s.license = 'GPLv3' +end
\ No newline at end of file diff --git a/test/test_postcodes.rb b/test/test_postcodes.rb new file mode 100644 index 0000000..1c4f368 --- /dev/null +++ b/test/test_postcodes.rb @@ -0,0 +1,55 @@ +require 'minitest/autorun' +require_relative '../lib/norwegian-postcodes' + +describe PostCodes::PostCode do + before do + @postcode = PostCodes::PostCode.new('1234', 'MYTOWN', '1378', 'MY MUNICIPALITY', 'G') + end + + it "compares to a postcode" do + (@postcode >= '1233').must_equal true + (@postcode >= '1234').must_equal true + (@postcode >= '1235').must_equal false + end + + it "has a county" do + @postcode.county.must_equal [13, '(BERGEN)'] + end + + it "can print it's original form" do + @postcode.to_s.must_equal "1234\tMYTOWN\t1378\tMY MUNICIPALITY\tG" + end +end + +describe 'PostCodes::county' do + it "must give the right county names" do + PostCodes.county(1).must_equal('ØSTFOLD') + PostCodes.county(2).must_equal('AKERSHUS') + PostCodes.county(3).must_equal('OSLO') + PostCodes.county(4).must_equal('HEDMARK') + PostCodes.county(5).must_equal('OPPLAND') + PostCodes.county(6).must_equal('BUSKERUD') + PostCodes.county(7).must_equal('VESTFOLD') + PostCodes.county(8).must_equal('TELEMARK') + PostCodes.county(9).must_equal('AUST-AGDER') + PostCodes.county(10).must_equal('VEST-AGDER') + PostCodes.county(11).must_equal('ROGALAND') + PostCodes.county(12).must_equal('HORDALAND') + PostCodes.county(13).must_equal('(BERGEN)') + PostCodes.county(14).must_equal('SOGN OG FJORDANE') + PostCodes.county(15).must_equal('MØRE OG ROMSDAL') + PostCodes.county(16).must_equal('SØR-TRØNDELAG') + PostCodes.county(17).must_equal('NORD-TRØNDELAG') + PostCodes.county(18).must_equal('NORDLAND') + PostCodes.county(19).must_equal('TROMS') + PostCodes.county(20).must_equal('FINNMARK') + PostCodes.county(21).must_equal('SVALBARD') + PostCodes.county(22).must_equal('JAN MAYEN') + PostCodes.county(23).must_equal('KONTINENTALSOKKELEN') + end + + it "must fail on invalid county codes" do + PostCodes.county(0).must_equal nil + PostCodes.county(25).must_equal nil + end +end
\ No newline at end of file |