Module: SwissMatch::Location
- Defined in:
- lib/swissmatch/location.rb,
lib/swissmatch/location/version.rb,
lib/swissmatch/location/datafiles.rb
Overview
This module only contains the version of the swissmatch-location gem
Defined Under Namespace
Classes: DataFiles
Constant Summary collapse
- Version =
The version of the swissmatch-location gem.
Gem::Version.new("0.1.2.201304")
Class Attribute Summary collapse
-
.data ⇒ SwissMatch::Location::DataFiles?
readonly
The data source used.
Class Method Summary collapse
-
.canton(name_or_plate) ⇒ SwissMatch::Canton
The canton with the given name or license_tag.
-
.cantons ⇒ SwissMatch::Cantons
All known cantons.
-
.cities_for_zip_code(code, only_types = nil, locale = nil) ⇒ Array<String>
A list of unique names matching the parameters (4 digit code, type, locale).
-
.city(name) ⇒ Array<SwissMatch::ZipCode>
Zip codes whose name equals the given name.
-
.communities(name = nil) ⇒ SwissMatch::Communities
All communities, or those matching the given name.
-
.community(key) ⇒ SwissMatch::Community
The community with the community number.
-
.district(district_number_or_name) ⇒ SwissMatch::District
The district with the given district_number or name.
-
.districts ⇒ SwissMatch::Districts
All known districts.
-
.load(data_source = nil) ⇒ self
Loads the swissmatch data.
-
.zip_code(code, city_or_add_on = nil) ⇒ SwissMatch::ZipCode
Returns a single zip code.
-
.zip_codes(code_or_name = nil) ⇒ Array<SwissMatch::ZipCode>
A list of zip codes with the given code or name.
Class Attribute Details
.data ⇒ SwissMatch::Location::DataFiles? (readonly)
Returns The data source used.
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.
50 51 52 |
# File 'lib/swissmatch/location.rb', line 50 def self.canton(name_or_plate) @data.cantons[name_or_plate] end |
.cantons ⇒ SwissMatch::Cantons
Returns All known cantons.
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).
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/swissmatch/location.rb', line 181 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.
167 168 169 |
# File 'lib/swissmatch/location.rb', line 167 def self.city(name) @data.zip_codes.by_name(name) end |
.communities(name = nil) ⇒ SwissMatch::Communities
Returns All communities, or those matching the given name.
89 90 91 |
# File 'lib/swissmatch/location.rb', line 89 def self.communities(name=nil) name ? @data.communities.by_name(name) : @data.communities end |
.community(key) ⇒ SwissMatch::Community
Returns The community with the community number.
80 81 82 |
# File 'lib/swissmatch/location.rb', line 80 def self.community(key) @data.communities.by_community_number(key) end |
.district(district_number_or_name) ⇒ SwissMatch::District
Returns The district with the given district_number or name.
65 66 67 |
# File 'lib/swissmatch/location.rb', line 65 def self.district(district_number_or_name) @data.districts[district_number_or_name] end |
.districts ⇒ SwissMatch::Districts
Returns All known districts.
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
204 205 206 207 208 209 |
# File 'lib/swissmatch/location.rb', line 204 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.
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/swissmatch/location.rb', line 149 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.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/swissmatch/location.rb', line 99 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 |