beerdb 0.6.6 → 0.6.7
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.
- data/lib/beerdb/models/beer.rb +8 -4
- data/lib/beerdb/models/brewery.rb +18 -136
- data/lib/beerdb/version.rb +1 -1
- metadata +12 -12
data/lib/beerdb/models/beer.rb
CHANGED
@@ -4,6 +4,10 @@ module BeerDb::Models
|
|
4
4
|
|
5
5
|
class Beer < ActiveRecord::Base
|
6
6
|
|
7
|
+
# NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
|
8
|
+
# self.create_or_update_from_values
|
9
|
+
extend TextUtils::ValueHelper # e.g. is_year?, is_region?, is_address?, is_taglist? etc.
|
10
|
+
|
7
11
|
belongs_to :country, :class_name => 'WorldDb::Models::Country', :foreign_key => 'country_id'
|
8
12
|
belongs_to :region, :class_name => 'WorldDb::Models::Region', :foreign_key => 'region_id'
|
9
13
|
belongs_to :city, :class_name => 'WorldDb::Models::City', :foreign_key => 'city_id'
|
@@ -79,7 +83,7 @@ class Beer < ActiveRecord::Base
|
|
79
83
|
value_region_key = value[7..-1] ## cut off region: prefix
|
80
84
|
value_region = Region.find_by_key_and_country_id!( value_region_key, new_attributes[:country_id] )
|
81
85
|
new_attributes[ :region_id ] = value_region.id
|
82
|
-
elsif value
|
86
|
+
elsif is_region?( value ) ## assume region code e.g. TX or N
|
83
87
|
value_region = Region.find_by_key_and_country_id!( value.downcase, new_attributes[:country_id] )
|
84
88
|
new_attributes[ :region_id ] = value_region.id
|
85
89
|
elsif value =~ /^city:/ ## city:
|
@@ -111,9 +115,9 @@ class Beer < ActiveRecord::Base
|
|
111
115
|
new_attributes[ :region_id ] = value_brewery.region.id
|
112
116
|
end
|
113
117
|
|
114
|
-
elsif value
|
118
|
+
elsif is_year?( value ) # founded/established year e.g. 1776
|
115
119
|
new_attributes[ :since ] = value.to_i
|
116
|
-
elsif value
|
120
|
+
elsif is_website?( value ) # check for url/internet address e.g. www.ottakringer.at
|
117
121
|
# fix: support more url format (e.g. w/o www. - look for .com .country code etc.)
|
118
122
|
new_attributes[ :web ] = value
|
119
123
|
elsif value =~ /^<?\s*(\d+(?:\.\d+)?)\s*%$/ ## abv (alcohol by volumee)
|
@@ -128,7 +132,7 @@ class Beer < ActiveRecord::Base
|
|
128
132
|
## nb: allow 44.4 kcal/100ml or 44.4 kcal or 44.4kcal
|
129
133
|
value_kcal_str = $1.dup # convert to decimal? how? use float?
|
130
134
|
new_attributes[ :kcal ] = value_kcal_str
|
131
|
-
elsif (values.size==(index+1)) && value
|
135
|
+
elsif (values.size==(index+1)) && is_taglist?( value ) # tags must be last entry
|
132
136
|
|
133
137
|
logger.debug " found tags: >>#{value}<<"
|
134
138
|
|
@@ -4,6 +4,10 @@ module BeerDb::Models
|
|
4
4
|
|
5
5
|
class Brewery < ActiveRecord::Base
|
6
6
|
|
7
|
+
# NB: use extend - is_<type>? become class methods e.g. self.is_<type>? for use in
|
8
|
+
# self.create_or_update_from_values
|
9
|
+
extend TextUtils::ValueHelper # e.g. is_year?, is_region?, is_address?, is_taglist? etc.
|
10
|
+
|
7
11
|
self.table_name = 'breweries'
|
8
12
|
|
9
13
|
belongs_to :country, :class_name => 'WorldDb::Models::Country', :foreign_key => 'country_id'
|
@@ -71,7 +75,7 @@ class Brewery < ActiveRecord::Base
|
|
71
75
|
value_region_key = value[7..-1] ## cut off region: prefix
|
72
76
|
value_region = Region.find_by_key_and_country_id!( value_region_key, new_attributes[:country_id] )
|
73
77
|
new_attributes[ :region_id ] = value_region.id
|
74
|
-
elsif value
|
78
|
+
elsif is_region?( value ) ## assume region code e.g. TX or N
|
75
79
|
value_region = Region.find_by_key_and_country_id!( value.downcase, new_attributes[:country_id] )
|
76
80
|
new_attributes[ :region_id ] = value_region.id
|
77
81
|
elsif value =~ /^city:/ ## city:
|
@@ -89,21 +93,21 @@ class Brewery < ActiveRecord::Base
|
|
89
93
|
if value_city.present? && value_city.region.present?
|
90
94
|
new_attributes[ :region_id ] = value_city.region.id
|
91
95
|
end
|
92
|
-
elsif value
|
96
|
+
elsif is_year?( value ) # founded/established year e.g. 1776
|
93
97
|
new_attributes[ :since ] = value.to_i
|
94
98
|
elsif value =~ /^(?:([0-9][0-9_ ]+[0-9]|[0-9]{1,2})\s*hl)$/ # e.g. 20_000 hl or 50hl etc.
|
95
99
|
value_prod = $1.gsub( /[ _]/, '' ).to_i
|
96
100
|
new_attributes[ :prod ] = value_prod
|
97
|
-
elsif value
|
101
|
+
elsif is_website?( value ) # check for url/internet address e.g. www.ottakringer.at
|
98
102
|
# fix: support more url format (e.g. w/o www. - look for .com .country code etc.)
|
99
103
|
new_attributes[ :web ] = value
|
100
|
-
elsif value
|
101
|
-
new_attributes[ :address ] = normalize_address( value )
|
104
|
+
elsif is_address?( value ) # if value includes // assume address e.g. 3970 Weitra // Sparkasseplatz 160
|
105
|
+
new_attributes[ :address ] = TextUtils.normalize_address( value )
|
102
106
|
elsif value =~ /^brands:/ # brands:
|
103
107
|
value_brands = value[7..-1] ## cut off brands: prefix
|
104
108
|
value_brands = value_brands.strip # remove leading and trailing spaces
|
105
109
|
# NB: brands get processed after record gets created (see below)
|
106
|
-
elsif (values.size==(index+1)) && value
|
110
|
+
elsif (values.size==(index+1)) && is_taglist?( value )) # tags must be last entry
|
107
111
|
|
108
112
|
logger.debug " found tags: >>#{value}<<"
|
109
113
|
|
@@ -144,23 +148,23 @@ class Brewery < ActiveRecord::Base
|
|
144
148
|
logger.debug " auto-adding brands >#{value_brands}<"
|
145
149
|
|
146
150
|
# remove optional english translation in square brackets ([]) e.g. Wien [Vienna]
|
147
|
-
value_brands =
|
151
|
+
value_brands = TextUtils.strip_translations( value_brands )
|
148
152
|
|
149
153
|
# remove optional longer title part in () e.g. Las Palmas (de Gran Canaria), Palma (de Mallorca)
|
150
|
-
value_brands =
|
151
|
-
|
154
|
+
value_brands = TextUtils.strip_subtitles( value_brands )
|
155
|
+
|
152
156
|
# remove optional longer title part in {} e.g. Ottakringer {Bio} or {Alkoholfrei}
|
153
|
-
value_brands =
|
154
|
-
|
157
|
+
value_brands = TextUtils.strip_tags( value_brands )
|
158
|
+
|
155
159
|
value_brand_titles = value_brands.split( ',' )
|
156
|
-
|
160
|
+
|
157
161
|
# pass 1) remove leading n trailing spaces
|
158
162
|
value_brand_titles = value_brand_titles.map { |value| value.strip }
|
159
|
-
|
163
|
+
|
160
164
|
value_brand_titles.each do |brand_title|
|
161
165
|
|
162
166
|
# autogenerate key from title
|
163
|
-
brand_key = title_to_key( brand_title )
|
167
|
+
brand_key = TextUtils.title_to_key( brand_title )
|
164
168
|
|
165
169
|
brand = Brand.find_by_key( brand_key )
|
166
170
|
|
@@ -209,128 +213,6 @@ class Brewery < ActiveRecord::Base
|
|
209
213
|
end # method create_or_update_from_values
|
210
214
|
|
211
215
|
|
212
|
-
### todo/fix:
|
213
|
-
# reuse method - put into helper in textutils or somewhere else ??
|
214
|
-
|
215
|
-
### todo/fix - move to textutils !!!!! AddressHelper
|
216
|
-
|
217
|
-
def self.normalize_address( old_address_line )
|
218
|
-
# for now only checks german 5-digit zip code
|
219
|
-
#
|
220
|
-
# e.g. Alte Plauener Straße 24 // 95028 Hof becomes
|
221
|
-
# 95028 Hof // Alte Plauener Straße 24
|
222
|
-
|
223
|
-
new_address_line = old_address_line # default - do nothing - just path through
|
224
|
-
|
225
|
-
lines = old_address_line.split( '//' )
|
226
|
-
|
227
|
-
if lines.size == 2 # two lines / check for switching lines
|
228
|
-
line1 = lines[0].strip
|
229
|
-
line2 = lines[1].strip
|
230
|
-
if line2 =~ /^[0-9]{5}\s/
|
231
|
-
new_address_line = "#{line2} // #{line1}" # swap - let line w/ 5-digit zip code go first
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
new_address_line
|
236
|
-
end
|
237
|
-
|
238
|
-
### todo/fix: move to textutils!!!
|
239
|
-
## add options for
|
240
|
-
## - remove translations e.g. []
|
241
|
-
## - remove subtitles e.g. ()
|
242
|
-
## - remove tags/extras e.g. {}
|
243
|
-
|
244
|
-
##
|
245
|
-
## fix: make strip_translations into fn
|
246
|
-
## make strip_subtitles into fn # better name for () strip_
|
247
|
-
## make strip_desc / tags (find better name strip_extras, strip_meta? strip_desc? strip_curly?)
|
248
|
-
|
249
|
-
|
250
|
-
def self.title_to_key( title )
|
251
|
-
|
252
|
-
## NB: downcase does NOT work for accented chars (thus, include in alternatives)
|
253
|
-
key = title.downcase
|
254
|
-
|
255
|
-
## remove all whitespace and punctuation
|
256
|
-
key = key.gsub( /[ \t_\-\.()\[\]'"\/]/, '' )
|
257
|
-
|
258
|
-
## remove special chars (e.g. %°&)
|
259
|
-
key = key.gsub( /[%&°]/, '' )
|
260
|
-
|
261
|
-
## turn accented char into ascii look alike if possible
|
262
|
-
##
|
263
|
-
## todo: add some more
|
264
|
-
## see http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references for more
|
265
|
-
|
266
|
-
## todo: add unicode codepoint name
|
267
|
-
|
268
|
-
alternatives = [
|
269
|
-
['ß', 'ss'],
|
270
|
-
['æ', 'ae'],
|
271
|
-
['ä', 'ae'],
|
272
|
-
['ā', 'a' ], # e.g. Liepājas
|
273
|
-
['á', 'a' ], # e.g. Bogotá, Králové
|
274
|
-
['ã', 'a' ], # e.g São Paulo
|
275
|
-
['ă', 'a' ], # e.g. Chișinău
|
276
|
-
['â', 'a' ], # e.g Goiânia
|
277
|
-
['å', 'a' ], # e.g. Vålerenga
|
278
|
-
['ą', 'a' ], # e.g. Śląsk
|
279
|
-
['ç', 'c' ], # e.g. São Gonçalo, Iguaçu, Neftçi
|
280
|
-
['ć', 'c' ], # e.g. Budućnost
|
281
|
-
['č', 'c' ], # e.g. Tradiční, Výčepní
|
282
|
-
['é', 'e' ], # e.g. Vélez, Králové
|
283
|
-
['è', 'e' ], # e.g. Rivières
|
284
|
-
['ê', 'e' ], # e.g. Grêmio
|
285
|
-
['ě', 'e' ], # e.g. Budějovice
|
286
|
-
['ĕ', 'e' ], # e.g. Svĕtlý
|
287
|
-
['ė', 'e' ], # e.g. Vėtra
|
288
|
-
['ë', 'e' ], # e.g. Skënderbeu
|
289
|
-
['ğ', 'g' ], # e.g. Qarabağ
|
290
|
-
['ì', 'i' ], # e.g. Potosì
|
291
|
-
['í', 'i' ], # e.g. Ústí
|
292
|
-
['ł', 'l' ], # e.g. Wisła, Wrocław
|
293
|
-
['ñ', 'n' ], # e.g. Porteño
|
294
|
-
['ň', 'n' ], # e.g. Plzeň, Třeboň
|
295
|
-
['ö', 'oe'],
|
296
|
-
['ő', 'o' ], # e.g. Győri
|
297
|
-
['ó', 'o' ], # e.g. Colón, Łódź, Kraków
|
298
|
-
['õ', 'o' ], # e.g. Nõmme
|
299
|
-
['ø', 'o' ], # e.g. Fuglafjørdur, København
|
300
|
-
['ř', 'r' ], # e.g. Třeboň
|
301
|
-
['ș', 's' ], # e.g. Chișinău, București
|
302
|
-
['ş', 's' ], # e.g. Beşiktaş
|
303
|
-
['š', 's' ], # e.g. Košice
|
304
|
-
['ť', 't' ], # e.g. Měšťan
|
305
|
-
['ü', 'ue'],
|
306
|
-
['ú', 'u' ], # e.g. Fútbol
|
307
|
-
['ū', 'u' ], # e.g. Sūduva
|
308
|
-
['ů', 'u' ], # e.g. Sládkův
|
309
|
-
['ı', 'u' ], # e.g. Bakı # use u?? (Baku) why-why not?
|
310
|
-
['ý', 'y' ], # e.g. Nefitrovaný
|
311
|
-
['ź', 'z' ], # e.g. Łódź
|
312
|
-
['ž', 'z' ], # e.g. Domžale, Petržalka
|
313
|
-
|
314
|
-
['Č', 'c' ], # e.g. České
|
315
|
-
['İ', 'i' ], # e.g. İnter
|
316
|
-
['Í', 'i' ], # e.g. ÍBV
|
317
|
-
['Ł', 'l' ], # e.g. Łódź
|
318
|
-
['Ö', 'oe' ], # e.g. Örebro
|
319
|
-
['Ř', 'r' ], # e.g. Řezák
|
320
|
-
['Ś', 's' ], # e.g. Śląsk
|
321
|
-
['Š', 's' ], # e.g. MŠK
|
322
|
-
['Ş', 's' ], # e.g. Şüvälan
|
323
|
-
['Ú', 'u' ], # e.g. Ústí, Újpest
|
324
|
-
['Ž', 'z' ] # e.g. Žilina
|
325
|
-
]
|
326
|
-
|
327
|
-
alternatives.each do |alt|
|
328
|
-
key = key.gsub( alt[0], alt[1] )
|
329
|
-
end
|
330
|
-
|
331
|
-
key
|
332
|
-
end # method title_to_key
|
333
|
-
|
334
216
|
end # class Brewery
|
335
217
|
|
336
218
|
|
data/lib/beerdb/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: beerdb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &81886320 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.2'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *81886320
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: worlddb
|
27
|
-
requirement: &
|
27
|
+
requirement: &81886100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.6'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *81886100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: commander
|
38
|
-
requirement: &
|
38
|
+
requirement: &81885880 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 4.1.3
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *81885880
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rdoc
|
49
|
-
requirement: &
|
49
|
+
requirement: &81902040 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '3.10'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *81902040
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: hoe
|
60
|
-
requirement: &
|
60
|
+
requirement: &81901820 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '3.3'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *81901820
|
69
69
|
description: beerdb - beer.db command line tool
|
70
70
|
email: beerdb@googlegroups.com
|
71
71
|
executables:
|