sportdb-formats 1.0.6 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/Manifest.txt +6 -33
  3. data/Rakefile +2 -5
  4. data/lib/sportdb/formats.rb +54 -70
  5. data/lib/sportdb/formats/country/country_index.rb +2 -2
  6. data/lib/sportdb/formats/event/event_index.rb +141 -0
  7. data/lib/sportdb/formats/event/event_reader.rb +183 -0
  8. data/lib/sportdb/formats/league/league_index.rb +22 -18
  9. data/lib/sportdb/formats/league/league_outline_reader.rb +45 -13
  10. data/lib/sportdb/formats/league/league_reader.rb +7 -1
  11. data/lib/sportdb/formats/match/match_parser.rb +101 -111
  12. data/lib/sportdb/formats/package.rb +59 -11
  13. data/lib/sportdb/formats/parser_helper.rb +11 -2
  14. data/lib/sportdb/formats/team/club_index.rb +13 -11
  15. data/lib/sportdb/formats/team/club_index_history.rb +134 -0
  16. data/lib/sportdb/formats/team/club_reader_history.rb +203 -0
  17. data/lib/sportdb/formats/team/club_reader_props.rb +20 -5
  18. data/lib/sportdb/formats/version.rb +2 -2
  19. data/test/helper.rb +51 -81
  20. data/test/test_club_index_history.rb +107 -0
  21. data/test/test_club_reader_history.rb +212 -0
  22. data/test/test_datafile_package.rb +1 -1
  23. data/test/test_regex.rb +25 -7
  24. metadata +9 -78
  25. data/lib/sportdb/formats/config.rb +0 -40
  26. data/lib/sportdb/formats/match/match_parser_csv.rb +0 -314
  27. data/lib/sportdb/formats/name_helper.rb +0 -84
  28. data/lib/sportdb/formats/score/score_formats.rb +0 -220
  29. data/lib/sportdb/formats/score/score_parser.rb +0 -202
  30. data/lib/sportdb/formats/season_utils.rb +0 -27
  31. data/lib/sportdb/formats/structs/country.rb +0 -31
  32. data/lib/sportdb/formats/structs/group.rb +0 -18
  33. data/lib/sportdb/formats/structs/league.rb +0 -37
  34. data/lib/sportdb/formats/structs/match.rb +0 -151
  35. data/lib/sportdb/formats/structs/matchlist.rb +0 -220
  36. data/lib/sportdb/formats/structs/round.rb +0 -25
  37. data/lib/sportdb/formats/structs/season.rb +0 -123
  38. data/lib/sportdb/formats/structs/standings.rb +0 -247
  39. data/lib/sportdb/formats/structs/team.rb +0 -150
  40. data/lib/sportdb/formats/structs/team_usage.rb +0 -88
  41. data/test/test_clubs.rb +0 -40
  42. data/test/test_conf.rb +0 -65
  43. data/test/test_csv_match_parser.rb +0 -114
  44. data/test/test_csv_match_parser_utils.rb +0 -20
  45. data/test/test_csv_reader.rb +0 -31
  46. data/test/test_match.rb +0 -30
  47. data/test/test_match_auto.rb +0 -72
  48. data/test/test_match_auto_champs.rb +0 -45
  49. data/test/test_match_auto_euro.rb +0 -37
  50. data/test/test_match_auto_worldcup.rb +0 -61
  51. data/test/test_match_champs.rb +0 -27
  52. data/test/test_match_eng.rb +0 -26
  53. data/test/test_match_euro.rb +0 -27
  54. data/test/test_match_worldcup.rb +0 -27
  55. data/test/test_name_helper.rb +0 -67
  56. data/test/test_scores.rb +0 -122
  57. data/test/test_season.rb +0 -62
@@ -16,7 +16,7 @@ class TestDatafilePackage < MiniTest::Test
16
16
  ].each do |eng|
17
17
  assert eng.find( '2015-16/.conf.txt' ).read.start_with?( '= English Premier League 2015/16' )
