worlddb 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/db/america/ca/regions.txt +1 -1
- data/db/america/countries.txt +1 -1
- data/db/europe/countries.txt +1 -1
- data/db/europe/fr/cities.txt +1 -1
- data/lib/worlddb/models/region.rb +1 -0
- data/lib/worlddb/reader.rb +48 -6
- data/lib/worlddb/schema.rb +10 -1
- data/lib/worlddb/version.rb +1 -1
- metadata +3 -3
data/db/america/ca/regions.txt
CHANGED
@@ -21,7 +21,7 @@ pe, Prince Edward Island, 5686, eastern canada|atlantic canada|maritimes
|
|
21
21
|
nl, Newfoundland and Labrador, 370511, eastern canada|atlantic canada
|
22
22
|
|
23
23
|
on, Ontario, 908608, eastern canada|central canada
|
24
|
-
qc, Québec
|
24
|
+
qc, Québec, 1356547, eastern canada|central canada
|
25
25
|
|
26
26
|
nt, Northwest Territories, 1143793, northern canada|territories
|
27
27
|
yt, Yukon, 474713, northern canada|territories
|
data/db/america/countries.txt
CHANGED
@@ -22,7 +22,7 @@ sv, El Salvador, SLV
|
|
22
22
|
|
23
23
|
ar, Argentina, ARG, 2780400, 40518425, south america|es
|
24
24
|
bo, Bolivia, BOL, 1098581, 10907778, south america|es
|
25
|
-
br, Brazil, BRA, 8514215, 192380000, south america|pt
|
25
|
+
br, Brazil, BRA, 8514215, 192380000, south america|pt
|
26
26
|
cl, Chile, CHI, 755696, 16763470, south america|es
|
27
27
|
co, Colombia, COL, 1138748, 46413791, south america|es
|
28
28
|
ec, Ecuador, ECU, 258238, 15007343, south america|es
|
data/db/europe/countries.txt
CHANGED
@@ -70,7 +70,7 @@ ad, Andorra, AND, 468, 85_082
|
|
70
70
|
al, Albania, ALB, 28_748, 3_002_859
|
71
71
|
by, Belarus, BLR, 207_600, 9_643_566
|
72
72
|
ch, Switzerland, SUI, 41_285, 7_866_500, schengen|uefa|un # NB: ISO (CHE) <> FIFA (SUI)
|
73
|
-
hr, Croatia, CRO,
|
73
|
+
hr, Croatia, CRO, 56_594, 4_290_612 # NB: ISO (HRV) <> FIFA (CRO); local name: Hrvatska
|
74
74
|
rs, Serbia, SRB, 88_361, 7_276_604
|
75
75
|
ru, Russia, RUS, 17_098_242, 142_517_670, asia|g8|g20
|
76
76
|
tr, Turkey, TUR, 783_562, 79_749_461, asia
|
data/db/europe/fr/cities.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# top 10 cities
|
2
2
|
|
3
3
|
paris, Paris, region:if, 2_234_105, m:10_755_000
|
4
|
-
marseille, Marseille
|
4
|
+
marseille, Marseille, region:ac, 850_602, m: 1_582_000
|
5
5
|
lyon, Lyon, region:ra, 479_803, m: 1_542_000
|
6
6
|
toulouse, Toulouse, region:mp, 440_204, m: 880_000
|
7
7
|
nice, Nice, region:ac, 340_735, m: 962_000
|
@@ -10,6 +10,7 @@ class Region < ActiveRecord::Base
|
|
10
10
|
has_many :tags, :through => :taggings
|
11
11
|
|
12
12
|
validates :key, :format => { :with => /^[a-z]{2,}$/, :message => 'expected two or more lowercase letters a-z' }
|
13
|
+
validates :code, :format => { :with => /^[A-Z_]{2,3}$/, :message => 'expected two or three uppercase letters A-Z (and _)' }, :allow_nil => true
|
13
14
|
|
14
15
|
def self.create_from_ary!( regions, more_values={} )
|
15
16
|
regions.each do |values|
|
data/lib/worlddb/reader.rb
CHANGED
@@ -140,6 +140,11 @@ private
|
|
140
140
|
reader.each_line do |attribs, values|
|
141
141
|
|
142
142
|
value_numbers = []
|
143
|
+
|
144
|
+
### todo: remove attribs[:tags] from attribs? lets us pass in "default" tags - why? why not?
|
145
|
+
##
|
146
|
+
|
147
|
+
value_tags = []
|
143
148
|
|
144
149
|
if clazz == City
|
145
150
|
attribs[ :c ] = true # assume city type by default (use metro,district to change in fixture)
|
@@ -151,7 +156,8 @@ private
|
|
151
156
|
values.each_with_index do |value,index|
|
152
157
|
if value =~ /^region:/ ## region:
|
153
158
|
value_region_key = value[7..-1] ## cut off region: prefix
|
154
|
-
|
159
|
+
## NB: requires country_id to make unique!
|
160
|
+
value_region = Region.find_by_key_and_country_id!( value_region_key, attribs[:country_id] )
|
155
161
|
attribs[ :region_id ] = value_region.id
|
156
162
|
elsif value =~ /^metro$/ ## metro(politan area)
|
157
163
|
attribs[ :c ] = false # turn off default c|city flag; make it m|metro only
|
@@ -184,15 +190,35 @@ private
|
|
184
190
|
value_popm = value_popm_str.gsub(/[ _]/, '').to_i
|
185
191
|
attribs[ :popm ] = value_popm
|
186
192
|
attribs[ :m ] = true # auto-mark city as m|metro too
|
187
|
-
elsif value =~ /^[A-Z]{3}$/ ## assume three-letter code
|
193
|
+
elsif value =~ /^[A-Z]{2,3}$/ ## assume two or three-letter code
|
188
194
|
attribs[ :code ] = value
|
189
195
|
elsif value =~ /(^[0-9]{1,2}$)|(^[0-9][0-9 _]+[0-9]$)/ ## numeric (nb: can use any _ or spaces inside digits e.g. 1_000_000 or 1 000 000)
|
190
196
|
value_numbers << value.gsub(/[ _]/, '').to_i
|
191
|
-
elsif (values.size==(index+
|
192
|
-
|
197
|
+
elsif (values.size==(index+1)) && value =~ /^[a-z0-9\|_ ]+$/ # tags must be last entry
|
198
|
+
|
199
|
+
puts " processing tags: >>#{value}<<..."
|
200
|
+
|
201
|
+
tag_keys = value.split('|')
|
202
|
+
|
203
|
+
## unify; replace _w/ space; remove leading n trailing whitespace
|
204
|
+
tag_keys = tag_keys.map do |key|
|
205
|
+
key = key.gsub( '_', ' ' )
|
206
|
+
key = key.strip
|
207
|
+
key
|
208
|
+
end
|
209
|
+
|
210
|
+
tag_keys.each do |key|
|
211
|
+
tag = Tag.find_by_key( key )
|
212
|
+
if tag.nil? # create tag if it doesn't exit
|
213
|
+
puts " creating tag >#{key}<"
|
214
|
+
tag = Tag.create!( key: key )
|
215
|
+
end
|
216
|
+
value_tags << tag
|
217
|
+
end
|
218
|
+
|
193
219
|
else
|
194
220
|
# issue warning: unknown type for value
|
195
|
-
puts "
|
221
|
+
puts "!!!! >>>> warning: unknown type for value >#{value}<"
|
196
222
|
end
|
197
223
|
end
|
198
224
|
|
@@ -206,7 +232,15 @@ private
|
|
206
232
|
end
|
207
233
|
end
|
208
234
|
|
209
|
-
rec =
|
235
|
+
rec = nil
|
236
|
+
|
237
|
+
if clazz == Region ## requires country_id
|
238
|
+
## todo: assert that country_id is present/valid, that is, NOT null
|
239
|
+
rec = clazz.find_by_key_and_country_id( attribs[ :key ], attribs[ :country_id] )
|
240
|
+
else
|
241
|
+
rec = clazz.find_by_key( attribs[ :key ] )
|
242
|
+
end
|
243
|
+
|
210
244
|
if rec.present?
|
211
245
|
## nb: [17..-1] cut off WorldDB::Models:: in name
|
212
246
|
puts "*** update #{clazz.name[17..-1].downcase} #{rec.id}-#{rec.key}:"
|
@@ -218,6 +252,14 @@ private
|
|
218
252
|
puts attribs.to_json
|
219
253
|
|
220
254
|
rec.update_attributes!( attribs )
|
255
|
+
|
256
|
+
## add tags
|
257
|
+
|
258
|
+
value_tags.each do |tag|
|
259
|
+
rec.tags << tag
|
260
|
+
end
|
261
|
+
|
262
|
+
### fix/todo: check tag_ids and only update diff (add/remove ids)
|
221
263
|
|
222
264
|
end # each_line
|
223
265
|
|
data/lib/worlddb/schema.rb
CHANGED
@@ -27,10 +27,14 @@ create_table :countries do |t|
|
|
27
27
|
t.string :motor # optional auto motor (vehicle) licene plate
|
28
28
|
end
|
29
29
|
|
30
|
+
add_index :countries, :key, :unique => true
|
31
|
+
add_index :countries, :code, :unique => true
|
32
|
+
|
33
|
+
|
30
34
|
create_table :regions do |t|
|
31
35
|
t.string :title, :null => false
|
32
36
|
t.string :key, :null => false
|
33
|
-
t.string :code # short three letter code
|
37
|
+
t.string :code # short two or three letter code e.g. NY, OAX, etc.
|
34
38
|
t.string :synonyms # comma separated list of synonyms
|
35
39
|
t.references :country, :null => false
|
36
40
|
t.integer :pop # optional population count
|
@@ -38,6 +42,9 @@ create_table :regions do |t|
|
|
38
42
|
t.timestamps
|
39
43
|
end
|
40
44
|
|
45
|
+
add_index :regions, [:key, :country_id], :unique => true
|
46
|
+
|
47
|
+
|
41
48
|
create_table :cities do |t|
|
42
49
|
t.string :title, :null => false
|
43
50
|
t.string :key, :null => false
|
@@ -66,6 +73,8 @@ create_table :tags do |t|
|
|
66
73
|
t.timestamps
|
67
74
|
end
|
68
75
|
|
76
|
+
add_index :tags, :key, :unique => true
|
77
|
+
|
69
78
|
create_table :taggings do |t|
|
70
79
|
t.references :tag, :null => false
|
71
80
|
t.references :taggable, :polymorphic => true
|
data/lib/worlddb/version.rb
CHANGED
metadata
CHANGED