Module: SwissMatch

Defined in:
lib/swissmatch.rb,
lib/swissmatch/name.rb,
lib/swissmatch/canton.rb,
lib/swissmatch/zipcode.rb,
lib/swissmatch/address.rb,
lib/swissmatch/version.rb,
lib/swissmatch/cantons.rb,
lib/swissmatch/zipcodes.rb,
lib/swissmatch/community.rb,
lib/swissmatch/loaderror.rb,
lib/swissmatch/telsearch.rb,
lib/swissmatch/datafiles.rb,
lib/swissmatch/communities.rb,
lib/swissmatch/activerecord.rb,
lib/swissmatch/directoryservice.rb

Overview

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

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

Examples:

Load the data

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

Get the ONRP for a given zip-code + city

require 'swissmatch/autoload'
SwissMatch.zip_code(8000, 'Zürich').ordering_number # => 

Defined Under Namespace

Modules: ActiveRecord Classes: Address, Canton, Cantons, Communities, Community, DataFiles, DirectoryService, LoadError, Name, TelSearch, 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("|")}/
Version =

The version of this package.

Gem::Version.new("0.0.1")

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

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



38
39
40
# File 'lib/swissmatch.rb', line 38

def data
  @data
end

+ (SwissMatch::DirectoryService?) directory_service

The directory service used to search for addresses



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

def directory_service
  @directory_service
end

Class Method Details

+ (Object) canton(name_or_plate)



45
46
47
# File 'lib/swissmatch.rb', line 45

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

+ (Object) cantons



49
50
51
# File 'lib/swissmatch.rb', line 49

def self.cantons
  @data.cantons
end

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/swissmatch.rb', line 149

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)



135
136
137
# File 'lib/swissmatch.rb', line 135

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

+ (Object) communities(name = nil)



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

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

+ (Object) community(key)



53
54
55
# File 'lib/swissmatch.rb', line 53

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

+ (Object) load(data_source = nil)



165
166
167
168
# File 'lib/swissmatch.rb', line 165

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

+ (Object) transliterate1(word)

Used to transliterate city names



207
208
209
# File 'lib/swissmatch.rb', line 207

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

+ (Object) transliterate2(word)

Used to transliterate city names



213
214
215
# File 'lib/swissmatch.rb', line 213

def self.transliterate2(word)
  word.gsub(TransMatch2, Transliteration2).delete("^ A-Za-z").downcase
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>


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/swissmatch.rb', line 117

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)



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/swissmatch.rb', line 67

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