18
18
  assert eng.find( '2017-18/.conf.txt' ).read.start_with?( '= English Premier League 2017/18' )
19
- assert eng.find( '2015-16/1-premierleague-i.txt' ).read.start_with?( '= English Premier League 2015/16' )
19
+ assert eng.find( '2015-16/1-premierleague.txt' ).read.start_with?( '= English Premier League 2015/16' )
20
20
  end
21
21
 
22
22
  [Datafile::DirPackage.new( '../../../openfootball/austria' ),
@@ -9,9 +9,27 @@ require 'helper'
9
9
 
10
10
  class TestRegex < MiniTest::Test
11
11
 
12
+ HEADER_SEP_RE = SportDb::MatchParser::HEADER_SEP_RE
13
+
14
+ def test_header_sep
15
+ assert_equal ['Round', 'Fr+Sa Dec 11+12'], 'Round // Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
16
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 /// Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
17
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 -- Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
18
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 --- Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
19
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 ++ Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
20
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 +++ Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
21
+ assert_equal ['Round 11', 'Fr+Sa Dec 11+12'], 'Round 11 || Fr+Sa Dec 11+12'.split( HEADER_SEP_RE )
22
+
23
+ assert_equal ['Final - Leg 1'], 'Final - Leg 1'.split( HEADER_SEP_RE )
24
+ assert_equal ['Final | Leg 1'], 'Final | Leg 1'.split( HEADER_SEP_RE )
25
+ assert_equal ['Final / Leg 1'], 'Final / Leg 1'.split( HEADER_SEP_RE )
26
+ assert_equal ['Final, Leg 1'], 'Final, Leg 1'.split( HEADER_SEP_RE )
27
+ end
28
+
29
+
12
30
  ADDR_MARKER_RE = SportDb::Import::ClubReader::ADDR_MARKER_RE
13
31
  B_TEAM_MARKER_RE = SportDb::Import::ClubReader::B_TEAM_MARKER_RE
14
-
32
+
15
33
  def test_addr
16
34
  assert '~ Wien' =~ ADDR_MARKER_RE
17
35
  assert 'Wien ~' =~ ADDR_MARKER_RE
@@ -22,13 +40,13 @@ class TestRegex < MiniTest::Test
22
40
  assert 'Fischhofgasse 12 /// 1100 Wien' =~ ADDR_MARKER_RE
23
41
 
24
42
  assert_nil 'Fischhofgasse 12 + 1100 Wien' =~ ADDR_MARKER_RE
25
- assert_nil 'Fischhofgasse 12++1100 Wien' =~ ADDR_MARKER_RE
43
+ assert_nil 'Fischhofgasse 12++1100 Wien' =~ ADDR_MARKER_RE ## todo/fix: make it a match!!! why? why not?
26
44
  assert_nil 'Fischhofgasse 12 / 1100 Wien' =~ ADDR_MARKER_RE
27
- assert_nil 'Fischhofgasse 12//1100 Wien' =~ ADDR_MARKER_RE
28
-
45
+ assert_nil 'Fischhofgasse 12//1100 Wien' =~ ADDR_MARKER_RE ## todo/fix: make it a match!!! why? why not?
46
+
29
47
  assert_nil 'Atlanta United FC, 2017, Atlanta › Georgia' =~ ADDR_MARKER_RE
30
48
  end
31
-
49
+
32
50
  def test_b_team
33
51
  assert 'b) Rapid Wien II' =~ B_TEAM_MARKER_RE
34
52
  assert '(b) Rapid Wien II' =~ B_TEAM_MARKER_RE
@@ -38,12 +56,12 @@ class TestRegex < MiniTest::Test
38
56
  assert '(ii.) Rapid Wien II' =~ B_TEAM_MARKER_RE
39
57
  assert '2) Rapid Wien II' =~ B_TEAM_MARKER_RE
40
58
  assert '(2) Rapid Wien II' =~ B_TEAM_MARKER_RE
41
-
59
+
42
60
  assert_nil '(3) Rapid Wien II' =~ B_TEAM_MARKER_RE
43
61
  assert_nil '(iii) Rapid Wien II' =~ B_TEAM_MARKER_RE
44
62
  assert_nil 'iii) Rapid Wien II' =~ B_TEAM_MARKER_RE
45
63
  assert_nil 'c) Rapid Wien II' =~ B_TEAM_MARKER_RE
46
64
  assert_nil '(c) Rapid Wien II' =~ B_TEAM_MARKER_RE
47
65
  end
48
-
66
+
49
67
  end # class TestRegex
metadata CHANGED
@@ -1,59 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sportdb-formats
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.4
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-24 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: alphabets
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.0.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 1.0.0
27
- - !ruby/object:Gem::Dependency
28
- name: date-formats
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 1.0.1
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 1.0.1
41
- - !ruby/object:Gem::Dependency
42
- name: csvreader
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: 1.2.4
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: 1.2.4
55
- - !ruby/object:Gem::Dependency
56
- name: sportdb-langs
14
+ name: sportdb-structs
57
15
  requirement: !ruby/object:Gem::Requirement
58
16
  requirements:
59
17
  - - ">="
@@ -122,11 +80,12 @@ files:
122
80
  - README.md
123
81
  - Rakefile
124
82
  - lib/sportdb/formats.rb
125
- - lib/sportdb/formats/config.rb
126
83
  - lib/sportdb/formats/country/country_index.rb
127
84
  - lib/sportdb/formats/country/country_reader.rb
128
85
  - lib/sportdb/formats/datafile.rb
129
86
  - lib/sportdb/formats/datafile_package.rb
87
+ - lib/sportdb/formats/event/event_index.rb
88
+ - lib/sportdb/formats/event/event_reader.rb
130
89
  - lib/sportdb/formats/goals.rb
131
90
  - lib/sportdb/formats/league/league_index.rb
132
91
  - lib/sportdb/formats/league/league_outline_reader.rb
@@ -136,26 +95,13 @@ files:
136
95
  - lib/sportdb/formats/match/mapper_teams.rb
137
96
  - lib/sportdb/formats/match/match_parser.rb
138
97
  - lib/sportdb/formats/match/match_parser_auto_conf.rb
139
- - lib/sportdb/formats/match/match_parser_csv.rb
140
- - lib/sportdb/formats/name_helper.rb
141
98
  - lib/sportdb/formats/outline_reader.rb
142
99
  - lib/sportdb/formats/package.rb
143
100
  - lib/sportdb/formats/parser_helper.rb
144
- - lib/sportdb/formats/score/score_formats.rb
145
- - lib/sportdb/formats/score/score_parser.rb
146
- - lib/sportdb/formats/season_utils.rb
147
- - lib/sportdb/formats/structs/country.rb
148
- - lib/sportdb/formats/structs/group.rb
149
- - lib/sportdb/formats/structs/league.rb
150
- - lib/sportdb/formats/structs/match.rb
151
- - lib/sportdb/formats/structs/matchlist.rb
152
- - lib/sportdb/formats/structs/round.rb
153
- - lib/sportdb/formats/structs/season.rb
154
- - lib/sportdb/formats/structs/standings.rb
155
- - lib/sportdb/formats/structs/team.rb
156
- - lib/sportdb/formats/structs/team_usage.rb
157
101
  - lib/sportdb/formats/team/club_index.rb
102
+ - lib/sportdb/formats/team/club_index_history.rb
158
103
  - lib/sportdb/formats/team/club_reader.rb
104
+ - lib/sportdb/formats/team/club_reader_history.rb
159
105
  - lib/sportdb/formats/team/club_reader_props.rb
160
106
  - lib/sportdb/formats/team/national_team_index.rb
161
107
  - lib/sportdb/formats/team/team_index.rb
@@ -163,37 +109,22 @@ files:
163
109
  - lib/sportdb/formats/version.rb
164
110
  - test/helper.rb
165
111
  - test/test_club_index.rb
112
+ - test/test_club_index_history.rb
166
113
  - test/test_club_reader.rb
114
+ - test/test_club_reader_history.rb
167
115
  - test/test_club_reader_props.rb
168
- - test/test_clubs.rb
169
- - test/test_conf.rb
170
116
  - test/test_country_index.rb
171
117
  - test/test_country_reader.rb
172
- - test/test_csv_match_parser.rb
173
- - test/test_csv_match_parser_utils.rb
174
- - test/test_csv_reader.rb
175
118
  - test/test_datafile.rb
176
119
  - test/test_datafile_package.rb
177
120
  - test/test_goals.rb
178
121
  - test/test_league_index.rb
179
122
  - test/test_league_outline_reader.rb
180
123
  - test/test_league_reader.rb
181
- - test/test_match.rb
182
- - test/test_match_auto.rb
183
- - test/test_match_auto_champs.rb
184
- - test/test_match_auto_euro.rb
185
- - test/test_match_auto_worldcup.rb
186
- - test/test_match_champs.rb
187
- - test/test_match_eng.rb
188
- - test/test_match_euro.rb
189
- - test/test_match_worldcup.rb
190
- - test/test_name_helper.rb
191
124
  - test/test_outline_reader.rb
192
125
  - test/test_package.rb
193
126
  - test/test_package_match.rb
194
127
  - test/test_regex.rb
195
- - test/test_scores.rb
196
- - test/test_season.rb
197
128
  - test/test_wiki_reader.rb
198
129
  homepage: https://github.com/sportdb/sport.db
199
130
  licenses:
@@ -1,40 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module SportDb
4
- module Import
5
-
6
- class Configuration
7
- ##
8
- ## todo: allow configure of countries_dir like clubs_dir
9
- ## "fallback" and use a default built-in world/countries.txt
10
-
11
- attr_accessor :catalog
12
-
13
- attr_reader :lang
14
- def lang=(value)
15
- ## check/todo: always use to_sym - why? needed?
16
- DateFormats.lang = value
17
- ScoreFormats.lang = value
18
- SportDb.lang.lang = value
19
-
20
- ## todo/fix: change SportDb.lang to SportDb.parser.lang or lang_parser or utils or someting !!!!
21
- ## use Sport.lang only as a read-only shortcut a la catalog for config.lang!!!!
22
- end
23
-
24
- end # class Configuration
25
-
26
-
27
- ## lets you use
28
- ## SportDb::Import.configure do |config|
29
- ## config.lang = 'it'
30
- ## end
31
-
32
- def self.configure() yield( config ); end
33
-
34
- def self.config() @config ||= Configuration.new; end
35
-
36
- ## e.g. use config.catalog -- keep Import.catalog as a shortcut (for "read-only" access)
37
- def self.catalog() config.catalog; end
38
-
39
- end # module Import
40
- end # module SportDb
@@ -1,314 +0,0 @@
1
- # encoding: utf-8
2
-
3
-
4
- module SportDb
5
- class CsvMatchParser
6
-
7
- #############
8
- # helpers
9
- def self.find_seasons( path, col: 'Season', sep: nil, headers: nil )
10
-
11
- ## check if headers incl. season if yes,has priority over col mapping
12
- ## e.g. no need to specify twice (if using headers)
13
- col = headers[:season] if headers && headers[:season]
14
-
15
- seasons = Hash.new( 0 ) ## default value is 0
16
-
17
- ## todo/fix: yes, use CsvHash.foreach - why? why not?
18
- ## use read_csv with block to switch to foreach!!!!
19
- rows = read_csv( path, sep: sep )
20
-
21
- rows.each_with_index do |row,i|
22
- puts "[#{i}] " + row.inspect if i < 2
23
-
24
- season = row[ col ] ## column name defaults to 'Season'
25
- seasons[ season ] += 1
26
- end
27
-
28
- pp seasons
29
-
30
- ## note: only return season keys/names (not hash with usage counter)
31
- seasons.keys
32
- end
33
-
34
-
35
- ##########
36
- # main machinery
37
-
38
- ## todo/fix: use a generic "global" parse_csv method - why? why not?
39
- ## def self.parse_csv( text, sep: ',' ) ## helper -lets you change the csv library in one place if needed/desired
40
- ## ## note: do NOT symbolize keys - keep them as is!!!!!!
41
- ## ## todo/fix: move "upstream" and remove symbolize keys too!!! - why? why not?
42
- ## CsvHash.parse( text, sep: sep )
43
- ## end
44
-
45
- def self.read( path, headers: nil, filters: nil, converters: nil, sep: nil )
46
- txt = File.open( path, 'r:utf-8' ) {|f| f.read } ## note: make sure to use (assume) utf-8
47
- parse( txt, headers: headers,
48
- filters: filters,
49
- converters: converters,
50
- sep: sep )
51
- end
52
-
53
- def self.parse( txt, headers: nil, filters: nil, converters: nil, sep: nil )
54
- new( txt ).parse( headers: headers,
55
- filters: filters,
56
- converters: converters,
57
- sep: sep )
58
- end
59
-
60
-
61
- def initialize( txt )
62
- @txt = txt
63
- end
64
-
65
- def parse( headers: nil, filters: nil, converters: nil, sep: nil )
66
-
67
- headers_mapping = {}
68
-
69
- rows = parse_csv( @txt, sep: sep )
70
-
71
- return [] if rows.empty? ## no rows / empty?
72
-
73
-
74
- ## fix/todo: use logger!!!!
75
- ## pp csv
76
-
77
- if headers ## use user supplied headers if present
78
- headers_mapping = headers_mapping.merge( headers )
79
- else
80
-
81
- ## note: returns an array of strings (header names) - assume all rows have the same columns/fields!!!
82
- headers = rows[0].keys
83
- pp headers
84
-
85
- # note: greece 2001-02 etc. use HT - check CVS reader row['HomeTeam'] may not be nil but an empty string?
86
- # e.g. row['HomeTeam'] || row['HT'] will NOT work for now
87
-
88
- if find_header( headers, ['Team 1']) && find_header( headers, ['Team 2'])
89
- ## assume our own football.csv format, see github.com/footballcsv
90
- headers_mapping[:team1] = find_header( headers, ['Team 1'] )
91
- headers_mapping[:team2] = find_header( headers, ['Team 2'] )
92
- headers_mapping[:date] = find_header( headers, ['Date'] )
93
-
94
- ## check for all-in-one full time (ft) and half time (ht9 scores?
95
- headers_mapping[:score] = find_header( headers, ['FT'] )
96
- headers_mapping[:scorei] = find_header( headers, ['HT'] )
97
-
98
- headers_mapping[:round] = find_header( headers, ['Round'] )
99
-
100
- ## optional headers - note: find_header returns nil if header NOT found
101
- header_stage = find_header( headers, ['Stage'] )
102
- headers_mapping[:stage] = header_stage if header_stage
103
- else
104
- ## else try footballdata.uk and others
105
- headers_mapping[:team1] = find_header( headers, ['HomeTeam', 'HT', 'Home'] )
106
- headers_mapping[:team2] = find_header( headers, ['AwayTeam', 'AT', 'Away'] )
107
- headers_mapping[:date] = find_header( headers, ['Date'] )
108
-
109
- ## note: FT = Full Time, HG = Home Goal, AG = Away Goal
110
- headers_mapping[:score1] = find_header( headers, ['FTHG', 'HG'] )
111
- headers_mapping[:score2] = find_header( headers, ['FTAG', 'AG'] )
112
-
113
- ## check for half time scores ?
114
- ## note: HT = Half Time
115
- headers_mapping[:score1i] = find_header( headers, ['HTHG'] )
116
- headers_mapping[:score2i] = find_header( headers, ['HTAG'] )
117
- end
118
- end
119
-
120
- pp headers_mapping
121
-
122
- ### todo/fix: check headers - how?
123
- ## if present HomeTeam or HT required etc.
124
- ## issue error/warn is not present
125
- ##
126
- ## puts "*** !!! wrong (unknown) headers format; cannot continue; fix it; sorry"
127
- ## exit 1
128
- ##
129
-
130
- matches = []
131
-
132
- rows.each_with_index do |row,i|
133
-
134
- ## fix/todo: use logger!!!!
135
- ## puts "[#{i}] " + row.inspect if i < 2
136
-
137
-
138
- ## todo/fix: move to its own (helper) method - filter or such!!!!
139
- if filters ## filter MUST match if present e.g. row['Season'] == '2017/2018'
140
- skip = false
141
- filters.each do |header, value|
142
- if row[ header ] != value ## e.g. row['Season']
143
- skip = true
144
- break
145
- end
146
- end
147
- next if skip ## if header values NOT matching
148
- end
149
-
150
-
151
- ## note:
152
- ## add converters after filters for now (why not before filters?)
153
- if converters ## any converters defined?
154
- ## convert single proc shortcut to array with single converter
155
- converters = [converters] if converters.is_a?( Proc )
156
-
157
- ## assumes array of procs
158
- converters.each do |converter|
159
- row = converter.call( row )
160
- end
161
- end
162
-
163
-
164
-
165
- team1 = row[ headers_mapping[ :team1 ]]
166
- team2 = row[ headers_mapping[ :team2 ]]
167
-
168
-
169
- ## check if data present - if not skip (might be empty row)
170
- if team1.nil? && team2.nil?
171
- puts "*** WARN: skipping empty? row[#{i}] - no teams found:"
172
- pp row
173
- next
174
- end
175
-
176
- ## remove possible match played counters e.g. (4) (11) etc.
177
- team1 = team1.sub( /\(\d+\)/, '' ).strip
178
- team2 = team2.sub( /\(\d+\)/, '' ).strip
179
-
180
-
181
-
182
- col = row[ headers_mapping[ :date ]]
183
- col = col.strip # make sure not leading or trailing spaces left over
184
-
185
- if col.empty? || col == '-' || col == '?'
186
- ## note: allow missing / unknown date for match
187
- date = nil
188
- else
189
- ## remove possible weekday or weeknumber e.g. (Fri) (4) etc.
190
- col = col.sub( /\(W?\d{1,2}\)/, '' ) ## e.g. (W11), (4), (21) etc.
191
- col = col.sub( /\(\w+\)/, '' ) ## e.g. (Fri), (Fr) etc.
192
- col = col.strip # make sure not leading or trailing spaces left over
193
-
194
- if col =~ /^\d{2}\/\d{2}\/\d{4}$/
195
- date_fmt = '%d/%m/%Y' # e.g. 17/08/2002
196
- elsif col =~ /^\d{2}\/\d{2}\/\d{2}$/
197
- date_fmt = '%d/%m/%y' # e.g. 17/08/02
198
- elsif col =~ /^\d{4}-\d{2}-\d{2}$/ ## "standard" / default date format
199
- date_fmt = '%Y-%m-%d' # e.g. 1995-08-04
200
- elsif col =~ /^\d{1,2} \w{3} \d{4}$/
201
- date_fmt = '%d %b %Y' # e.g. 8 Jul 2017
202
- else
203
- puts "*** !!! wrong (unknown) date format >>#{col}<<; cannot continue; fix it; sorry"
204
- ## todo/fix: add to errors/warns list - why? why not?
205
- exit 1
206
- end
207
-
208
- ## todo/check: use date object (keep string?) - why? why not?
209
- ## todo/fix: yes!! use date object!!!! do NOT use string
210
- date = Date.strptime( col, date_fmt ).strftime( '%Y-%m-%d' )
211
- end
212
-
213
-
214
- round = nil
215
- ## check for (optional) round / matchday
216
- if headers_mapping[ :round ]
217
- col = row[ headers_mapping[ :round ]]
218
- ## todo: issue warning if not ? or - (and just empty string) why? why not
219
- round = col.to_i if col =~ /^\d{1,2}$/ # check format - e.g. ignore ? or - or such non-numbers for now
220
- end
221
-
222
-
223
- score1 = nil
224
- score2 = nil
225
- score1i = nil
226
- score2i = nil
227
-
228
- ## check for full time scores ?
229
- if headers_mapping[ :score1 ] && headers_mapping[ :score2 ]
230
- ft = [ row[ headers_mapping[ :score1 ]],
231
- row[ headers_mapping[ :score2 ]] ]
232
-
233
- ## todo/fix: issue warning if not ? or - (and just empty string) why? why not
234
- score1 = ft[0].to_i if ft[0] =~ /^\d{1,2}$/
235
- score2 = ft[1].to_i if ft[1] =~ /^\d{1,2}$/
236
- end
237
-
238
- ## check for half time scores ?
239
- if headers_mapping[ :score1i ] && headers_mapping[ :score2i ]
240
- ht = [ row[ headers_mapping[ :score1i ]],
241
- row[ headers_mapping[ :score2i ]] ]
242
-
243
- ## todo/fix: issue warning if not ? or - (and just empty string) why? why not
244
- score1i = ht[0].to_i if ht[0] =~ /^\d{1,2}$/
245
- score2i = ht[1].to_i if ht[1] =~ /^\d{1,2}$/
246
- end
247
-
248
- ## check for all-in-one full time scores?
249
- if headers_mapping[ :score ]
250
- ft = row[ headers_mapping[ :score ] ]
251
- if ft =~ /^\d{1,2}[\-:]\d{1,2}$/ ## sanity check scores format
252
- scores = ft.split( /[\-:]/ )
253
- score1 = scores[0].to_i
254
- score2 = scores[1].to_i
255
- end
256
- ## todo/fix: issue warning if non-empty!!! and not matching format!!!!
257
- end
258
-
259
- if headers_mapping[ :scorei ]
260
- ht = row[ headers_mapping[ :scorei ] ]
261
- if ht =~ /^\d{1,2}[\-:]\d{1,2}$/ ## sanity check scores format
262
- scores = ht.split( /[\-:]/) ## allow 1-1 and 1:1
263
- score1i = scores[0].to_i
264
- score2i = scores[1].to_i
265
- end
266
- ## todo/fix: issue warning if non-empty!!! and not matching format!!!!
267
- end
268
-
269
-
270
- ## try some optional headings / columns
271
- stage = nil
272
- if headers_mapping[ :stage ]
273
- col = row[ headers_mapping[ :stage ]]
274
- ## todo/fix: check can col be nil e.g. col.nil? possible?
275
- stage = if col.nil? || col.empty? || col == '-' || col == 'n/a'
276
- ## note: allow missing stage for match / defaults to "regular"
277
- nil
278
- elsif col == '?'
279
- ## note: default explicit unknown to unknown for now AND not regular - why? why not?
280
- '?' ## todo/check: use unkown and NOT ? - why? why not?
281
- else
282
- col
283
- end
284
- end
285
-
286
-
287
- match = Import::Match.new( date: date,
288
- team1: team1, team2: team2,
289
- score1: score1, score2: score2,
290
- score1i: score1i, score2i: score2i,
291
- round: round,
292
- stage: stage )
293
- matches << match
294
- end
295
-
296
- ## pp matches
297
- matches
298
- end
299
-
300
-
301
- private
302
-
303
- def find_header( headers, candidates )
304
- ## todo/fix: use find_first from enumare of similar ?! - why? more idiomatic code?
305
-
306
- candidates.each do |candidate|
307
- return candidate if headers.include?( candidate ) ## bingo!!!
308
- end
309
- nil ## no matching header found!!!
310
- end
311
-
312
- end # class CsvMatchParser
313
- end # module SportDb
314
-