Module: SwissMatch::Location

Defined in:
lib/swissmatch/location.rb,
lib/swissmatch/location/version.rb,
lib/swissmatch/location/converter.rb,
lib/swissmatch/location/datafiles.rb

Overview

This module only contains the version of the swissmatch-location gem

Defined Under Namespace

Classes: Converter, DataFiles

Constant Summary collapse

Version =

The version of the swissmatch-location gem.

Gem::Version.new("1.0.1")

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataSwissMatch::Location::DataFiles? (readonly)

Returns The data source used.

Returns:


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

def data
  @data
end

Class Method Details

.canton(name_or_plate) ⇒ SwissMatch::Canton

Returns The canton with the given name or license_tag.

Parameters:

  • name_or_plate (String)

    The name or license_tag of the canton

Returns:


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

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

.cantonsSwissMatch::Cantons

Returns All known cantons.

Returns:


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

def self.cantons
  @data.cantons
end

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

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).


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/swissmatch/location.rb', line 185

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

.city(name) ⇒ Array<SwissMatch::ZipCode>

Returns Zip codes whose name equals the given name.

Parameters:

  • name (String)

    The name for which to return matching zip codes

Returns:


171
172
173
# File 'lib/swissmatch/location.rb', line 171

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

.communitiesSwissMatch::Communities

Returns All communities.

Returns:


93
94
95
# File 'lib/swissmatch/location.rb', line 93

def self.communities
  @data.communities
end

.community(key) ⇒ SwissMatch::Community

Returns The community with the given name or community number.

Parameters:

  • key (Integer, String)

    The name or community number of the community

Returns:


80
81
82
83
84
85
86
87
88
89
# File 'lib/swissmatch/location.rb', line 80

def self.community(key)
  case key
    when Integer
      @data.communities.by_community_number(key)
    when String
      @data.communities.by_name(key)
    else
      raise TypeError, "Expected Integer or String, but got #{key.inspect}:#{key.class}"
  end
end

.district(district_number_or_name) ⇒ SwissMatch::District

Returns The district with the given district_number or name.

Parameters:

  • district_number_or_name (String)

    The district_number or name of the district

Returns:


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

def self.district(district_number_or_name)
  @data.districts[district_number_or_name]
end

.districtsSwissMatch::Districts

Returns All known districts.

Returns:


71
72
73
# File 'lib/swissmatch/location.rb', line 71

def self.districts
  @data.districts
end

.load(data_source = nil) ⇒ self

Loads the swissmatch data

Parameters:

Returns:

  • (self)

    Returns self


208
209
210
211
212
213
# File 'lib/swissmatch/location.rb', line 208

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

  self
end

.zip_code(code, city_or_add_on = nil) ⇒ SwissMatch::ZipCode

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:


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/swissmatch/location.rb', line 153

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

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

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:


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/swissmatch/location.rb', line 103

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