Module: SportDb

Defined in:
lib/sportdb/importers.rb,
lib/sportdb/importers/event.rb,
lib/sportdb/importers/match.rb,
lib/sportdb/importers/version.rb

Defined Under Namespace

Modules: Module Classes: CsvEventImporter, CsvMatchImporter, Package

Class Method Summary collapse

Class Method Details

.handle_csv(source, start: nil) ⇒ Object

helper

move handle_csv somewhere else - why? why not?


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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sportdb/importers.rb', line 45

def self.handle_csv( source, start: nil )
  ## todo/fix:  (re)use a more generic filter instead of start for start of season only

  ##  todo/fix: use a "generic" filter_season helper for easy reuse
  ##     filter_season( clause, season_key )
  ##   or better filter = SeasonFilter.new( clause )
  ##             filter.skip? filter.include? ( season_sason_key )?
  ##             fiteer.before?( season_key )  etc.
  ##              find some good method names!!!!
  season_start = start ? Season.parse( start ) : nil

  if source.is_a?( Datafile::DirPackage::Entry) ||
     source.is_a?( Datafile::ZipPackage::Entry)
    entry = source

    basename = File.basename( entry.name, File.extname( entry.name ) )  ## get basename WITHOUT extension

    ## check if basename is all numbers (and _-) e.g. 2020.csv or 20.csv etc.
    ##   if yes, assume "mixed" match datafiles (with many/mixed leagues)
    if basename =~ /^[0-9_-]+$/
      pp [entry.name, basename]

      CsvMatchImporter.parse( entry.read )
    else  ## assume "classic" with season
      league_key = basename

      ## todo/fix: check if season_key is proper season - e.g. matches pattern !!!!
      season_q   = File.basename( File.dirname( entry.name ))
      season     = Season.parse( season_q )  ## normalize season
      season_key = season.key

      if season_start && season_start > season
        ## skip if start season before this season
      else
        pp [entry.name, season_key, league_key]

        event = CsvEventImporter.parse( entry.read, league:  league_key,
                                                    season:  season_key )

        puts "added #{event.name} - from source >#{entry.name}<"
        puts "  #{event.teams.size} teams"
        puts "  #{event.matches.size} matches"
        puts "  #{event.rounds.size} rounds"
      end
    end
  else  ## assume (string) filepath for now  - add more options later on!!!!
    ## assume single (free-standing) file
    path = source
    full_path = File.expand_path( path )   ## resolve/make path absolute
    ## 1) assume basename is the league key
    ## 2) assume last directory is the season key

    basename = File.basename( full_path, File.extname( full_path ) )  ## get basename WITHOUT extension
    if basename =~ /^[0-9_-]+$/
      pp [path, basename]
      CsvMatchImporter.read( full_path )
    else ## assume "classic" with season
      ## 1) assume basename is the league key
      ## 2) assume last directory is the season key
      league_key = basename

      season_q   = File.basename( File.dirname( full_path ) )
      season     = Season.parse( season_q )  ## normalize season
      season_key = season.key

      if season_start && season_start > season
        ## skip if start season before this season
      else
        ## todo/fix: check if season_key is proper season - e.g. matches pattern !!!!
        pp [path, season_key, league_key]

        event = CsvEventImporter.read( full_path, league: league_key,
                                                  season: season_key )

        puts "added #{event.name} - from source >#{path}<"
        puts "  #{event.teams.size} teams"
        puts "  #{event.matches.size} matches"
        puts "  #{event.rounds.size} rounds"
      end
    end
  end
end

.read_csv(path) ⇒ Object

add convenience shortcut helper



31
32
33
34
35
36
37
38
39
40
# File 'lib/sportdb/importers.rb', line 31

def self.read_csv( path )
  if File.directory?( path )          ## if directory assume "unzipped" package
    DirPackage.new( path ).read_csv
  elsif File.file?( path ) && File.extname( path ).downcase == '.zip'   ## check if file is a .zip (archive) file
    ZipPackage.new( path ).read_csv
  else                                ## no package; assume single (standalone) datafile
    ## assume single (free-standing) file
    handle_csv( path )
  end
end