swissmatch-rails 0.0.1 → 0.0.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.
Files changed (3) hide show
  1. data/swissmatch-rails.gemspec +1 -3
  2. metadata +2 -19
  3. data/lib/swissmatch.rb +0 -216
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "swissmatch-rails"
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
  s.authors = "Stefan Rusterholz"
7
7
  s.email = "stefan.rusterholz@gmail.com"
8
8
  s.homepage = "http://github.com/apeiros/swissmatch-rails"
@@ -33,8 +33,6 @@ Gem::Specification.new do |s|
33
33
  s.executables = executables unless executables.empty?
34
34
  end
35
35
 
36
- s.add_dependency "swissmatch"
37
-
38
36
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1")
39
37
  s.rubygems_version = "1.3.1"
40
38
  s.specification_version = 3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swissmatch-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,23 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-08-08 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: swissmatch
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
13
+ dependencies: []
30
14
  description: Adds ActiveRecord models and javascript assets for rails to swissmatch.
31
15
  email: stefan.rusterholz@gmail.com
32
16
  executables:
@@ -37,7 +21,6 @@ files:
37
21
  - bin/swissmatch_db
38
22
  - lib/swissmatch/activerecord.rb
39
23
  - lib/swissmatch/rails.rb
40
- - lib/swissmatch.rb
41
24
  - swissmatch-rails.gemspec
42
25
  - LICENSE.txt
43
26
  - Rakefile
