swiss_municipality 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/swiss_municipality/municipality_data.rb +96464 -0
- data/lib/swiss_municipality.rb +111 -3
- metadata +18 -7
data/lib/swiss_municipality.rb
CHANGED
@@ -1,5 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# gem build swiss_municipality.gemspec
|
4
|
+
# gem install ./swiss_municipality-0.1.1.gem
|
5
|
+
|
6
|
+
# require 'swiss_municipality'
|
7
|
+
#
|
8
|
+
|
1
9
|
class SwissMunicipality
|
2
|
-
def self.
|
3
|
-
|
10
|
+
def self.canton(**options)
|
11
|
+
options = sanitize_options(options)
|
12
|
+
validate_identifier(options)
|
13
|
+
validate_variant(options)
|
14
|
+
|
15
|
+
raise ArgumentError, 'Argument canton must not be given' unless options[:canton].nil?
|
16
|
+
|
17
|
+
if options[:municipality].nil?
|
18
|
+
zip_code_data = MunicipalityData.zip_hash[options[:zip_code]]
|
19
|
+
|
20
|
+
raise ArgumentError, "No municipality found for zip code #{options[:zip_code]}" if zip_code_data.nil?
|
21
|
+
|
22
|
+
canton_data = zip_code_data[:canton]
|
23
|
+
else
|
24
|
+
municipality_data = MunicipalityData.municipality_hash[options[:municipality]]
|
25
|
+
canton_data = municipality_data[:canton]
|
26
|
+
end
|
27
|
+
|
28
|
+
if options[:variant] == :short
|
29
|
+
canton_data[:short]
|
30
|
+
elsif options[:variant] == :full
|
31
|
+
canton_data[options[:language]]
|
32
|
+
end
|
4
33
|
end
|
5
|
-
|
34
|
+
|
35
|
+
def self.all_cantons(options)
|
36
|
+
options = sanitize_options(options)
|
37
|
+
validate_variant(options)
|
38
|
+
|
39
|
+
if options[:variant] == :short
|
40
|
+
MunicipalityData.canton_hash.keys.sort
|
41
|
+
elsif options[:variant] == :full
|
42
|
+
MunicipalityData.canton_hash.values.map { |canton| canton[:full_name][options[:language]] }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.municipality(**options)
|
47
|
+
options = sanitize_options(options)
|
48
|
+
validate_identifier(options)
|
49
|
+
|
50
|
+
raise ArgumentError, 'Argument municipality must not be given' unless options[:municipality].nil?
|
51
|
+
raise ArgumentError, 'Argument canton must not be given' unless options[:canton].nil?
|
52
|
+
|
53
|
+
zip_code_data = MunicipalityData.zip_hash[options[:zip_code]]
|
54
|
+
zip_code_data[:municipality]
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.all_municipalities(**options)
|
58
|
+
options = sanitize_options(options)
|
59
|
+
|
60
|
+
if options[:canton].nil?
|
61
|
+
MunicipalityData.municipality_hash
|
62
|
+
else
|
63
|
+
MunicipalityData.canton_hash[options[:canton]][:municipalities]
|
64
|
+
end.keys.sort.map(&:to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.all_zip_codes(**options)
|
68
|
+
options = sanitize_options(options)
|
69
|
+
|
70
|
+
zip_code_data = if !options[:municipality].nil?
|
71
|
+
MunicipalityData.municipality_hash[options[:municipality]][:zip_codes]
|
72
|
+
elsif !options[:canton].nil?
|
73
|
+
MunicipalityData.canton_hash[options[:canton]][:zip_codes]
|
74
|
+
else
|
75
|
+
MunicipalityData.zip_hash.keys
|
76
|
+
end
|
77
|
+
|
78
|
+
if options[:variant] == :with_municipality
|
79
|
+
zip_code_data.map { |zip_code| [zip_code, MunicipalityData.zip_hash[zip_code.to_sym][:municipality]].join(' ') }
|
80
|
+
else
|
81
|
+
zip_code_data.map(&:to_s)
|
82
|
+
end.sort
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.sanitize_options(options)
|
86
|
+
options[:variant] = options[:variant]&.to_s&.downcase&.to_sym
|
87
|
+
options[:language] = options[:language]&.to_s&.downcase&.gsub('-ch', '')&.to_s&.to_sym
|
88
|
+
options[:zip_code] = options[:zip_code]&.to_s&.to_sym
|
89
|
+
options[:municipality] = options[:municipality]&.to_s&.to_sym
|
90
|
+
options[:canton] = options[:canton]&.to_s&.to_sym
|
91
|
+
|
92
|
+
options
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.validate_variant(options)
|
96
|
+
unless %i[short full].include? options[:variant]
|
97
|
+
raise ArgumentError, "'Argument variant must be provided. The allowed values are 'short' and 'full'"
|
98
|
+
end
|
99
|
+
|
100
|
+
return unless options[:variant] == :full && !%i[de fr it].include?(options[:language])
|
101
|
+
|
102
|
+
raise ArgumentError,
|
103
|
+
"Language must be provided for variant :full. The allowed values are 'de-CH', 'de', 'fr-CH', 'fr', 'it-CH', 'it'"
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.validate_identifier(options)
|
107
|
+
return unless [options[:zip_code], options[:municipality], options[:canton]].compact.count != 1
|
108
|
+
|
109
|
+
raise ArgumentError, 'Exactly one of the arguments zip_code, municipality or canton must be given'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
require 'swiss_municipality/municipality_data'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swiss_municipality
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Müller
|
@@ -9,12 +9,22 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2023-11-11 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.12'
|
27
|
+
description: A gem to map swiss municipalities to zip codes and cantons.
|
18
28
|
email:
|
19
29
|
- denis@synerma.ch
|
20
30
|
executables: []
|
@@ -22,6 +32,7 @@ extensions: []
|
|
22
32
|
extra_rdoc_files: []
|
23
33
|
files:
|
24
34
|
- lib/swiss_municipality.rb
|
35
|
+
- lib/swiss_municipality/municipality_data.rb
|
25
36
|
homepage: https://rubygems.org/gems/swiss_municipality
|
26
37
|
licenses:
|
27
38
|
- MIT
|