Module: SwissMatch

Defined in:
lib/swissmatch/location.rb,
lib/swissmatch/name.rb,
lib/swissmatch/canton.rb,
lib/swissmatch/zipcode.rb,
lib/swissmatch/cantons.rb,
lib/swissmatch/zipcodes.rb,
lib/swissmatch/community.rb,
lib/swissmatch/datafiles.rb,
lib/swissmatch/loaderror.rb,
lib/swissmatch/communities.rb,
lib/swissmatch/location/version.rb

Overview

Note:

All strings passed to SwissMatch are expected to be utf-8. All strings returned by SwissMatch are also in utf-8.

From SwissMatch::Location Deal with swiss zip codes, cities, communities and cantons.

Examples:

Load the data

require 'swissmatch'
SwissMatch.load
# alternatively, just require 'swissmatch/location/autoload'

Get the ONRP for a given zip-code + city

require 'swissmatch/location/autoload'
SwissMatch.zip_code(8000, 'Zürich').ordering_number # => 4384
SwissMatch.zip_code(4384).name # => "Zürich"(de, 0)

Defined Under Namespace

Modules: Location Classes: Canton, Cantons, Communities, Community, DataFiles, LoadError, Name, ZipCode, ZipCodes

Constant Summary

Transliteration1 =

Used to transliterate city names

{
  "à" => "a",
  "â" => "a",
  "ä" => "a",
  "è" => "e",
  "é" => "e",
  "ê" => "e",
  "ë" => "e",
  "ì" => "i",
  "î" => "i",
  "ï" => "i",
  "ô" => "o",
  "ö" => "o",
  "ù" => "u",
  "ü" => "u",
}
Transliteration2 =

Used to transliterate city names

Transliteration1.merge({
  "ä" => "ae",
  "ö" => "oe",
  "ü" => "ue",
})
TransMatch1 =

Used to transliterate city names

/#{Transliteration1.keys.map { |k| Regexp.escape(k) }.join("|")}/
TransMatch2 =

Used to transliterate city names

/#{Transliteration2.keys.map { |k| Regexp.escape(k) }.join("|")}/

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (SwissMatch::DataFiles?) data (readonly)

Returns The data source used

Returns:



43
44
45
# File 'lib/swissmatch/location.rb', line 43

def data
  @data
end

Class Method Details

+ (SwissMatch::Canton) canton(name_or_plate)

Returns The canton with the given name or license_tag

Parameters:

  • name_or_plate (String)

    The name or license_tag of the canton

Returns:



51
52
53
# File 'lib/swissmatch/location.rb', line 51

def self.canton(name_or_plate)
  @data.cantons[name_or_plate]
end

+ (SwissMatch::Cantons) cantons

Returns All known cantons

Returns:



57
58
59
# File 'lib/swissmatch/location.rb', line 57

def self.cantons
  @data.cantons
end

+ (Array<String>) cities_for_zip_code(code, only_types = nil, locale = nil)

Returns A list of unique names matching the parameters (4 digit code, type, locale).

