module PostCodes
Constants
- Counties
Correspond to the Norwegian ‘Fylke’
Public Class Methods
county(index)
click to toggle source
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.
# File lib/postcodes-norway.rb, line 112 def county(index) return nil unless index > 0 && index <= Counties.size Counties[index - 1] end
load(file)
click to toggle source
Load the postcode data into memory.
file
is a file name or IO object to read the data from.
# File lib/postcodes-norway.rb, line 79 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(postcode)
click to toggle source
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.
# File lib/postcodes-norway.rb, line 98 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