sportdb-structs 0.4.1 → 0.5.0
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/CHANGELOG.md +1 -1
- data/Manifest.txt +4 -0
- data/Rakefile +1 -0
- data/lib/sportdb/csv/goal.rb +192 -0
- data/lib/sportdb/csv/goal_parser_csv.rb +28 -0
- data/lib/sportdb/csv/match_parser_csv.rb +490 -0
- data/lib/sportdb/csv/match_status_parser.rb +63 -0
- data/lib/sportdb/structs/country.rb +101 -5
- data/lib/sportdb/structs/league.rb +148 -15
- data/lib/sportdb/structs/match.rb +17 -15
- data/lib/sportdb/structs/round.rb +3 -2
- data/lib/sportdb/structs/team.rb +12 -9
- data/lib/sportdb/structs/version.rb +2 -2
- data/lib/sportdb/structs.rb +40 -3
- metadata +23 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6efbd5615829502f2960305080f413c167a199b0c5be71fd2ca46882633bb447
|
4
|
+
data.tar.gz: 7d9025f5ac565666527eb87b67a6f94e6607b4d8cca023a5478d601b523144d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78e8dea8c1842fe14e57ecc4794263c2ab29a17d43bd45c9131aa8889c4e05f026506d43768293679c88ebb563caf47dfca9610f0a1736bb964f325fa945886d
|
7
|
+
data.tar.gz: b950d1600926c9c6394c6267a874dcc6fe17ce5cd7f06cfe34244474aaa8bce535ce598142b98b8ef3f8e17cb31c1f6d1003b1eca8331451bdc75fb3542a0ccf
|
data/CHANGELOG.md
CHANGED
data/Manifest.txt
CHANGED
@@ -2,6 +2,10 @@ CHANGELOG.md
|
|
2
2
|
Manifest.txt
|
3
3
|
README.md
|
4
4
|
Rakefile
|
5
|
+
lib/sportdb/csv/goal.rb
|
6
|
+
lib/sportdb/csv/goal_parser_csv.rb
|
7
|
+
lib/sportdb/csv/match_parser_csv.rb
|
8
|
+
lib/sportdb/csv/match_status_parser.rb
|
5
9
|
lib/sportdb/structs.rb
|
6
10
|
lib/sportdb/structs/country.rb
|
7
11
|
lib/sportdb/structs/event_info.rb
|
data/Rakefile
CHANGED
@@ -0,0 +1,192 @@
|
|
1
|
+
|
2
|
+
module Sports
|
3
|
+
|
4
|
+
## "free-standing" goal event - for import/export in separate event / goal datafiles
|
5
|
+
## returned by CsvGoalParser and others
|
6
|
+
class GoalEvent
|
7
|
+
|
8
|
+
def self.build( row ) ## rename to parse or such - why? why not?
|
9
|
+
|
10
|
+
## split match_id
|
11
|
+
team_str, more_str = row['Match'].split( '|' )
|
12
|
+
team1_str, team2_str = team_str.split( ' - ' )
|
13
|
+
|
14
|
+
more_str = more_str.strip
|
15
|
+
team1_str = team1_str.strip
|
16
|
+
team2_str = team2_str.strip
|
17
|
+
|
18
|
+
# check if more_str is a date otherwise assume round
|
19
|
+
date_fmt = if more_str =~ /^[A-Z]{3} [0-9]{1,2}$/i ## Apr 4
|
20
|
+
'%b %d'
|
21
|
+
elsif more_str =~ /^[A-Z]{3} [0-9]{1,2} [0-9]{4}$/i ## Apr 4 2019
|
22
|
+
'%b %d %Y'
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
if date_fmt
|
28
|
+
date = Date.strptime( more_str, date_fmt )
|
29
|
+
round = nil
|
30
|
+
else
|
31
|
+
date = nil
|
32
|
+
round = more_str
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
values = row['Score'].split('-')
|
37
|
+
values = values.map { |value| value.strip }
|
38
|
+
score1 = values[0].to_i
|
39
|
+
score2 = values[1].to_i
|
40
|
+
|
41
|
+
minute = nil
|
42
|
+
offset = nil
|
43
|
+
if m=%r{([0-9]+)
|
44
|
+
(?:[ ]+
|
45
|
+
\+([0-9]+)
|
46
|
+
)?
|
47
|
+
['.]
|
48
|
+
$}x.match( row['Minute'])
|
49
|
+
minute = m[1].to_i
|
50
|
+
offset = m[2] ? m[2].to_i : nil
|
51
|
+
else
|
52
|
+
puts "!! ERROR - unsupported minute (goal) format >#{row['Minute']}<"
|
53
|
+
exit 1
|
54
|
+
end
|
55
|
+
|
56
|
+
attributes = {
|
57
|
+
team1: team1_str,
|
58
|
+
team2: team2_str,
|
59
|
+
date: date,
|
60
|
+
round: round,
|
61
|
+
score1: score1,
|
62
|
+
score2: score2,
|
63
|
+
minute: minute,
|
64
|
+
offset: offset,
|
65
|
+
player: row['Player'],
|
66
|
+
owngoal: ['(og)', '(o.g.)'].include?( row['Extra']),
|
67
|
+
penalty: ['(pen)', '(pen.)'].include?( row['Extra']),
|
68
|
+
notes: (row['Notes'].nil? || row['Notes'].empty?) ? nil : row['Notes']
|
69
|
+
}
|
70
|
+
|
71
|
+
new( **attributes )
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
## match id
|
76
|
+
attr_reader :team1,
|
77
|
+
:team2,
|
78
|
+
:round, ## optional
|
79
|
+
:date ## optional
|
80
|
+
|
81
|
+
## main attributes
|
82
|
+
attr_reader :score1,
|
83
|
+
:score2,
|
84
|
+
:player,
|
85
|
+
:minute,
|
86
|
+
:offset,
|
87
|
+
:owngoal,
|
88
|
+
:penalty,
|
89
|
+
:notes
|
90
|
+
|
91
|
+
|
92
|
+
## todo/check: or just use match.hash or such if match mapping known - why? why not?
|
93
|
+
def match_id
|
94
|
+
if round
|
95
|
+
"#{@team1} - #{@team2} | #{@round}"
|
96
|
+
else
|
97
|
+
"#{@team1} - #{@team2} | #{@date}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def owngoal?() @owngoal==true; end
|
103
|
+
def penalty?() @penalty==true; end
|
104
|
+
|
105
|
+
def initialize( team1:,
|
106
|
+
team2:,
|
107
|
+
round: nil,
|
108
|
+
date: nil,
|
109
|
+
score1:,
|
110
|
+
score2:,
|
111
|
+
player:,
|
112
|
+
minute:,
|
113
|
+
offset: nil,
|
114
|
+
owngoal: false,
|
115
|
+
penalty: false,
|
116
|
+
notes: nil
|
117
|
+
)
|
118
|
+
@team1 = team1
|
119
|
+
@team2 = team2
|
120
|
+
@round = round
|
121
|
+
@date = date
|
122
|
+
|
123
|
+
@score1 = score1
|
124
|
+
@score2 = score2
|
125
|
+
@player = player
|
126
|
+
@minute = minute
|
127
|
+
@offset = offset
|
128
|
+
@owngoal = owngoal
|
129
|
+
@penalty = penalty
|
130
|
+
@notes = notes
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
## note: lets you use normalize teams or such acts like a Match struct
|
135
|
+
def update( **kwargs )
|
136
|
+
## todo/fix: use team1_name, team2_name or similar - for compat with db activerecord version? why? why not?
|
137
|
+
@team1 = kwargs[:team1] if kwargs.has_key? :team1
|
138
|
+
@team2 = kwargs[:team2] if kwargs.has_key? :team2
|
139
|
+
end
|
140
|
+
end # class GoalEvent
|
141
|
+
|
142
|
+
|
143
|
+
### extend "basic" goal struct with goal event build
|
144
|
+
class Goal ### nested (non-freestanding) inside match (match is parent)
|
145
|
+
|
146
|
+
def self.build( events ) ## check/todo - rename to build_from_event/row or such - why? why not?
|
147
|
+
## build an array of goal structs from (csv) recs
|
148
|
+
recs = []
|
149
|
+
|
150
|
+
last_score1 = 0
|
151
|
+
last_score2 = 0
|
152
|
+
|
153
|
+
events.each do |event|
|
154
|
+
|
155
|
+
if last_score1+1 == event.score1 && last_score2 == event.score2
|
156
|
+
team = 1
|
157
|
+
elsif last_score2+1 == event.score2 && last_score1 == event.score1
|
158
|
+
team = 2
|
159
|
+
else
|
160
|
+
puts "!! ERROR - unexpected score advance (one goal at a time expected):"
|
161
|
+
puts " #{last_score1}-#{last_score2}=> #{event.score1}-#{event.score2}"
|
162
|
+
exit 1
|
163
|
+
end
|
164
|
+
|
165
|
+
last_score1 = event.score1
|
166
|
+
last_score2 = event.score2
|
167
|
+
|
168
|
+
|
169
|
+
attributes = {
|
170
|
+
score1: event.score1,
|
171
|
+
score2: event.score2,
|
172
|
+
team: team,
|
173
|
+
minute: event.minute,
|
174
|
+
offset: event.offset,
|
175
|
+
player: event.player,
|
176
|
+
owngoal: event.owngoal,
|
177
|
+
penalty: event.penalty,
|
178
|
+
notes: event.notes
|
179
|
+
}
|
180
|
+
|
181
|
+
recs << new( **attributes )
|
182
|
+
end
|
183
|
+
|
184
|
+
recs
|
185
|
+
end
|
186
|
+
end # class Goal
|
187
|
+
|
188
|
+
|
189
|
+
end # module Sports
|
190
|
+
|
191
|
+
|
192
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
module SportDb
|
3
|
+
class CsvGoalParser
|
4
|
+
|
5
|
+
|
6
|
+
def self.read( path )
|
7
|
+
txt = File.open( path, 'r:utf-8' ) {|f| f.read } ## note: make sure to use (assume) utf-8
|
8
|
+
parse( txt )
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.parse( txt )
|
12
|
+
new( txt ).parse
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def initialize( txt )
|
17
|
+
@txt = txt
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse
|
21
|
+
rows = parse_csv( @txt )
|
22
|
+
recs = rows.map { |row| Sports::GoalEvent.build( row ) }
|
23
|
+
## pp recs[0]
|
24
|
+
recs
|
25
|
+
end
|
26
|
+
|
27
|
+
end # class CsvGoalParser
|
28
|
+
end # module Sports
|