sportdb-sync 1.0.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 348a66cb30ccc072d2f6e0a82f61af6234e193d2
4
- data.tar.gz: cae64f1ab5a09c9b0cd930d49a62f4b3a565eceb
3
+ metadata.gz: cdadfa4b00e61764c6b7b1f62ffb983f89c4a1ff
4
+ data.tar.gz: 3721087e4b6ff6bf0e604a00cce8658df1cb05bb
5
5
  SHA512:
6
- metadata.gz: a864409290725d1ed3d6cf529ae7bf30c6091f28e76ce01fe427b1ff0cf31349d049a07e7de39c599f8e8db1b0136c2e2d7e1750aac1eab90b43e78a4153d4b4
7
- data.tar.gz: b28d7dd3b5202cb3d58e1afb7e24c21f45e6b74c19391717257e16332c93470f17a78810314252bd4ce24758bc50af1d6d533ccbc41d712bb6d1282943533797
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 = ['https://github.com/sportdb/sport.db']
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-config', '>= 1.0.4'],
24
- ['sportdb-models', '>= 1.19.0'],
23
+ ['sportdb-catalogs', '>= 1.0.0'],
24
+ ['sportdb-models', '>= 2.0.2'],
25
25
  ]
26
26
 
27
27
  self.spec_extras = {
@@ -1,20 +1,9 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'sportdb/config'
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
@@ -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/titles for now for clubs
54
- rec = Model::Team.find_by( title: club.name )
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
- title: club.name,
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[:synonyms] = club.alt_names.join('|')
74
+ attribs[:alt_names] = club.alt_names.join('|')
75
75
  end
76
76
 
77
77
  rec = Model::Team.create!( attribs )
@@ -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" start_at date for now
83
- attribs[:start_at] = if season.year? ## e.g. assume 2018 etc.
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 )
@@ -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
- title: league.name }
44
+ name: league.name }
48
45
 
49
46
  if league.country
50
47
  attribs[ :country_id ] = Sync::Country.find_or_create( league.country ).id
@@ -43,7 +43,7 @@ module SportDb
43
43
  rec = find( season )
44
44
  if rec.nil?
45
45
  attribs = { key: season.key,
46
- title: season.name }
46
+ name: season.name }
47
47
  rec = Model::Season.create!( attribs )
48
48
  end
49
49
  rec
@@ -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( title: team.name )
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
- title: team.name,
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[:synonyms] = team.alt_names.join('|')
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( title: round.title, event_id: event.id )
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
- title: round.title,
71
- pos: round.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( title: group.title, event_id: event.id )
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
- title: group.title,
87
- pos: group.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( title: name, event_id: event.id )
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
- title: name,
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 ## todo/check: add alias for Game class - why? why not?
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
- ## query for round - allow string or round rec
135
- round_title = match.round.is_a?( String ) ? match.round : match.round.title
136
- round_rec = Model::Round.find_by!( event_id: event.id,
137
- title: round_title )
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!( title: team1_name )
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
- group_title = match.group.is_a?( String ) ? match.group : match.group.title
178
+ group_name = match.group.is_a?( String ) ? match.group : match.group.name
151
179
  Model::Group.find_by!( event_id: event.id,
152
- title: group_title )
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
- attribs = { round_id: round_rec.id,
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: 999, ## make optional why? why not? - change to num?
166
- play_at: match.date.to_date,
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::Game.create!( attribs )
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
@@ -6,8 +6,8 @@ module Module
6
6
  module Sync
7
7
 
8
8
  MAJOR = 1 ## todo: namespace inside version or something - why? why not??
9
- MINOR = 0
10
- PATCH = 1
9
+ MINOR = 1
10
+ PATCH = 2
11
11
  VERSION = [MAJOR,MINOR,PATCH].join('.')
12
12
 
13
13
  def self.version
@@ -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-config/lib' ))
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
 
@@ -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.title ## fix: add name alias (rename title to name!!)
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.club
21
+ # assert_equal true, rec.clubs
22
22
 
23
23
  rec = League.search!( 'eng' )
24
- assert_equal 'Premier League', rec.title ## fix: add name alias (rename title to name!!)
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.club
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.title ## fix: add name alias (rename title to name!!)
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.club
36
+ ## assert_equal true, rec.clubs
37
37
  end
38
38
  end # class TestLeague
@@ -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.title ## fix: add name alias (rename title to name!!)
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.title ## fix: add name alias (rename title to name!!)
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.title ## fix: add name alias (rename title to name!!)
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.0.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-05-19 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sportdb-config
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.4
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.4
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: 1.19.0
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: 1.19.0
40
+ version: 2.0.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement