From 9af117df2f9e26a151a24caf54b967f56c08d60a Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 2 Feb 2015 18:59:12 +0100 Subject: Rename files and prepare for release. --- lib/norwegian-postcodes.rb | 117 ------------------------------------- lib/norwegian-postcodes/railtie.rb | 11 ---- lib/postcodes-norway.rb | 117 +++++++++++++++++++++++++++++++++++++ lib/postcodes-norway/railtie.rb | 11 ++++ lib/tasks/norwegian-postcodes.rake | 13 ----- lib/tasks/postcodes-norway.rake | 13 +++++ 6 files changed, 141 insertions(+), 141 deletions(-) delete mode 100644 lib/norwegian-postcodes.rb delete mode 100644 lib/norwegian-postcodes/railtie.rb create mode 100644 lib/postcodes-norway.rb create mode 100644 lib/postcodes-norway/railtie.rb delete mode 100644 lib/tasks/norwegian-postcodes.rake create mode 100644 lib/tasks/postcodes-norway.rake (limited to 'lib') diff --git a/lib/norwegian-postcodes.rb b/lib/norwegian-postcodes.rb deleted file mode 100644 index 6aefc98..0000000 --- a/lib/norwegian-postcodes.rb +++ /dev/null @@ -1,117 +0,0 @@ -module PostCodes - - require 'norwegian-postcodes/railtie' if defined?(Rails) - - # Correspond to the Norwegian 'Fylke' - 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 for holding postcode data - class PostCode - # Four digit post code - attr_reader :postcode - - # Name of city - attr_reader :city - - # Four digit municipality ('kommune') id - attr_reader :municipality - - # Name of municipality ('kommune') - attr_reader :municipality_name - - # Category - # * 'G' = street address - # * 'P' = Postboxes - # * 'B' = Both street addresses and postboxes - # * 'S' = Service addresses - # * 'K' = Customer with its own post code - # * 'F' = Multiple uses - attr_reader :cat - - # Create a new post code. - # This method should generally not be used by users of the library. - def initialize(postcode, city, muni, muni_name, cat) - @postcode, @city, @municipality, @municipality_name, @cat = postcode, city, muni, muni_name, cat - end - - # Return the County ('kommune') from the postcode data. - # The returned format is `[county_id, county_name]` - def county - code = @municipality[0..1].to_i - [code, PostCodes.county(code)] - end - - # Output postcode data in the same format as in the original postcode database. - def to_s - [@postcode, @city, @municipality, @municipality_name, @cat].join("\t") - end - end - - class << self - - # Load the postcode data into memory. - # - # +file+ is a file name or IO object to read the data from. - def load(file) - @postcodes = [] - if file.is_a?(String) - f = File.open(file, :encoding => Encoding::ISO_8859_15) - else - f = file - end - - f.each_line do |l| - a = l.chomp().split("\t").map{|s| s.encode(Encoding::UTF_8)} - @postcodes << PostCode.new(*a) - end - end - - # Search for a given postcode. - # - # Takes a 4 digit postcode as a string, and returns an object of - # type PostCodes::PostCode, or +nil+ if the postcode was not found. - # - def search(postcode) - res = @postcodes.bsearch {|x| x.postcode.to_i >= postcode.to_i} - unless res.nil? - res.postcode.to_i == postcode.to_i ? res : nil - end - end - - # Return the county by number. - # - # Takes a number between 1 and 23 as input. This corresponds to the - # first two digits in the municipality number. - # - # The county name as a string is returned. - # - def county(index) - return nil unless index > 0 && index <= Counties.size - Counties[index - 1] - end - end -end diff --git a/lib/norwegian-postcodes/railtie.rb b/lib/norwegian-postcodes/railtie.rb deleted file mode 100644 index 19cd3e7..0000000 --- a/lib/norwegian-postcodes/railtie.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'norwegian-postcodes' -require 'rails' - -module PostCodes - class Railtie < Rails::Railtie - railtie_name :norwegian_postcodes - rake_tasks do - load 'tasks/norwegian-postcodes.rake' - end - end -end \ No newline at end of file diff --git a/lib/postcodes-norway.rb b/lib/postcodes-norway.rb new file mode 100644 index 0000000..6aefc98 --- /dev/null +++ b/lib/postcodes-norway.rb @@ -0,0 +1,117 @@ +module PostCodes + + require 'norwegian-postcodes/railtie' if defined?(Rails) + + # Correspond to the Norwegian 'Fylke' + 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 for holding postcode data + class PostCode + # Four digit post code + attr_reader :postcode + + # Name of city + attr_reader :city + + # Four digit municipality ('kommune') id + attr_reader :municipality + + # Name of municipality ('kommune') + attr_reader :municipality_name + + # Category + # * 'G' = street address + # * 'P' = Postboxes + # * 'B' = Both street addresses and postboxes + # * 'S' = Service addresses + # * 'K' = Customer with its own post code + # * 'F' = Multiple uses + attr_reader :cat + + # Create a new post code. + # This method should generally not be used by users of the library. + def initialize(postcode, city, muni, muni_name, cat) + @postcode, @city, @municipality, @municipality_name, @cat = postcode, city, muni, muni_name, cat + end + + # Return the County ('kommune') from the postcode data. + # The returned format is `[county_id, county_name]` + def county + code = @municipality[0..1].to_i + [code, PostCodes.county(code)] + end + + # Output postcode data in the same format as in the original postcode database. + def to_s + [@postcode, @city, @municipality, @municipality_name, @cat].join("\t") + end + end + + class << self + + # Load the postcode data into memory. + # + # +file+ is a file name or IO object to read the data from. + def load(file) + @postcodes = [] + if file.is_a?(String) + f = File.open(file, :encoding => Encoding::ISO_8859_15) + else + f = file + end + + f.each_line do |l| + a = l.chomp().split("\t").map{|s| s.encode(Encoding::UTF_8)} + @postcodes << PostCode.new(*a) + end + end + + # Search for a given postcode. + # + # Takes a 4 digit postcode as a string, and returns an object of + # type PostCodes::PostCode, or +nil+ if the postcode was not found. + # + def search(postcode) + res = @postcodes.bsearch {|x| x.postcode.to_i >= postcode.to_i} + unless res.nil? + res.postcode.to_i == postcode.to_i ? res : nil + end + end + + # Return the county by number. + # + # Takes a number between 1 and 23 as input. This corresponds to the + # first two digits in the municipality number. + # + # The county name as a string is returned. + # + def county(index) + return nil unless index > 0 && index <= Counties.size + Counties[index - 1] + end + end +end diff --git a/lib/postcodes-norway/railtie.rb b/lib/postcodes-norway/railtie.rb new file mode 100644 index 0000000..19cd3e7 --- /dev/null +++ b/lib/postcodes-norway/railtie.rb @@ -0,0 +1,11 @@ +require 'norwegian-postcodes' +require 'rails' + +module PostCodes + class Railtie < Rails::Railtie + railtie_name :norwegian_postcodes + rake_tasks do + load 'tasks/norwegian-postcodes.rake' + end + end +end \ No newline at end of file diff --git a/lib/tasks/norwegian-postcodes.rake b/lib/tasks/norwegian-postcodes.rake deleted file mode 100644 index 6ec53b7..0000000 --- a/lib/tasks/norwegian-postcodes.rake +++ /dev/null @@ -1,13 +0,0 @@ -require 'uri' -require 'open-uri' - -namespace :postcodes do - desc "Fetch postcode database from Bring" - task :fetch, [:data_path] do |t, args| - args.with_defaults(:data_path => 'data') - uri = URI.parse('http://www.bring.no/hele-bring/forside/_attachment/159761') - filename = File.join(args.data_path, 'Postnummerregister_ansi.txt') - puts "Downloading postcodes to: #{filename}..." - IO.write(filename, uri.read) - end -end diff --git a/lib/tasks/postcodes-norway.rake b/lib/tasks/postcodes-norway.rake new file mode 100644 index 0000000..6ec53b7 --- /dev/null +++ b/lib/tasks/postcodes-norway.rake @@ -0,0 +1,13 @@ +require 'uri' +require 'open-uri' + +namespace :postcodes do + desc "Fetch postcode database from Bring" + task :fetch, [:data_path] do |t, args| + args.with_defaults(:data_path => 'data') + uri = URI.parse('http://www.bring.no/hele-bring/forside/_attachment/159761') + filename = File.join(args.data_path, 'Postnummerregister_ansi.txt') + puts "Downloading postcodes to: #{filename}..." + IO.write(filename, uri.read) + end +end -- cgit v1.2.3