sportdb-sync 1.0.1 → 1.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/Rakefile +3 -3
- data/lib/sportdb/sync.rb +1 -12
- data/lib/sportdb/sync/club.rb +4 -4
- data/lib/sportdb/sync/event.rb +2 -2
- data/lib/sportdb/sync/league.rb +1 -4
- data/lib/sportdb/sync/season.rb +1 -1
- data/lib/sportdb/sync/sync.rb +94 -30
- data/lib/sportdb/sync/version.rb +2 -2
- data/test/helper.rb +1 -1
- data/test/test_league.rb +6 -6
- data/test/test_misc.rb +3 -3
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdadfa4b00e61764c6b7b1f62ffb983f89c4a1ff
|
4
|
+
data.tar.gz: 3721087e4b6ff6bf0e604a00cce8658df1cb05bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18c37a874743c779fabc931f8e09c14a30e3e13609242a82cc0d2d5f14319092d103f470a8541eabfef95b0f83c0c98849b54d652fc3f9ff5615299be8e7bc17
|
7
|
+
data.tar.gz: 2dd81facb4bbcd8a36c09d66d894288660724f058121799f5a6c29a64ece00c115eb6c257ef3075b2b8231a01e8e5a70afc6c4cb7f793e61752113acbf577344
|
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ Hoe.spec 'sportdb-sync' do
|
|
8
8
|
self.summary = "sportdb-sync - sport.db sync helpers for leagues, seasons, clubs, match schedules and results, and more"
|
9
9
|
self.description = summary
|
10
10
|
|
11
|
-
self.urls =
|
11
|
+
self.urls = { home: 'https://github.com/sportdb/sport.db' }
|
12
12
|
|
13
13
|
self.author = 'Gerald Bauer'
|
14
14
|
self.email = 'opensport@googlegroups.com'
|
@@ -20,8 +20,8 @@ Hoe.spec 'sportdb-sync' do
|
|
20
20
|
self.licenses = ['Public Domain']
|
21
21
|
|
22
22
|
self.extra_deps = [
|
23
|
-
['sportdb-
|
24
|
-
['sportdb-models',
|
23
|
+
['sportdb-catalogs', '>= 1.0.0'],
|
24
|
+
['sportdb-models', '>= 2.0.2'],
|
25
25
|
]
|
26
26
|
|
27
27
|
self.spec_extras = {
|
data/lib/sportdb/sync.rb
CHANGED
@@ -1,20 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
require 'sportdb/
|
3
|
+
require 'sportdb/catalogs'
|
4
4
|
require 'sportdb/models' ## add sql database support
|
5
5
|
|
6
6
|
|
7
|
-
##
|
8
|
-
# fix!!!
|
9
|
-
# patches for worlddb/models - move upstream!!!!
|
10
|
-
module WorldDb
|
11
|
-
COUNTRY_KEY_PATTERN = '\A[a-z]{2,}\z' # allow two AND three letter keys e.g. at, mx, eng, sco, etc.
|
12
|
-
COUNTRY_KEY_PATTERN_MESSAGE = "expected two or more lowercase letters a-z /#{COUNTRY_KEY_PATTERN}/"
|
13
|
-
|
14
|
-
COUNTRY_CODE_PATTERN = '\A[A-Z_]{2,}\z'
|
15
|
-
COUNTRY_CODE_PATTERN_MESSAGE = "expected two or more uppercase letters A-Z (and _) /#{COUNTRY_CODE_PATTERN}/"
|
16
|
-
end # module WorldDb
|
17
|
-
|
18
7
|
|
19
8
|
###
|
20
9
|
# our own code
|
data/lib/sportdb/sync/club.rb
CHANGED
@@ -50,8 +50,8 @@ module SportDb
|
|
50
50
|
# finders
|
51
51
|
|
52
52
|
def self.find_or_create( club )
|
53
|
-
## note: assume "canonical uniquie" names
|
54
|
-
rec = Model::Team.find_by(
|
53
|
+
## note: assume "canonical uniquie" names for now for clubs
|
54
|
+
rec = Model::Team.find_by( name: club.name )
|
55
55
|
if rec.nil?
|
56
56
|
|
57
57
|
## todo/fix: move auto-key gen to structs for re(use)!!!!!!
|
@@ -61,7 +61,7 @@ module SportDb
|
|
61
61
|
|
62
62
|
attribs = {
|
63
63
|
key: key,
|
64
|
-
|
64
|
+
name: club.name,
|
65
65
|
country_id: Sync::Country.find_or_create( club.country ).id,
|
66
66
|
club: true,
|
67
67
|
national: false ## check -is default anyway - use - why? why not?
|
@@ -71,7 +71,7 @@ module SportDb
|
|
71
71
|
attribs[:code] = club.code if club.code ## add code (abbreviation) if present
|
72
72
|
|
73
73
|
if club.alt_names.empty? == false
|
74
|
-
attribs[:
|
74
|
+
attribs[:alt_names] = club.alt_names.join('|')
|
75
75
|
end
|
76
76
|
|
77
77
|
rec = Model::Team.create!( attribs )
|
data/lib/sportdb/sync/event.rb
CHANGED
@@ -79,8 +79,8 @@ module SportDb
|
|
79
79
|
## otherwise use 2017, 7, 1
|
80
80
|
## start_at use year and 7,1 e.g. Date.new( 2017, 7, 1 )
|
81
81
|
## hack: fix/todo1!!
|
82
|
-
## add "fake"
|
83
|
-
attribs[:
|
82
|
+
## add "fake" start_date for now
|
83
|
+
attribs[:start_date] = if season.year? ## e.g. assume 2018 etc.
|
84
84
|
Date.new( season.start_year, 1, 1 )
|
85
85
|
else ## assume 2014/15 etc.
|
86
86
|
Date.new( season.start_year, 7, 1 )
|
data/lib/sportdb/sync/league.rb
CHANGED
@@ -40,11 +40,8 @@ module SportDb
|
|
40
40
|
def self.find_or_create( league )
|
41
41
|
rec = find( league )
|
42
42
|
if rec.nil?
|
43
|
-
## use title and not name - why? why not?
|
44
|
-
## quick fix: change name to title
|
45
|
-
|
46
43
|
attribs = { key: league.key,
|
47
|
-
|
44
|
+
name: league.name }
|
48
45
|
|
49
46
|
if league.country
|
50
47
|
attribs[ :country_id ] = Sync::Country.find_or_create( league.country ).id
|
data/lib/sportdb/sync/season.rb
CHANGED
data/lib/sportdb/sync/sync.rb
CHANGED
@@ -6,14 +6,14 @@ module SportDb
|
|
6
6
|
|
7
7
|
class NationalTeam
|
8
8
|
def self.find_or_create( team )
|
9
|
-
rec = Model::Team.find_by(
|
9
|
+
rec = Model::Team.find_by( name: team.name )
|
10
10
|
if rec.nil?
|
11
11
|
puts "add national team: #{team.key}, #{team.name}, #{team.country.name} (#{team.country.key})"
|
12
12
|
|
13
13
|
### note: key expected three or more lowercase letters a-z /\A[a-z]{3,}\z/
|
14
14
|
attribs = {
|
15
15
|
key: team.key, ## note: always use downcase fifa code for now!!!
|
16
|
-
|
16
|
+
name: team.name,
|
17
17
|
code: team.code,
|
18
18
|
country_id: Sync::Country.find_or_create( team.country ).id,
|
19
19
|
club: false,
|
@@ -21,7 +21,7 @@ module SportDb
|
|
21
21
|
}
|
22
22
|
|
23
23
|
if team.alt_names.empty? == false
|
24
|
-
attribs[:
|
24
|
+
attribs[:alt_names] = team.alt_names.join('|')
|
25
25
|
end
|
26
26
|
|
27
27
|
rec = Model::Team.create!( attribs )
|
@@ -64,13 +64,20 @@ module SportDb
|
|
64
64
|
|
65
65
|
class Round
|
66
66
|
def self.find_or_create( round, event: )
|
67
|
-
rec = Model::Round.find_by(
|
67
|
+
rec = Model::Round.find_by( name: round.name, event_id: event.id )
|
68
68
|
if rec.nil?
|
69
|
+
## find last pos - check if it can be nil?
|
70
|
+
max_pos = Model::Round.where( event_id: event.id ).maximum( 'pos' )
|
71
|
+
max_pos = max_pos ? max_pos+1 : 1
|
72
|
+
|
69
73
|
attribs = { event_id: event.id,
|
70
|
-
|
71
|
-
pos:
|
72
|
-
start_at: event.start_at.to_date
|
74
|
+
name: round.name,
|
75
|
+
pos: max_pos
|
73
76
|
}
|
77
|
+
|
78
|
+
## todo/fix: check if round has (optional) start or end date and add!!!
|
79
|
+
## attribs[ :start_date] = round.start_date.to_date
|
80
|
+
|
74
81
|
rec = Model::Round.create!( attribs )
|
75
82
|
end
|
76
83
|
rec
|
@@ -80,12 +87,18 @@ module SportDb
|
|
80
87
|
|
81
88
|
class Group
|
82
89
|
def self.find_or_create( group, event: )
|
83
|
-
rec = Model::Group.find_by(
|
90
|
+
rec = Model::Group.find_by( name: group.name, event_id: event.id )
|
84
91
|
if rec.nil?
|
92
|
+
## find last pos - check if it can be nil?
|
93
|
+
max_pos = Model::Group.where( event_id: event.id ).maximum( 'pos' )
|
94
|
+
max_pos = max_pos ? max_pos+1 : 1
|
95
|
+
|
85
96
|
attribs = { event_id: event.id,
|
86
|
-
|
87
|
-
pos:
|
97
|
+
name: group.name,
|
98
|
+
pos: max_pos
|
88
99
|
}
|
100
|
+
|
101
|
+
## todo/fix: check/add optional group key (was: pos before)!!!!
|
89
102
|
rec = Model::Group.create!( attribs )
|
90
103
|
end
|
91
104
|
## todo/fix: add/update teams in group too!!!!!
|
@@ -96,7 +109,7 @@ module SportDb
|
|
96
109
|
|
97
110
|
class Stage
|
98
111
|
def self.find( name, event: )
|
99
|
-
Model::Stage.find_by(
|
112
|
+
Model::Stage.find_by( name: name, event_id: event.id )
|
100
113
|
end
|
101
114
|
def self.find!( name, event: )
|
102
115
|
rec = find( name, event: event )
|
@@ -112,10 +125,8 @@ module SportDb
|
|
112
125
|
def self.find_or_create( name, event: )
|
113
126
|
rec = find( name, event: event )
|
114
127
|
if rec.nil?
|
115
|
-
## use title and not name - why? why not?
|
116
|
-
## quick fix: change name to title
|
117
128
|
attribs = { event_id: event.id,
|
118
|
-
|
129
|
+
name: name,
|
119
130
|
}
|
120
131
|
rec = Model::Stage.create!( attribs )
|
121
132
|
end
|
@@ -125,21 +136,38 @@ module SportDb
|
|
125
136
|
|
126
137
|
|
127
138
|
|
128
|
-
class Match
|
139
|
+
class Match
|
140
|
+
### todo/fix: rename to create!! (add update support later) !!!!
|
141
|
+
## use update_by_round or update_by_date or update_by_teams or such
|
142
|
+
## NO easy (unique always auto-id match) possible!!!!!!
|
129
143
|
def self.create_or_update( match, event: )
|
130
144
|
## note: MUST find round, thus, use bang (!)
|
131
145
|
|
132
146
|
## todo/check: allow strings too - why? why not?
|
133
147
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
148
|
+
|
149
|
+
## todo/check:
|
150
|
+
## how to model "same" rounds in different stages
|
151
|
+
## e.g. Belgium
|
152
|
+
## in regular season (stage) - round 1, round 2, etc.
|
153
|
+
## in playoff (stage) - round 1, round 2, etc.
|
154
|
+
## reference same round or create a new one for each stage!!???
|
155
|
+
## and lookup by name AND stage??
|
156
|
+
|
157
|
+
|
158
|
+
round_rec = if match.round
|
159
|
+
## query for round - allow string or round rec
|
160
|
+
round_name = match.round.is_a?( String ) ? match.round : match.round.name
|
161
|
+
Model::Round.find_by!( event_id: event.id,
|
162
|
+
name: round_name )
|
163
|
+
else # note: allow matches WITHOUT rounds too (e.g. England Football League 1888 and others)
|
164
|
+
nil
|
165
|
+
end
|
138
166
|
|
139
167
|
## todo/check: allow fallback with db lookup if NOT found in cache - why? why not?
|
140
168
|
## or better use Sync::Team.find_or_create( team ) !!!!!!! to auto-create on first hit!
|
141
169
|
## || Team.find_or_create( team1 ) -- note: does NOT work for string (only recs) - what to do?
|
142
|
-
## || Model::Team.find_by!(
|
170
|
+
## || Model::Team.find_by!( name: team1_name )
|
143
171
|
team1_name = match.team1.is_a?( String ) ? match.team1 : match.team1.name
|
144
172
|
team1_rec = Team.cache[ team1_name ]
|
145
173
|
team2_name = match.team2.is_a?( String ) ? match.team2 : match.team2.name
|
@@ -147,32 +175,68 @@ module SportDb
|
|
147
175
|
|
148
176
|
## check optional group (e.g. Group A, etc.)
|
149
177
|
group_rec = if match.group
|
150
|
-
|
178
|
+
group_name = match.group.is_a?( String ) ? match.group : match.group.name
|
151
179
|
Model::Group.find_by!( event_id: event.id,
|
152
|
-
|
180
|
+
name: group_name )
|
181
|
+
else
|
182
|
+
nil
|
183
|
+
end
|
184
|
+
|
185
|
+
## check optional stage (e.g. Regular, Play Off, Relegation, etc. )
|
186
|
+
stage_rec = if match.stage
|
187
|
+
stage_name = match.stage.is_a?( String ) ? match.stage : match.stage.name
|
188
|
+
Model::Stage.find_by!( event_id: event.id,
|
189
|
+
name: stage_name )
|
153
190
|
else
|
154
191
|
nil
|
155
192
|
end
|
156
193
|
|
194
|
+
### todo/check: what happens if there's more than one match? exception raised??
|
195
|
+
rec = if round_rec
|
196
|
+
## add match status too? allows [abandoned] and [replay] in same round
|
197
|
+
find_attributes = { round_id: round_rec.id,
|
198
|
+
team1_id: team1_rec.id,
|
199
|
+
team2_id: team2_rec.id }
|
200
|
+
|
201
|
+
## add stage if present to query
|
202
|
+
find_attributes[ :stage_id] = stage_rec.id if stage_rec
|
203
|
+
|
204
|
+
Model::Match.find_by( find_attributes )
|
205
|
+
else
|
206
|
+
## always assume new record for now
|
207
|
+
## check for date or such - why? why not?
|
208
|
+
nil
|
209
|
+
end
|
157
210
|
|
158
|
-
rec = Model::Game.find_by( round_id: round_rec.id,
|
159
|
-
team1_id: team1_rec.id,
|
160
|
-
team2_id: team2_rec.id )
|
161
211
|
if rec.nil?
|
162
|
-
|
212
|
+
## find last pos - check if it can be nil? yes, is nil if no records found
|
213
|
+
max_pos = Model::Match.where( event_id: event.id ).maximum( 'pos' )
|
214
|
+
max_pos = max_pos ? max_pos+1 : 1
|
215
|
+
|
216
|
+
attribs = { event_id: event.id, ## todo/fix: change to data struct too?
|
163
217
|
team1_id: team1_rec.id,
|
164
218
|
team2_id: team2_rec.id,
|
165
|
-
pos:
|
166
|
-
|
219
|
+
pos: max_pos,
|
220
|
+
date: match.date.to_date, ## todo/fix: split and add date & time!!!!
|
167
221
|
score1: match.score1,
|
168
222
|
score2: match.score2,
|
169
223
|
score1i: match.score1i,
|
170
|
-
score2i: match.score2i
|
224
|
+
score2i: match.score2i,
|
225
|
+
score1et: match.score1et,
|
226
|
+
score2et: match.score2et,
|
227
|
+
score1p: match.score1p,
|
228
|
+
score2p: match.score2p,
|
229
|
+
status: match.status }
|
230
|
+
|
231
|
+
attribs[ :round_id ] = round_rec.id if round_rec
|
171
232
|
attribs[ :group_id ] = group_rec.id if group_rec
|
233
|
+
attribs[ :stage_id ] = stage_rec.id if stage_rec
|
172
234
|
|
173
|
-
rec = Model::
|
235
|
+
rec = Model::Match.create!( attribs )
|
174
236
|
else
|
175
237
|
# update - todo
|
238
|
+
puts "!! ERROR - match updates not yet supported (only inserts); sorry"
|
239
|
+
exit 1
|
176
240
|
end
|
177
241
|
rec
|
178
242
|
end
|
data/lib/sportdb/sync/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
## note: use the local version of sportdb gems
|
2
2
|
$LOAD_PATH.unshift( File.expand_path( '../sportdb-formats/lib' ))
|
3
|
-
$LOAD_PATH.unshift( File.expand_path( '../sportdb-
|
3
|
+
$LOAD_PATH.unshift( File.expand_path( '../sportdb-catalogs/lib' ))
|
4
4
|
$LOAD_PATH.unshift( File.expand_path( '../sportdb-models/lib' ))
|
5
5
|
|
6
6
|
|
data/test/test_league.rb
CHANGED
@@ -14,25 +14,25 @@ class TestLeague < MiniTest::Test
|
|
14
14
|
|
15
15
|
def test_search
|
16
16
|
rec = League.search_or_create!( 'eng' )
|
17
|
-
assert_equal 'Premier League', rec.
|
17
|
+
assert_equal 'Premier League', rec.name
|
18
18
|
assert_equal 'eng.1', rec.key
|
19
19
|
assert_equal 'eng', rec.country.key
|
20
20
|
assert_equal 'England', rec.country.name
|
21
|
-
# assert_equal true, rec.
|
21
|
+
# assert_equal true, rec.clubs
|
22
22
|
|
23
23
|
rec = League.search!( 'eng' )
|
24
|
-
assert_equal 'Premier League', rec.
|
24
|
+
assert_equal 'Premier League', rec.name
|
25
25
|
assert_equal 'eng.1', rec.key
|
26
26
|
assert_equal 'eng', rec.country.key
|
27
27
|
assert_equal 'England', rec.country.name
|
28
|
-
## assert_equal true, rec.
|
28
|
+
## assert_equal true, rec.clubs
|
29
29
|
|
30
30
|
## try 2nd call (just lookup)
|
31
31
|
rec = League.search_or_create!( 'eng' )
|
32
|
-
assert_equal 'Premier League', rec.
|
32
|
+
assert_equal 'Premier League', rec.name
|
33
33
|
assert_equal 'eng.1', rec.key
|
34
34
|
assert_equal 'eng', rec.country.key
|
35
35
|
assert_equal 'England', rec.country.name
|
36
|
-
## assert_equal true, rec.
|
36
|
+
## assert_equal true, rec.clubs
|
37
37
|
end
|
38
38
|
end # class TestLeague
|
data/test/test_misc.rb
CHANGED
@@ -14,15 +14,15 @@ class TestMisc < MiniTest::Test
|
|
14
14
|
|
15
15
|
def test_season
|
16
16
|
rec = Season.search_or_create( '2017-18' )
|
17
|
-
assert_equal '2017/18', rec.
|
17
|
+
assert_equal '2017/18', rec.name
|
18
18
|
assert_equal '2017/18', rec.key
|
19
19
|
|
20
20
|
rec = Season.search_or_create( '2017/2018' )
|
21
|
-
assert_equal '2017/18', rec.
|
21
|
+
assert_equal '2017/18', rec.name
|
22
22
|
assert_equal '2017/18', rec.key
|
23
23
|
|
24
24
|
rec = Season.search_or_create( '2017/8' )
|
25
|
-
assert_equal '2017/18', rec.
|
25
|
+
assert_equal '2017/18', rec.name
|
26
26
|
assert_equal '2017/18', rec.key
|
27
27
|
end
|
28
28
|
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sportdb-sync
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: sportdb-
|
14
|
+
name: sportdb-catalogs
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.0.
|
19
|
+
version: 1.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.0.
|
26
|
+
version: 1.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sportdb-models
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 2.0.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rdoc
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|