aboutsummaryrefslogtreecommitdiffstats
path: root/lib/postcodes-norway.rb
blob: 3726cb88416dd5491aec75746d84e7614cd7c9a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# postcodes-norway: Access norwegian postcode data from Ruby.
# Copyright (C) 2015  Harald Eilertsen <haraldei@anduin.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

module PostCodes

  require 'postcodes-norway/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