Class: SportDb::CsvGameReader
- Inherits:
-
Object
- Object
- SportDb::CsvGameReader
- Includes:
- LogUtils::Logging, Models
- Defined in:
- lib/sportdb/csv_reader.rb
Class Method Summary collapse
-
.from_string(event_key, text) ⇒ Object
todo: add from_file and from_zip too.
Instance Method Summary collapse
- #handle_game(date_str, team1_str, team2_str, ft_str, ht_str) ⇒ Object
- #handle_round(round_pos_str) ⇒ Object
-
#initialize(event_key, text) ⇒ CsvGameReader
constructor
A new instance of CsvGameReader.
- #parse_fixtures ⇒ Object
- #read ⇒ Object
Constructor Details
#initialize(event_key, text) ⇒ CsvGameReader
Returns a new instance of CsvGameReader.
24 25 26 27 28 29 30 31 |
# File 'lib/sportdb/csv_reader.rb', line 24 def initialize( event_key, text ) ### fix - fix -fix: ## change event to event_or_event_key !!!!! - allow event_key as string passed in ## todo/fix: how to add opts={} ??? @event_key = event_key @text = text end |
Class Method Details
.from_string(event_key, text) ⇒ Object
todo: add from_file and from_zip too
18 19 20 21 22 |
# File 'lib/sportdb/csv_reader.rb', line 18 def self.from_string( event_key, text ) ### fix - fix -fix: ## change event to event_or_event_key !!!!! - allow event_key as string passed in self.new( event_key, text ) end |
Instance Method Details
#handle_game(date_str, team1_str, team2_str, ft_str, ht_str) ⇒ Object
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/sportdb/csv_reader.rb', line 87 def handle_game( date_str, team1_str, team2_str, ft_str, ht_str ) ## todo/fix: make more "efficient" ## - e.g. add new support method for mapping single team/reference - why? why not?? line = "#{team1_str} - #{team2_str}" @team_mapper.map_titles!( line ) team1_key = @team_mapper.find_key!( line ) team2_key = @team_mapper.find_key!( line ) ## note: if we do NOT find two teams; return false - no match found if team1_key.nil? || team2_key.nil? fail " no game match (two teams required) found for line: >#{line}<" end if date_str date = DateTime.strptime( date_str, '%Y-%m-%d' ) ## (always) use DateTime - why? why not?? @last_date = date # keep a reference for later use else date = @last_date # no date found; (re)use last seen date end ## ## todo: support for awarded, abadoned, a.e.t, pen. etc. - why?? why not?? ## if ht_str ## half time scores scoresi_str = ht_str.gsub(/ /, '').split('-') ## note: remove all (inline) spaces first score1i = scoresi_str[0].to_i score2i = scoresi_str[1].to_i else score1i = nil score2i = nil end if ft_str ## full time scores scores_str = ft_str.gsub(/ /, '').split('-') ## note: remove all (inline) spaces first score1 = scores_str[0].to_i score2 = scores_str[1].to_i else score1 = nil score2 = nil end ### todo: cache team lookups in hash? - why? why not?? team1 = Team.find_by!( key: team1_key ) team2 = Team.find_by!( key: team2_key ) round = @last_round ### check if games exists ## with this teams in this round if yes only update game = Game.find_by( round_id: round.id, team1_id: team1.id, team2_id: team2.id ) game_attribs = { score1: score1, score2: score2, score1i: score1i, score2i: score2i, play_at: date, play_at_v2: nil, postponed: false, knockout: false, ## round.knockout, ## note: for now always use knockout flag from round - why? why not?? ground_id: nil, group_id: nil } if game.present? logger.debug "update game #{game.id}:" else logger.debug "create game:" game = Game.new ## Note: use round.games.count for pos ## lets us add games out of order if later needed more_game_attribs = { round_id: round.id, team1_id: team1.id, team2_id: team2.id, pos: round.games.count+1 } game_attribs = game_attribs.merge( more_game_attribs ) end logger.debug game_attribs.to_json game.update_attributes!( game_attribs ) end |
#handle_round(round_pos_str) ⇒ Object
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 |
# File 'lib/sportdb/csv_reader.rb', line 52 def handle_round( round_pos_str ) round_pos = round_pos_str.to_i round_attribs = { } round = Round.find_by( event_id: @event.id, pos: round_pos ) if round.present? logger.debug "update round #{round.id}:" else logger.debug "create round:" round = Round.new round_attribs = round_attribs.merge( { event_id: @event.id, pos: round_pos, title: "Round #{round_pos}", title2: nil, knockout: false, start_at: Date.parse('1911-11-11'), end_at: Date.parse('1911-11-11') }) end logger.debug round_attribs.to_json round.update_attributes!( round_attribs ) ### store list of round ids for patching start_at/end_at at the end @patch_round_ids << round.id @last_round = round ## keep track of last seen round for matches that follow etc. end |
#parse_fixtures ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/sportdb/csv_reader.rb', line 178 def parse_fixtures CSV.parse( @text, headers: true ) do |row| puts row.inspect pp round = row['Round'] pp date = row['Date'] pp team1 = row['Team 1'] pp team2 = row['Team 2'] pp ft = row['FT'] pp ht = row['HT'] ## find round by pos if round handle_round( round ) handle_game( date, team1, team2, ft, ht ) else fail "round required for import; sorry" end end ########################### # backtrack and patch round dates (start_at/end_at) unless @patch_round_ids.empty? ### # note: use uniq - to allow multiple round headers (possible?) Round.find( @patch_round_ids.uniq ).each do |r| logger.debug "patch round start_at/end_at date for #{r.title}:" ## note: ## will add "scope" pos first e.g # ## SELECT "games".* FROM "games" WHERE "games"."round_id" = ? # ORDER BY pos, play_at asc [["round_id", 7]] # thus will NOT order by play_at but by pos first!!! # => # need to unscope pos!!! or use unordered_games - games_by_play_at_date etc.?? # thus use reorder()!!! - not just order('play_at asc') games = r.games.reorder( 'play_at asc' ).all ## skip rounds w/ no games ## todo/check/fix: what's the best way for checking assoc w/ 0 recs? next if games.size == 0 # note: make sure start_at/end_at is date only (e.g. use play_at.to_date) # sqlite3 saves datetime in date field as datetime, for example (will break date compares later!) round_attribs = { start_at: games[0].play_at.to_date, # use games.first ? end_at: games[-1].play_at.to_date # use games.last ? why? why not? } logger.debug round_attribs.to_json r.update_attributes!( round_attribs ) end end end |
#read ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sportdb/csv_reader.rb', line 34 def read ## note: assume active activerecord connection @event = Event.find_by!( key: @event_key ) logger.debug "Event #{@event.key} >#{@event.title}<" @team_mapper = TextUtils::TitleMapper.new( @event.teams, 'team' ) ## reset cached values @patch_round_ids = [] @last_round = nil ## remove last round ?? - always required - why? why not? @last_date = nil ## remove last date ?? - always required - why? why not? parse_fixtures end |