Class: SportDb::CsvMatchImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/importers/match.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt, headers: nil) ⇒ CsvMatchImporter

Returns a new instance of CsvMatchImporter.



16
17
18
19
# File 'lib/sportdb/importers/match.rb', line 16

def initialize( txt, headers: nil )
  @txt     = txt
  @headers = headers
end

Class Method Details

.parse(txt, headers: nil) ⇒ Object



11
12
13
# File 'lib/sportdb/importers/match.rb', line 11

def self.parse( txt, headers: nil )
  new( txt, headers: headers ).parse
end

.read(path, headers: nil) ⇒ Object



6
7
8
9
# File 'lib/sportdb/importers/match.rb', line 6

def self.read( path, headers: nil )
  txt = File.open( path, 'r:utf-8' ) {|f| f.read }
  parse( txt, headers: headers )
end

Instance Method Details

#parseObject



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
# File 'lib/sportdb/importers/match.rb', line 22

def parse
  ## todo/fix: add headers options (pass throughto CsvMatchReader)
  ##    add filters too why? why not?

  ##  todo/fix:
  ##     add normalize: false/mapping: false  flag for NOT mapping club/team names
  ##       make normalize: false the default, anyways - why? why not?
  opts = {}
  opts[:headers] = @headers  if @headers

  matches = CsvMatchParser.parse( @txt, **opts )

  matches.each do |match|
    league = Import.catalog.leagues.find!( match.league )
    # pp league

    team1  = Import.catalog.teams.find_by!( name: match.team1, league: league )
    team2  = Import.catalog.teams.find_by!( name: match.team2, league: league )

    date =  Date.strptime( match.date, '%Y-%m-%d' )
    ## quick hack - for now always use 2019/20 style season
    ##   fix!!! - use league to find proper season e.g. 2019 or 2019/20 etc.

    start_year = if date.month >= 7
                   date.year
                 else
                   date.year-1
                 end

    ## note: for now always assume 2019/20 season
    season = Season.parse( '%d/%d' % [start_year, (start_year+1)%100] )
    # pp season


    event_rec = Sync::Event.find_or_create_by( league: league,
                                               season: season )

    team1_rec = Sync::Team.find_or_create( team1 )
    team2_rec = Sync::Team.find_or_create( team2 )

    ## warn about duplicates?
    ##  note: for now only allow one (unique) match pair per team
    match_recs = Model::Match.where( event_id: event_rec.id,
                                     team1_id: team1_rec.id,
                                     team2_id: team2_rec.id ).to_a
    if match_recs.size > 0
      puts "!! #{match_recs.size} duplicate match record(s) found:"
      pp match_recs
      exit 1
    end

    ## find last pos - check if it can be nil?  yes, is nil if no records found
    max_pos = Model::Match.where( event_id: event_rec.id ).maximum( 'pos' )
    max_pos = max_pos ? max_pos+1 : 1

    rec = Model::Match.create!(
            event_id: event_rec.id,
            team1_id: team1_rec.id,
            team2_id: team2_rec.id,
            ## round_id: round_rec.id,  -- note: now optional
            pos:      max_pos,
            date:     date.to_date,
            score1:   match.score1,
            score2:   match.score2,
            score1i:  match.score1i,
            score2i:  match.score2i )

    ## todo/fix:
    ##   check if event includes teams?
    ##    if not (auto-)add teams to event.teams !!!!!!!
  end
end