Parameters:

  • code (String, Integer)

    The 4 digit zip code

  • only_types (nil, Array<Integer>) (defaults to: nil)

    An array of zip code types (see ZipCode#type) which the returned zip codes must match.

  • locale (nil, Symbol) (defaults to: nil)

    Return the names in the given locale, defaults to nil/:native (nil and :native are treated the same and will return the native names)

Returns:

  • (Array<String>)

    A list of unique names matching the parameters (4 digit code, type, locale).



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/swissmatch/location.rb', line 167

def self.cities_for_zip_code(code, only_types=nil, locale=nil)
  codes = @data.zip_codes.by_code(code.to_i)
  return [] unless codes
  codes = codes.select { |code| only_types.include?(code.type) } if only_types
  names = case locale
    when :native,nil then codes.map(&:name)
    when :de then codes.map(&:name_de)
    when :fr then codes.map(&:name_fr)
    when :it then codes.map(&:name_it)
    when :rt then codes.map(&:name_rt)
    else raise ArgumentError, "Invalid locale #{locale}"
  end

  names.uniq
end

+ (Array<SwissMatch::ZipCode>) city(name)

Returns Zip codes whose name equals the given name

Parameters:

  • name (String)

    The name for which to return matching zip codes

Returns:



153
154
155
# File 'lib/swissmatch/location.rb', line 153

def self.city(name)
  @data.zip_codes.by_name(name)
end

+ (SwissMatch::Communities) communities(name = nil)

Returns All communities, or those matching the given name

Parameters:

  • name (String) (defaults to: nil)

    The name of the communities

Returns:



75
76
77
# File 'lib/swissmatch/location.rb', line 75

def self.communities(name=nil)
  name ? @data.communities.by_name(name) : @data.communities
end

+ (SwissMatch::Community) community(key)

Returns The community with the community number

Parameters:

  • key (Integer)

    The community number of the community

Returns:



66
67
68
# File 'lib/swissmatch/location.rb', line 66

def self.community(key)
  @data.communities.by_community_number(key)
end

+ (self) load(data_source = nil)

Loads the swissmatch data

Parameters:

  • data_source (DataFiles) (defaults to: nil)

    A valid data-source.

Returns:

  • (self)

    Returns self



190
191
192
193
194
195
# File 'lib/swissmatch/location.rb', line 190

def self.load(data_source=nil)
  @data = data_source || DataFiles.new
  @data.load!

  self
end

+ (Object) transliterate1(word)

Used to transliterate city names



234
235
236
# File 'lib/swissmatch/location.rb', line 234

def self.transliterate1(word)
  word.gsub(TransMatch1, Transliteration1).delete("^ A-Za-z").downcase
end

+ (Object) transliterate2(word)

Used to transliterate city names



240
241
242
# File 'lib/swissmatch/location.rb', line 240

def self.transliterate2(word)
  word.gsub(TransMatch2, Transliteration2).delete("^ A-Za-z").downcase
end

+ (Object) transliterated_words(string)

Transliterates a string into the unique transliterated (1 & 2) word list



246
247
248
# File 'lib/swissmatch/location.rb', line 246

def self.transliterated_words(string)
  "#{transliterate1(string)} #{transliterate2(string)}".split(" ").uniq
end

+ (SwissMatch::ZipCode) zip_code(code, city_or_add_on = nil)

Returns a single zip code. A zip code can be uniquely identified by any of: * Its ordering_number (ONRP, a 4 digit Integer) * Its zip code (4 digit Integer) and add-on (2 digit Integer) * Its zip code (4 digit Integer) and any official name (String) The data can be passed in different ways, e.g. all numbers can be passed either as a String or as an Integer. The identification by zip code and add-on can be done by either using a combined 6 digit number (e.g. 800000 for "8000 Zürich"), or by passing 2 arguments, passing the zip code and the add-on separately.

=== IMPORTANT You must be aware, that passing a single 4-digit code to SwissMatch::zip_code uses the ONRP, and NOT the zip-code. The 4 digit zip code alone does NOT uniquely identify a zip code.

Examples:

Get a zip code by ONRP

SwissMatch.zip_code(4384)           # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>

Get a zip code by 4-digit code and add-on

SwissMatch.zip_code(8000, 0)        # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
SwissMatch.zip_code("8000", "00")   # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
SwissMatch.zip_code(800000)         # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
SwissMatch.zip_code("800000")       # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>

Get a zip code by 4-digit code and name

SwissMatch.zip_code(8000, "Zürich") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
SwissMatch.zip_code(8000, "Zurigo") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>

Parameters:

  • code (String, Integer)

    The 4 digit zip code as Integer or String

  • city_or_add_on (String, Integer) (defaults to: nil)

    Either the 2 digit zip-code add-on as string or integer, or the city name as a String in utf-8.

Returns:



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/swissmatch/location.rb', line 135

def self.zip_code(code, city_or_add_on=nil)
  case city_or_add_on
    when nil
      @data.zip_codes.by_ordering_number(code.to_i)
    when Integer, /\A\d\d\z/
      @data.zip_codes.by_code_and_add_on(code.to_i, city_or_add_on.to_i)
    when String
      @data.zip_codes.by_code_and_name(code.to_i, city_or_add_on)
    else
      raise ArgumentError, "Invalid second argument, must be nil, ZipCode#add_on or ZipCode#name"
  end
end

+ (Array<SwissMatch::ZipCode>) zip_codes(code_or_name = nil)

Returns A list of zip codes with the given code or name.

Parameters:

  • code_or_name (String, Integer) (defaults to: nil)

    Either the 4 digit zip code as Integer or String, or the city name as a String in utf-8.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/swissmatch/location.rb', line 85

def self.zip_codes(code_or_name=nil)
  case code_or_name
    when Integer, /\A\d{4}\z/
      @data.zip_codes.by_code(code_or_name.to_i)
    when String
      @data.zip_codes.by_name(code_or_name)
    when nil
      @data.zip_codes
    else
      raise ArgumentError, "Invalid argument, must be a ZipCode#code (Integer or String) or ZipCode#name (String)"
  end
end