Class: SportDb::QuickLeagueOutlineReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/quick/quick_league_outline_reader.rb

Overview

shared “higher-level” outline reader

quick version WITHOUT any validation/mapping !!!

Constant Summary collapse

LEAGUE_SEASON_HEADING_RE =

split into league + season

e.g. Österr. Bundesliga 2015/16   ## or 2015-16
     World Cup 2018
%r{^
         (?<league>.+?)     ## non-greedy
\s+
         (?<season>\d{4}
(?:[\/-]\d{1,4})?     ## optional 2nd year in season
         )
$}x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ QuickLeagueOutlineReader

Returns a new instance of QuickLeagueOutlineReader.



20
21
22
# File 'lib/sportdb/quick/quick_league_outline_reader.rb', line 20

def initialize( txt )
  @txt = txt
end

Class Method Details

.parse(txt) ⇒ Object



15
16
17
# File 'lib/sportdb/quick/quick_league_outline_reader.rb', line 15

def self.parse( txt )
  new( txt ).parse
end

.read(path) ⇒ Object



10
11
12
13
# File 'lib/sportdb/quick/quick_league_outline_reader.rb', line 10

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

Instance Method Details

#parseObject



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

def parse
  secs=[]     # sec(tion)s
  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_RE )
        puts "league >#{m[:league]}<, season >#{m[:season]}<"

         secs << { 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] == :h2
       ## todo/check - make sure parsed h1 first
       heading = node[1]
       ## reuse league, season from h1
       secs << { league: secs[-1][:league],
                 season: secs[-1][:season],
                 stage:  heading,
                 lines:  []
               }
    elsif node[0] == :p   ## paragraph with (text) lines
      lines = node[1]
      ## note: skip lines if no heading seen
      if secs.empty?
        puts "** !!! WARN - skipping lines (no heading):"
        pp lines
      else
        ## todo/check: unroll paragraphs into lines or pass along paragraphs - why? why not?
        secs[-1][:lines] += lines
      end
    else
      puts "** !!! ERROR - unknown line type; for now only heading 1 for leagues supported; sorry:"
      pp node
      exit 1
    end
  end
  secs
end

#split_league(str) ⇒ Object

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



84
85
86
87
88
89
90
91
92
# File 'lib/sportdb/quick/quick_league_outline_reader.rb', line 84

def 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