data/lib/swissmatch.rb DELETED
@@ -1,216 +0,0 @@
1
- # encoding: utf-8
2
-
3
-
4
-
5
- require 'date'
6
- require 'swissmatch/ruby'
7
- require 'swissmatch/canton'
8
- require 'swissmatch/cantons'
9
- require 'swissmatch/community'
10
- require 'swissmatch/communities'
11
- require 'swissmatch/datafiles'
12
- require 'swissmatch/version'
13
- require 'swissmatch/zipcode'
14
- require 'swissmatch/zipcodes'
15
-
16
-
17
-
18
- # SwissMatch
19
- # Deal with swiss zip codes, cities, communities and cantons.
20
- #
21
- # Notice that all strings passed to SwissMatch are expected to be utf-8. All strings
22
- # returned by SwissMatch are also in utf-8.
23
- #
24
- # @example Load the data
25
- # require 'swissmatch'
26
- # SwissMatch.load
27
- # # alternatively, just require 'swissmatch/autoload'
28
- #
29
- # @example Get the ONRP for a given zip-code + city
30
- # require 'swissmatch/autoload'
31
- # SwissMatch.zip_code(8000, 'Zürich').ordering_number # =>
32
- module SwissMatch
33
- @data = nil
34
- @directory_service = nil
35
-
36
- class <<self
37
- # @return [SwissMatch::DataFiles, nil] The data source used
38
- attr_reader :data
39
-
40
- # @return [SwissMatch::DirectoryService, nil]
41
- # The directory service used to search for addresses
42
- attr_accessor :directory_service
43
- end
44
-
45
- def self.canton(name_or_plate)
46
- @data.cantons[name_or_plate]
47
- end
48
-
49
- def self.cantons
50
- @data.cantons
51
- end
52
-
53
- def self.community(key)
54
- @data.communities.by_community_number(key)
55
- end
56
-
57
- def self.communities(name=nil)
58
- name ? @data.communities.by_name(name) : @data.communities
59
- end
60
-
61
- # @param [String, Integer] code_or_name
62
- # Either the 4 digit zip code as Integer or String, or the city name as a String in
63
- # utf-8.
64
- #
65
- # @return [Array<SwissMatch::ZipCode>]
66
- # A list of zip codes with the given code or name.
67
- def self.zip_codes(code_or_name=nil)
68
- case code_or_name
69
- when Integer, /\A\d{4}\z/
70
- @data.zip_codes.by_code(code_or_name.to_i)
71
- when String
72
- @data.zip_codes.by_name(code_or_name)
73
- when nil
74
- @data.zip_codes
75
- else
76
- raise ArgumentError, "Invalid argument, must be a ZipCode#code (Integer or String) or ZipCode#name (String)"
77
- end
78
- end
79
-
80
- # Returns a single zip code. A zip code can be uniquely identified by any of:
81
- # * Its ordering_number (ONRP, a 4 digit Integer)
82
- # * Its zip code (4 digit Integer) and add-on (2 digit Integer)
83
- # * Its zip code (4 digit Integer) and any official name (String)
84
- # The data can be passed in different ways, e.g. all numbers can be passed either
85
- # as a String or as an Integer. The identification by zip code and add-on can be done
86
- # by either using a combined 6 digit number (e.g. 800000 for "8000 Zürich"), or by
87
- # passing 2 arguments, passing the zip code and the add-on separately.
88
- #
89
- # === IMPORTANT
90
- # You must be aware, that passing a single 4-digit code to SwissMatch::zip_code uses
91
- # the ONRP, and NOT the zip-code. The 4 digit zip code alone does NOT uniquely identify
92
- # a zip code.
93
- #
94
- #
95
- # @example Get a zip code by ONRP
96
- # SwissMatch.zip_code(4384) # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
97
- #
98
- # @example Get a zip code by 4-digit code and add-on
99
- # SwissMatch.zip_code(8000, 0) # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
100
- # SwissMatch.zip_code("8000", "00") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
101
- # SwissMatch.zip_code(800000) # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
102
- # SwissMatch.zip_code("800000") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
103
- #
104
- # @example Get a zip code by 4-digit code and name
105
- # SwissMatch.zip_code(8000, "Zürich") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
106
- # SwissMatch.zip_code(8000, "Zurigo") # => #<SwissMatch::ZipCode:003ff996cf8d3c 8000 Zürich>
107
- #
108
- #
109
- # @param [String, Integer] code
110
- # The 4 digit zip code as Integer or String
111
- # @param [String, Integer] city_or_add_on
112
- # Either the 2 digit zip-code add-on as string or integer, or the city name as a
113
- # String in utf-8.
114
- #
115
- # @return [SwissMatch::ZipCode]
116
- # The zip codes with the given code and the given add-on or name.
117
- def self.zip_code(code, city_or_add_on=nil)
118
- case city_or_add_on
119
- when nil
120
- @data.zip_codes.by_ordering_number(code.to_i)
121
- when Integer, /\A\d\d\z/
122
- @data.zip_codes.by_code_and_add_on(code.to_i, city_or_add_on.to_i)
123
- when String
124
- @data.zip_codes.by_code_and_name(code.to_i, city_or_add_on)
125
- else
126
- raise ArgumentError, "Invalid second argument, must be nil, ZipCode#add_on or ZipCode#name"
127
- end
128
- end
129
-
130
- # @param [String] name
131
- # The name for which to return matching zip codes
132
- #
133
- # @return [Array<SwissMatch::ZipCode>]
134
- # Zip codes whose name equals the given name
135
- def self.city(name)
136
- @data.zip_codes.by_name(name)
137
- end
138
-
139
- # @param [String, Integer] code
140
- # The 4 digit zip code
141
- # @param [nil, Array<Integer>] only_types
142
- # An array of zip code types (see ZipCode#type) which the returned zip codes must match.
143
- # @param [nil, Symbol] locale
144
- # Return the names in the given locale, defaults to nil/:native (nil and :native are
145
- # treated the same and will return the native names)
146
- #
147
- # @return [Array<String>]
148
- # A list of unique names matching the parameters (4 digit code, type, locale).
149
- def self.cities_for_zip_code(code, only_types=nil, locale=nil)
150
- codes = @data.zip_codes.by_code(code.to_i)
151
- return [] unless codes
152
- codes = codes.select { |code| only_types.include?(code.type) } if only_types
153
- names = case locale
154
- when :native,nil then codes.map(&:name)
155
- when :de then codes.map(&:name_de)
156
- when :fr then codes.map(&:name_fr)
157
- when :it then codes.map(&:name_it)
158
- when :rt then codes.map(&:name_rt)
159
- else raise ArgumentError, "Invalid locale #{locale}"
160
- end
161
-
162
- names.uniq
163
- end
164
-
165
- def self.load(data_source=nil)
166
- @data = data_source || DataFiles.new
167
- @data.load!
168
- end
169
-
170
- # @private
171
- # Used to transliterate city names
172
- Transliteration1 = {
173
- "à" => "a",
174
- "â" => "a",
175
- "ä" => "a",
176
- "è" => "e",
177
- "é" => "e",
178
- "ê" => "e",
179
- "ë" => "e",
180
- "ì" => "i",
181
- "î" => "i",
182
- "ï" => "i",
183
- "ô" => "o",
184
- "ö" => "o",
185
- "ù" => "u",
186
- "ü" => "u",
187
- }
188
-
189
- # @private
190
- # Used to transliterate city names
191
- Transliteration2 = Transliteration1.merge({
192
- "ä" => "ae",
193
- "ö" => "oe",
194
- "ü" => "ue",
195
- })
196
-
197
- # @private
198
- # Used to transliterate city names
199
- TransMatch1 = /#{Transliteration1.keys.map { |k| Regexp.escape(k) }.join("|")}/
200
-
201
- # @private
202
- # Used to transliterate city names
203
- TransMatch2 = /#{Transliteration2.keys.map { |k| Regexp.escape(k) }.join("|")}/
204
-
205
- # @private
206
- # Used to transliterate city names
207
- def self.transliterate1(word)
208
- word.gsub(TransMatch1, Transliteration1).delete("^ A-Za-z").downcase
209
- end
210
-
211
- # @private
212
- # Used to transliterate city names
213
- def self.transliterate2(word)
214
- word.gsub(TransMatch2, Transliteration2).delete("^ A-Za-z").downcase
215
- end
216
- end