Class: SportDb::LeagueOutlineReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/readers/outline_reader.rb

Overview

shared “higher-level” outline reader

todo: add CountryOutlineReader - why? why not?

Constant Summary collapse

LEAGUE_SEASON_HEADING_REGEX =

split into league + season

e.g. 
/^
         (?<league>.+?)     ## non-greedy
\s+
         (?<season>\d{4}
(?:[\/-]\d{2})?     ## optional 2nd year in season
         )
$/x

Class Method Summary collapse

Class Method Details

.check_stage(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sportdb/readers/outline_reader.rb', line 81

def self.check_stage( name )
  known_stages = ['regular season',
                  'championship round',
                  'relegation round',
                 ]

  if known_stages.include?( name.downcase )
     ## everything ok

  else
    puts "** !!! ERROR !!! no (league) stage match found for >#{name}<, add to (builtin) stages table; sorry"
    exit 1
  end
end

.configObject

shortcut convenience helper



11
# File 'lib/sportdb/readers/outline_reader.rb', line 11

def self.config() Import.config; end

.find_league(name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sportdb/readers/outline_reader.rb', line 96

def self.find_league( name )
  league = nil
  m = config.leagues.match( name )
  # pp m


  if m.nil?
    puts "** !!! ERROR !!! no league match found for >#{name}<, add to leagues table; sorry"
    exit 1
  elsif m.size > 1
    puts "** !!! ERROR !!! ambigious league name; too many leagues (#{m.size}) found:"
    pp m
    exit 1
  else
    league = m[0]
  end

  league
end

.parse(txt) ⇒ Object



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
# File 'lib/sportdb/readers/outline_reader.rb', line 24

def self.parse( txt )
  recs=[]
  OutlineReader.parse( txt ).each do |node|
    if node[0] == :h1
      ## check for league (and stage) and season

      heading = node[1]
      values = split_league( heading )
      if m=values[0].match( LEAGUE_SEASON_HEADING_REGEX )
        puts "league >#{m[:league]}<, season >#{m[:season]}<"

         recs << { league: m[:league],
                   season: m[:season],
                   stage:  values[1],     ## note: defaults to nil if not present

                   lines:  []
                 }
      else
        puts "** !!! ERROR !!! - CANNOT match league and season in heading; season missing?"
        pp heading
        exit 1
      end
    elsif node[0] == :l   ## regular (text) line

      line = node[1]
      ## note: skip lines if no heading seen

      if recs.empty?
        puts "** !! WARN !! - skipping line (no heading) >#{line}<"
      else
        recs[-1][:lines] << line
      end
    else
      puts "** !!! ERROR !!! unknown line type; for now only heading 1 for leagues supported; sorry:"
      pp node
      exit 1
    end
  end

  ## pass 2 - check & map; replace inline (string with data record)

  recs.each do |rec|
    league = find_league( rec[:league] )
    rec[:league] = league

    check_stage( rec[:stage] )   if rec[:stage]   ## note: only check for now (no remapping etc.)

  end

  recs
end

.split_league(str) ⇒ Object

todo/check: rename to parse_league(s) - why? why not?



71
72
73
74
75
76
77
78
79
# File 'lib/sportdb/readers/outline_reader.rb', line 71

def self.split_league( str )   ## todo/check: rename to parse_league(s) - why? why not?

  ## split into league / stage / ... e.g.

  ##  => Österr. Bundesliga 2018/19, Regular Season

  ##  => Österr. Bundesliga 2018/19, Championship Round

  ##  etc.

  values = str.split( /[,<>‹›]/ )  ## note: allow , > < or › ‹ for now

  values = values.map { |value| value.strip }   ## remove all whitespaces

  values
end