sportdb-sync 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03f672e265c84d8bcac59a05381705e7b64acc69
4
+ data.tar.gz: 3fcdc3f0bbc5968e641fb0b873e473b10adbe94e
5
+ SHA512:
6
+ metadata.gz: 98d27e26d97d13eab1fb49907e77bac2f326592451fedc7f839b26d0e96ba659fef61c44d1bd0bb5addccd79c948ef42b1926ea6091a06eaca42eaa01eca369b
7
+ data.tar.gz: 8913f9a0a783e93b43a2db943164812888e85c60675d9602b18d87745909905512bc8f0ac58264ce8789140e4ad00bf4aea6b90b2c0061468b5dc70aa5b75a4c
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.0.1 / 2019-11-04
2
+
3
+ * Everything is new. First release.
data/Manifest.txt ADDED
@@ -0,0 +1,9 @@
1
+ CHANGELOG.md
2
+ Manifest.txt
3
+ README.md
4
+ Rakefile
5
+ lib/sportdb/sync.rb
6
+ lib/sportdb/sync/sync.rb
7
+ lib/sportdb/sync/version.rb
8
+ test/helper.rb
9
+ test/test_sync.rb
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # sportdb-sync - sport.db sync helpers for leagues, seasons, clubs, match schedules and results, and more
2
+
3
+
4
+ * home :: [github.com/sportdb/sport.db](https://github.com/sportdb/sport.db)
5
+ * bugs :: [github.com/sportdb/sport.db/issues](https://github.com/sportdb/sport.db/issues)
6
+ * gem :: [rubygems.org/gems/sportdb-sync](https://rubygems.org/gems/sportdb-sync)
7
+ * rdoc :: [rubydoc.info/gems/sportdb-sync](http://rubydoc.info/gems/sportdb-sync)
8
+ * forum :: [opensport](http://groups.google.com/group/opensport)
9
+
10
+
11
+
12
+ ## Usage
13
+
14
+ TBD
15
+
16
+
17
+
18
+ ## License
19
+
20
+ The `sportdb-sync` scripts are dedicated to the public domain.
21
+ Use it as you please with no restrictions whatsoever.
22
+
23
+
24
+ ## Questions? Comments?
25
+
26
+ Send them along to the
27
+ [Open Sports & Friends Forum/Mailing List](http://groups.google.com/group/opensport).
28
+ Thanks!
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'hoe'
2
+ require './lib/sportdb/sync/version.rb'
3
+
4
+ Hoe.spec 'sportdb-sync' do
5
+
6
+ self.version = SportDb::Sync::VERSION
7
+
8
+ self.summary = "sportdb-sync - sport.db sync helpers for leagues, seasons, clubs, match schedules and results, and more"
9
+ self.description = summary
10
+
11
+ self.urls = ['https://github.com/sportdb/sport.db']
12
+
13
+ self.author = 'Gerald Bauer'
14
+ self.email = 'opensport@googlegroups.com'
15
+
16
+ # switch extension to .markdown for gihub formatting
17
+ self.readme_file = 'README.md'
18
+ self.history_file = 'CHANGELOG.md'
19
+
20
+ self.licenses = ['Public Domain']
21
+
22
+ self.extra_deps = [
23
+ ['sportdb-clubs', '>= 0.2.3'],
24
+ ['sportdb-leagues', '>= 0.2.1'],
25
+ ['sportdb-models', '>= 1.18.2'],
26
+ ]
27
+
28
+ self.spec_extras = {
29
+ required_ruby_version: '>= 2.2.2'
30
+ }
31
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ require 'sportdb/clubs'
5
+ require 'sportdb/leagues'
6
+
7
+
8
+ require 'sportdb/models' ## add sql database support
9
+
10
+
11
+
12
+ ###
13
+ # our own code
14
+ require 'sportdb/sync/version' # let version always go first
15
+ require 'sportdb/sync/sync'
16
+
17
+
18
+ puts SportDb::Sync.banner # say hello
@@ -0,0 +1,241 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module SportDb
5
+
6
+ module Sync
7
+ class Country
8
+ def self.find_or_create( country )
9
+ rec = WorldDb::Model::Country.find_by( key: country.key )
10
+ if rec.nil?
11
+ attribs = {
12
+ key: country.key,
13
+ name: country.name,
14
+ code: country.fifa, ## fix: uses fifa code now (should be iso-alpha3 if available)
15
+ fifa: country.fifa,
16
+ area: 1,
17
+ pop: 1
18
+ }
19
+ rec = WorldDb::Model::Country.create!( attribs )
20
+ end
21
+ rec
22
+ end
23
+ end # class Country
24
+
25
+
26
+ class League
27
+ def self.find( league )
28
+ SportDb::Model::League.find_by( key: league.key )
29
+ end
30
+ def self.find!( league )
31
+ rec = find( league )
32
+ if rec.nil?
33
+ puts "** !!!ERROR!!! db sync - no league match found for:"
34
+ pp league
35
+ exit 1
36
+ end
37
+ rec
38
+ end
39
+
40
+ def self.find_or_create( league )
41
+ rec = find( league )
42
+ if rec.nil?
43
+ ## use title and not name - why? why not?
44
+ ## quick fix: change name to title
45
+ attribs = { key: league.key,
46
+ title: league.name }
47
+ if league.country
48
+ attribs[ :country_id ] = Country.find_or_create( league.country ).id
49
+ end
50
+
51
+ rec = SportDb::Model::League.create!( attribs )
52
+ end
53
+ rec
54
+ end
55
+ end # class League
56
+
57
+
58
+ class Season
59
+ def self.normalize_key( key ) ## helper for season key (rename to norm_key ???)
60
+ ## note: "normalize" season key
61
+ ## always use 2017/18 (and not 2017-18 or 2017-2018 or 2017/2018)
62
+ ## 1) change 2017-18 to 2017/18
63
+ key = key.tr( '-', '/' )
64
+ ## 2) check for 2017/2018 - change to 2017/18
65
+ if key.length == 9
66
+ key = "#{key[0..3]}/#{key[7..8]}"
67
+ end
68
+ key
69
+ end
70
+
71
+ def self.find( key )
72
+ key = normalize_key( key )
73
+ SportDb::Model::Season.find_by( key: key )
74
+ end
75
+ def self.find!( key )
76
+ rec = find( key )
77
+ if rec.nil?
78
+ puts "** !!!ERROR!!! db sync - no season match found for >#{normalize_key(key)}<:"
79
+ pp key
80
+ exit 1
81
+ end
82
+ rec
83
+ end
84
+
85
+ def self.find_or_create( key ) ## e.g. key = '2017/18'
86
+ rec = find( key )
87
+ if rec.nil?
88
+ key = normalize_key( key ) ## note: do NOT forget to normalize key e.g. always use slash (2019/20) etc.
89
+ attribs = { key: key,
90
+ title: key }
91
+ rec = SportDb::Model::Season.create!( attribs )
92
+ end
93
+ rec
94
+ end
95
+ end # class Season
96
+
97
+ class Club
98
+ def self.find_or_create( club )
99
+ rec = SportDb::Model::Team.find_by( title: club.name )
100
+ if rec.nil?
101
+ ## remove all non-ascii a-z chars
102
+ key = club.name.downcase.gsub( /[^a-z]/, '' )
103
+ puts "add club: #{key}, #{club.name}, #{club.country.name} (#{club.country.key})"
104
+
105
+ attribs = {
106
+ key: key,
107
+ title: club.name,
108
+ country_id: Country.find_or_create( club.country ).id,
109
+ club: true,
110
+ national: false ## check -is default anyway - use - why? why not?
111
+ ## todo/fix: add city if present - why? why not?
112
+ }
113
+ if club.alt_names.empty? == false
114
+ attribs[:synonyms] = club.alt_names.join('|')
115
+ end
116
+
117
+ rec = SportDb::Model::Team.create!( attribs )
118
+ end
119
+ rec
120
+ end
121
+ end # class Club
122
+
123
+ class Event
124
+ def self.find( league:, season: )
125
+ SportDb::Model::Event.find_by( league_id: league.id, season_id: season.id )
126
+ end
127
+ def self.find!( league:, season: )
128
+ rec = find( league: league, season: season )
129
+ if rec.nil?
130
+ puts "** !!!ERROR!!! db sync - no event match found for:"
131
+ pp league
132
+ pp season
133
+ exit 1
134
+ end
135
+ rec
136
+ end
137
+
138
+ def self.find_or_create( league:, season: )
139
+ rec = find( league: league, season: season )
140
+ if rec.nil?
141
+ ## quick hack/change later !!
142
+ ## todo/fix: check season - if is length 4 (single year) use 2017, 1, 1
143
+ ## otherwise use 2017, 7, 1
144
+ ## start_at use year and 7,1 e.g. Date.new( 2017, 7, 1 )
145
+ ## hack: fix/todo1!!
146
+ ## add "fake" start_at date for now
147
+ if season.key.size == '4' ## e.g. assume 2018 etc.
148
+ year = season.key.to_i
149
+ start_at = Date.new( year, 1, 1 )
150
+ else ## assume 2014/15 etc.
151
+ year = season.key[0..3].to_i
152
+ start_at = Date.new( year, 7, 1 )
153
+ end
154
+
155
+ attribs = {
156
+ league_id: league.id,
157
+ season_id: season.id,
158
+ start_at: start_at }
159
+
160
+ rec = SportDb::Model::Event.create!( attribs )
161
+ end
162
+ rec
163
+ end
164
+ end # class Event
165
+
166
+ class Round
167
+ def self.find_or_create( round, event: )
168
+ rec = SportDb::Model::Round.find_by( title: round.title, event_id: event.id )
169
+ if rec.nil?
170
+ attribs = { event_id: event.id,
171
+ title: round.title,
172
+ pos: round.pos,
173
+ start_at: event.start_at.to_date
174
+ }
175
+ rec = SportDb::Model::Round.create!( attribs )
176
+ end
177
+ rec
178
+ end
179
+ end # class Round
180
+
181
+
182
+ class Stage
183
+ def self.find( name, event: )
184
+ SportDb::Model::Stage.find_by( title: name, event_id: event.id )
185
+ end
186
+ def self.find!( name, event: )
187
+ rec = find( name, event: event )
188
+ if rec.nil?
189
+ puts "** !!!ERROR!!! db sync - no stage match found for:"
190
+ pp name
191
+ pp event
192
+ exit 1
193
+ end
194
+ rec
195
+ end
196
+
197
+ def self.find_or_create( name, event: )
198
+ rec = find( name, event: event )
199
+ if rec.nil?
200
+ ## use title and not name - why? why not?
201
+ ## quick fix: change name to title
202
+ attribs = { event_id: event.id,
203
+ title: name,
204
+ }
205
+ rec = SportDb::Model::Stage.create!( attribs )
206
+ end
207
+ rec
208
+ end
209
+ end # class Stage
210
+
211
+
212
+
213
+ class Match ## todo/check: add alias for Game class - why? why not?
214
+ def self.create_or_update( match, event: )
215
+ ## note: MUST find round, thus, use bang (!)
216
+ round_rec = SportDb::Model::Round.find_by!( event_id: event.id,
217
+ title: match.round.title )
218
+
219
+ rec = SportDb::Model::Game.find_by( round_id: round_rec.id,
220
+ team1_id: match.team1.id,
221
+ team2_id: match.team2.id )
222
+ if rec.nil?
223
+ attribs = { round_id: round_rec.id,
224
+ team1_id: match.team1.id,
225
+ team2_id: match.team2.id,
226
+ pos: 999, ## make optional why? why not? - change to num?
227
+ play_at: match.date.to_date,
228
+ score1: match.score1,
229
+ score2: match.score2,
230
+ score1i: match.score1i,
231
+ score2i: match.score2i }
232
+ rec = SportDb::Model::Game.create!( attribs )
233
+ else
234
+ # update - todo
235
+ end
236
+ rec
237
+ end
238
+ end # class Match
239
+
240
+ end # module Sync
241
+ end # module SportDb
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ module SportDb
5
+ module Sync
6
+
7
+ MAJOR = 0 ## todo: namespace inside version or something - why? why not??
8
+ MINOR = 0
9
+ PATCH = 1
10
+ VERSION = [MAJOR,MINOR,PATCH].join('.')
11
+
12
+ def self.version
13
+ VERSION
14
+ end
15
+
16
+ def self.banner
17
+ "sportdb-sync/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
18
+ end
19
+
20
+ def self.root
21
+ File.expand_path( File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) )
22
+ end
23
+
24
+ end # module Sync
25
+ end # module SportDb
data/test/helper.rb ADDED
@@ -0,0 +1,8 @@
1
+ ## $:.unshift(File.dirname(__FILE__))
2
+
3
+ ## minitest setup
4
+ require 'minitest/autorun'
5
+
6
+
7
+ ## our own code
8
+ require 'sportdb/sync'
data/test/test_sync.rb ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ ###
4
+ # to run use
5
+ # ruby -I ./lib -I ./test test/test_sync.rb
6
+
7
+
8
+ require 'helper'
9
+
10
+
11
+ class TestSync < MiniTest::Test
12
+
13
+ def test_sync
14
+ SportDb.connect( adapter: 'sqlite3', database: ':memory:' )
15
+ SportDb.create_all ## build schema
16
+
17
+ ## turn on logging to console
18
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
19
+
20
+ at_rec = SportDb::Import::Country.new( 'at', 'Austria', fifa: 'AUT' )
21
+ eng_rec = SportDb::Import::Country.new( 'eng', 'England', fifa: 'ENG' )
22
+
23
+ at = SportDb::Sync::Country.find_or_create( at_rec )
24
+ at2 = SportDb::Sync::Country.find_or_create( at_rec )
25
+
26
+ eng = SportDb::Sync::Country.find_or_create( eng_rec )
27
+ eng2 = SportDb::Sync::Country.find_or_create( eng_rec )
28
+ end # method test_sync
29
+ end # class TestSync
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sportdb-sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gerald Bauer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sportdb-clubs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: sportdb-leagues
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sportdb-models
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.18.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.18.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: hoe
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.16'
83
+ description: sportdb-sync - sport.db sync helpers for leagues, seasons, clubs, match
84
+ schedules and results, and more
85
+ email: opensport@googlegroups.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - CHANGELOG.md
90
+ - Manifest.txt
91
+ - README.md
92
+ files:
93
+ - CHANGELOG.md
94
+ - Manifest.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/sportdb/sync.rb
98
+ - lib/sportdb/sync/sync.rb
99
+ - lib/sportdb/sync/version.rb
100
+ - test/helper.rb
101
+ - test/test_sync.rb
102
+ homepage: https://github.com/sportdb/sport.db
103
+ licenses:
104
+ - Public Domain
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options:
108
+ - "--main"
109
+ - README.md
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.2.2
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: sportdb-sync - sport.db sync helpers for leagues, seasons, clubs, match schedules
128
+ and results, and more
129
+ test_files: []