19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/sportdb/helpers/event_reader.rb', line 19
def parse
recs = []
parse_csv( @txt ).each do |row|
league_col = row['League']
season_col = row['Season'] || row['Year']
dates_col = row['Dates']
season = Season.parse( season_col )
league = League.find!( league_col )
dates = []
if dates_col.nil? || dates_col.empty?
else
dates_col = dates_col.gsub( /[ ]{2,}/, ' ' )
puts "#{league.name} (#{league.key}) | #{season.key} | #{dates_col}"
parts = dates_col.split( /[ ]*[–-][ ]*/ )
if parts.size == 1
pp parts
dates << DateFormats.parse( parts[0], start: Date.new( season.start_year, 1, 1 ), lang: 'en' )
pp dates
elsif parts.size == 2
pp parts
dates << DateFormats.parse( parts[0], start: Date.new( season.start_year, 1, 1 ), lang: 'en' )
dates << DateFormats.parse( parts[1], start: Date.new( season.end_year ? season.end_year : season.start_year, 1, 1 ), lang: 'en' )
pp dates
diff = dates[1].to_date.jd - dates[0].to_date.jd
puts "#{diff}d"
if diff > 365
puts "!! ERROR - date range / period assertion failed; expected diff < 365 days"
exit 1
end
else
puts "!! ERRROR - expected data range / period - one or two dates; got #{parts.size}:"
pp dates_col
pp parts
exit 1
end
end
teams_col = row['Clubs'] || row['Teams']
goals_col = row['Goals']
teams_col = teams_col.gsub( /[^0-9]/, '' ) if teams_col
goals_col = goals_col.gsub( /[^0-9]/, '' ) if goals_col
teams = (teams_col.nil? || teams_col.empty?) ? nil : teams_col.to_i
goals = (goals_col.nil? || goals_col.empty?) ? nil : goals_col.to_i
matches_col = row['Matches']
matches_col = matches_col.gsub( /[^0-9+]/, '' ) if matches_col
matches = if matches_col.nil? || matches_col.empty?
nil
else
if matches_col.index( '+' )
matches_col.split( '+' ).reduce( 0 ) do |sum,str|
sum + str.to_i
end
else
matches_col.to_i
end
end
rec = EventInfo.new( league: league,
season: season,
start_date: dates[0],
end_date: dates[1],
teams: teams,
matches: matches,
goals: goals
)
recs << rec
end
recs
end
|