worlddb 2.0.7 → 2.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +1 -0
- data/lib/worlddb.rb +1 -0
- data/lib/worlddb/deleter.rb +1 -0
- data/lib/worlddb/models/country.rb +50 -0
- data/lib/worlddb/models/country_code.rb +41 -0
- data/lib/worlddb/models/forward.rb +12 -8
- data/lib/worlddb/reader.rb +2 -39
- data/lib/worlddb/schema.rb +14 -0
- data/lib/worlddb/stats.rb +7 -3
- data/lib/worlddb/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7c9137d81e29c751381a6a489626e295cea4619
|
4
|
+
data.tar.gz: c8fb56e5b4f5bcf7e8364c67c4f795a01d464053
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e338f8332bad32c4e567f4fe7c754e9f90a0ee0fa3d790b9ad59ca856dfcb28d6e2a0bcad0c74ed8ab10245b498b4d4d70dfb17ce971288a717b8573e417b065
|
7
|
+
data.tar.gz: 86ad1f3b50b857a9540320b339a1ef4ee4bcebd2c2d9aa9ff3b71384e638c748003cfba65cf9f7e042dc23c8c8cacd71b9aecab7d9e70320ab92159bfb23d881
|
data/Manifest.txt
CHANGED
@@ -14,6 +14,7 @@ lib/worlddb/models/city_comp.rb
|
|
14
14
|
lib/worlddb/models/continent.rb
|
15
15
|
lib/worlddb/models/continent_comp.rb
|
16
16
|
lib/worlddb/models/country.rb
|
17
|
+
lib/worlddb/models/country_code.rb
|
17
18
|
lib/worlddb/models/country_comp.rb
|
18
19
|
lib/worlddb/models/forward.rb
|
19
20
|
lib/worlddb/models/lang.rb
|
data/lib/worlddb.rb
CHANGED
@@ -41,6 +41,7 @@ require 'worlddb/models/continent'
|
|
41
41
|
require 'worlddb/models/continent_comp'
|
42
42
|
require 'worlddb/models/country'
|
43
43
|
require 'worlddb/models/country_comp'
|
44
|
+
require 'worlddb/models/country_code'
|
44
45
|
require 'worlddb/models/region'
|
45
46
|
require 'worlddb/models/region_comp'
|
46
47
|
require 'worlddb/models/city'
|
data/lib/worlddb/deleter.rb
CHANGED
@@ -81,17 +81,23 @@ class Country < ActiveRecord::Base
|
|
81
81
|
'SUPR'
|
82
82
|
elsif is_dependency?
|
83
83
|
'TERR'
|
84
|
+
elsif is_misc? ## misc(ellaneous) country or dependent territory
|
85
|
+
# todo: use different marker?
|
86
|
+
# territory w/ shared or disputes claims e.g Antartica/Western Sahara/Paracel Islands pg Spratly Islands/etc.
|
87
|
+
'MISC'
|
84
88
|
else
|
85
89
|
'CNTY'
|
86
90
|
end
|
87
91
|
end
|
88
92
|
|
93
|
+
|
89
94
|
###
|
90
95
|
# NB: use is_ for flags to avoid conflict w/ assocs
|
91
96
|
|
92
97
|
def is_supra?() s? == true; end
|
93
98
|
def is_country?() c? == true; end
|
94
99
|
def is_dependency?() d? == true; end
|
100
|
+
def is_misc?() m? == true; end
|
95
101
|
|
96
102
|
|
97
103
|
def all_names( opts={} )
|
@@ -114,6 +120,50 @@ class Country < ActiveRecord::Base
|
|
114
120
|
end
|
115
121
|
|
116
122
|
|
123
|
+
def self.search_by_name( q ) ## todo/check: just use search (rename)? why? why not?
|
124
|
+
|
125
|
+
## fix: add/configure logger for ActiveRecord!!!
|
126
|
+
## logger = LogKernel::Logger.root
|
127
|
+
|
128
|
+
name = q.strip
|
129
|
+
|
130
|
+
## 1) first try 1:1 (exact) match
|
131
|
+
cty = Country.find_by_name( name ) # NOTE: assume AR escapes quotes in name ??
|
132
|
+
if cty.nil?
|
133
|
+
## 2) retry: a) remove all (..) enclosed
|
134
|
+
## b) remove all extra spaces (e.g. Cocos (Keeling) Islands => Cocos__Islands => Cocos_Islands)
|
135
|
+
name = name.gsub( /\([^)]+\)/, '' ).strip
|
136
|
+
name = name.gsub( /[ \t]{2,}/, ' ' )
|
137
|
+
cty = Country.find_by_name( name )
|
138
|
+
|
139
|
+
### NOTE: escape ' for sql like clause
|
140
|
+
## for now use '' for escapes, that is, double quotes
|
141
|
+
## check - working for postgresql n sqlite??
|
142
|
+
name_esc = name.gsub( /'/, "''" )
|
143
|
+
|
144
|
+
## 3) retry: use SQL like match
|
145
|
+
## % is used to match *zero* or more occurrences of any characters
|
146
|
+
## todo: check if it matches zero too
|
147
|
+
if cty.nil?
|
148
|
+
cty = Country.where( "name LIKE '%#{name_esc}%'" ).first
|
149
|
+
end
|
150
|
+
|
151
|
+
## 4) retry: use SQL like match for alternative names match
|
152
|
+
if cty.nil?
|
153
|
+
cty = Country.where( "alt_names LIKE '%#{name_esc}%'" ).first
|
154
|
+
end
|
155
|
+
|
156
|
+
## 5) retry: use SQL like match for historic names match (e.g. Burma for Myanmar etc.)
|
157
|
+
## todo/check: make it optional (pass in opts hash to configure) - why? why not???
|
158
|
+
if cty.nil?
|
159
|
+
cty = Country.where( "hist_names LIKE '%#{name_esc}%'" ).first
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
cty # return cty (country); nil if not found
|
164
|
+
end
|
165
|
+
|
166
|
+
|
117
167
|
def self.create_or_update_from_values( values, more_attribs={} )
|
118
168
|
|
119
169
|
## key & title
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module WorldDb
|
4
|
+
module Model
|
5
|
+
|
6
|
+
class CountryCode < ActiveRecord::Base
|
7
|
+
|
8
|
+
self.table_name = 'country_codes'
|
9
|
+
|
10
|
+
belongs_to :country, class_name: 'Country', foreign_key: 'country_id'
|
11
|
+
|
12
|
+
|
13
|
+
scope :by_name, ->{ order( 'name asc' ) } # order by name (a-z)
|
14
|
+
|
15
|
+
|
16
|
+
def self.update!
|
17
|
+
## update (auto-create) country codes from existing countries in db
|
18
|
+
|
19
|
+
## fix: add/configure logger for ActiveRecord!!!
|
20
|
+
logger = LogKernel::Logger.root
|
21
|
+
|
22
|
+
logger.debug( "delete all (old) country codes" )
|
23
|
+
CountryCode.delete_all
|
24
|
+
|
25
|
+
Country.order(:id).each do |cty|
|
26
|
+
logger.debug( "add country #{cty.key} #{cty.name}" )
|
27
|
+
CountryCode.create!( country_id: cty.id, kind: 'NET', name: cty.net ) unless cty.net.nil?
|
28
|
+
CountryCode.create!( country_id: cty.id, kind: 'NUM', name: cty.num ) unless cty.num.nil?
|
29
|
+
CountryCode.create!( country_id: cty.id, kind: 'A2', name: cty.alpha2 ) unless cty.alpha2.nil?
|
30
|
+
CountryCode.create!( country_id: cty.id, kind: 'A3', name: cty.alpha3 ) unless cty.alpha3.nil?
|
31
|
+
CountryCode.create!( country_id: cty.id, kind: 'FIFA', name: cty.fifa ) unless cty.fifa.nil?
|
32
|
+
CountryCode.create!( country_id: cty.id, kind: 'IOC', name: cty.ioc ) unless cty.ioc.nil?
|
33
|
+
CountryCode.create!( country_id: cty.id, kind: 'FIPS', name: cty.fips ) unless cty.fips.nil?
|
34
|
+
CountryCode.create!( country_id: cty.id, kind: 'M', name: cty.motor ) unless cty.motor.nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end # class CountryCode
|
39
|
+
|
40
|
+
end # module Model
|
41
|
+
end # module WorldDb
|
@@ -25,6 +25,8 @@ class City < ActiveRecord::Base ; end
|
|
25
25
|
class Lang < ActiveRecord::Base ; end
|
26
26
|
class Usage < ActiveRecord::Base ; end
|
27
27
|
|
28
|
+
class CountryCode < ActiveRecord::Base ; end
|
29
|
+
|
28
30
|
end
|
29
31
|
|
30
32
|
# note: convenience alias for Model
|
@@ -39,15 +41,17 @@ module TagDb
|
|
39
41
|
# add alias? why? why not? # is there a better way?
|
40
42
|
# - just include WorldDb::Models - why? why not?
|
41
43
|
|
42
|
-
Name
|
43
|
-
Place
|
44
|
-
Continent
|
45
|
-
Country
|
46
|
-
Region
|
47
|
-
City
|
44
|
+
Name = WorldDb::Model::Name
|
45
|
+
Place = WorldDb::Model::Place
|
46
|
+
Continent = WorldDb::Model::Continent
|
47
|
+
Country = WorldDb::Model::Country
|
48
|
+
Region = WorldDb::Model::Region
|
49
|
+
City = WorldDb::Model::City
|
50
|
+
|
51
|
+
Lang = WorldDb::Model::Lang
|
52
|
+
Usage = WorldDb::Model::Usage
|
48
53
|
|
49
|
-
|
50
|
-
Usage = WorldDb::Model::Usage
|
54
|
+
CountryCode = WorldDb::Model::CountryCode
|
51
55
|
|
52
56
|
end
|
53
57
|
end
|
data/lib/worlddb/reader.rb
CHANGED
@@ -170,7 +170,7 @@ class ReaderBase
|
|
170
170
|
|
171
171
|
values = line.split(',')
|
172
172
|
|
173
|
-
logger.debug '[>' + values.join( '<|>' ) + '<]'
|
173
|
+
## logger.debug '[>' + values.join( '<|>' ) + '<]'
|
174
174
|
|
175
175
|
if name =~ /iso/
|
176
176
|
# special case for iso
|
@@ -182,7 +182,7 @@ class ReaderBase
|
|
182
182
|
end
|
183
183
|
|
184
184
|
## try to find country
|
185
|
-
cty =
|
185
|
+
cty = Country.search_by_name( country_name )
|
186
186
|
|
187
187
|
if cty.nil?
|
188
188
|
logger.warn "no country match found for >#{country_name}<; skipping line; in [#{name}]"
|
@@ -217,45 +217,8 @@ class ReaderBase
|
|
217
217
|
|
218
218
|
####
|
219
219
|
# helper methods
|
220
|
-
##
|
221
|
-
## todo: move to country model !!!!!!
|
222
|
-
## find a good name Country.find not really possible
|
223
|
-
## superfind ?? use search!!! search_by_name() Country.search() or lookup?
|
224
|
-
##
|
225
220
|
## todo: also add City.search_by_name etc. !!!
|
226
221
|
|
227
|
-
def find_country( country_name )
|
228
|
-
|
229
|
-
name = country_name.strip
|
230
|
-
|
231
|
-
## 1) first try 1:1 (exact) match
|
232
|
-
cty = Country.find_by_name( name ) # NOTE: assume AR escapes quotes in name ??
|
233
|
-
if cty.nil?
|
234
|
-
## 2) retry: remove all () enclosed
|
235
|
-
name = name.gsub( /\([^)]+\)/, '' ).strip
|
236
|
-
cty = Country.find_by_name( name )
|
237
|
-
|
238
|
-
### NOTE: escape ' for sql like clause
|
239
|
-
## for now use '' for escapes, that is, double quotes
|
240
|
-
## check - working for postgresql n sqlite??
|
241
|
-
name_esc = name.gsub( /'/, "''" )
|
242
|
-
|
243
|
-
## 3) retry: use SQL like match
|
244
|
-
## % is used to match *zero* or more occurrences of any characters
|
245
|
-
## todo: check if it matches zero too
|
246
|
-
if cty.nil?
|
247
|
-
cty = Country.where( "name LIKE '%#{name_esc}%'" ).first
|
248
|
-
end
|
249
|
-
|
250
|
-
## 4) retry: use SQL like match for alternative names match
|
251
|
-
if cty.nil?
|
252
|
-
cty = Country.where( "alt_names LIKE '%#{name_esc}%'" ).first
|
253
|
-
end
|
254
|
-
end
|
255
|
-
cty # return cty (country); nil if not found
|
256
|
-
end # method find_country
|
257
|
-
|
258
|
-
|
259
222
|
|
260
223
|
end # class ReaderBase
|
261
224
|
end # module WorldDb
|
data/lib/worlddb/schema.rb
CHANGED
@@ -75,6 +75,15 @@ end
|
|
75
75
|
add_index :continents, :key, unique: true
|
76
76
|
|
77
77
|
|
78
|
+
create_table :country_codes do |t|
|
79
|
+
t.string :name, null: false
|
80
|
+
t.string :kind, null: false # e.g. ALPHA2,NUM,FIFA,IOC,FIPS,NET,etc.
|
81
|
+
t.references :country, null: false
|
82
|
+
end
|
83
|
+
|
84
|
+
add_index :country_codes, [:name, :kind], unique: true
|
85
|
+
|
86
|
+
|
78
87
|
create_table :countries do |t|
|
79
88
|
t.string :name, null: false
|
80
89
|
t.string :slug, null: false # auto-generate default
|
@@ -82,6 +91,7 @@ create_table :countries do |t|
|
|
82
91
|
t.references :place, null: false
|
83
92
|
t.string :code, null: false # short three letter code (FIFA country code e.g. ITA)
|
84
93
|
t.string :alt_names # comma separated list of alternate names (synonyms)
|
94
|
+
t.string :hist_names # comma separated list of historic names (no longer in use)
|
85
95
|
t.integer :pop, null: false # population count
|
86
96
|
t.integer :area, null: false # area in square km (sq. km)
|
87
97
|
t.references :continent
|
@@ -92,6 +102,10 @@ create_table :countries do |t|
|
|
92
102
|
t.boolean :c, null: false, default: false # country flag (is this needed?)
|
93
103
|
t.boolean :d, null: false, default: false # dependency flag
|
94
104
|
|
105
|
+
t.boolean :m, null: false, default: false # misc(ellaneous) flag
|
106
|
+
# misc use for territories w/ shared or disputed claims
|
107
|
+
# e.g. Antartica/Western Sahara/Paracel Islands/Spratly Islands/etc.
|
108
|
+
|
95
109
|
# extras - country codes
|
96
110
|
t.string :motor # optional motor (vehicle) licene plate code (bumper sticker)
|
97
111
|
t.string :alpha2 # optional iso two letter country code
|
data/lib/worlddb/stats.rb
CHANGED
@@ -8,17 +8,21 @@ module WorldDb
|
|
8
8
|
def tables
|
9
9
|
puts "Stats:"
|
10
10
|
puts " #{'%5d' % Continent.count} continents"
|
11
|
-
puts " #{'%5d' % Country.count} countries (#{Country.where(s: true).count} supras, #{Country.where(d:true).count} deps)"
|
11
|
+
puts " #{'%5d' % Country.count} countries (#{Country.where(s: true).count} supras, #{Country.where(d:true).count} deps), #{Country.where(m:true).count} miscs)"
|
12
12
|
puts " #{'%5d' % Region.count} regions"
|
13
13
|
puts " #{'%5d' % City.where(m: true).where(c: false).count} metros"
|
14
14
|
puts " #{'%5d' % City.where(c: true).count} cities (#{City.where(c: true).where(m: true).count} metros)"
|
15
15
|
puts " #{'%5d' % City.where(d: true).count} districts"
|
16
|
-
# puts " #{'%5d' % Tag.count} tags"
|
17
|
-
# puts " #{'%5d' % Tagging.count} taggings"
|
18
16
|
puts " #{'%5d' % Place.count} places"
|
19
17
|
puts " #{'%5d' % Name.count} names"
|
18
|
+
|
20
19
|
puts " #{'%5d' % Lang.count} langs"
|
21
20
|
puts " #{'%5d' % Usage.count} usages"
|
21
|
+
|
22
|
+
puts " #{'%5d' % CountryCode.count} (country) codes"
|
23
|
+
|
24
|
+
# puts " #{'%5d' % Tag.count} tags"
|
25
|
+
# puts " #{'%5d' % Tagging.count} taggings"
|
22
26
|
end
|
23
27
|
|
24
28
|
end # class Stats
|
data/lib/worlddb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: worlddb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: props
|
@@ -163,6 +163,7 @@ files:
|
|
163
163
|
- lib/worlddb/models/continent.rb
|
164
164
|
- lib/worlddb/models/continent_comp.rb
|
165
165
|
- lib/worlddb/models/country.rb
|
166
|
+
- lib/worlddb/models/country_code.rb
|
166
167
|
- lib/worlddb/models/country_comp.rb
|
167
168
|
- lib/worlddb/models/forward.rb
|
168
169
|
- lib/worlddb/models/lang.rb
|