Module: PostCodes
- Defined in:
- lib/postcodes-norway.rb,
lib/postcodes-norway/railtie.rb
Defined Under Namespace
Constant Summary collapse
- Counties =
Correspond to the Norwegian ‘Fylke’
[ "Ø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 Method Summary collapse
-
.county(index) ⇒ Object
Return the county by number.
-
.load(file) ⇒ Object
Load the postcode data into memory.
-
.search(postcode) ⇒ Object
Search for a given postcode.
Class Method Details
.county(index) ⇒ Object
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.
112 113 114 115 |
# File 'lib/postcodes-norway.rb', line 112 def county(index) return nil unless index > 0 && index <= Counties.size Counties[index - 1] end |
.load(file) ⇒ Object
Load the postcode data into memory.
file
is a file name or IO object to read the data from.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
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.
98 99 100 101 102 103 |
# 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 |