Class: SportDb::Import::EventInfoReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sportdb/helpers/event_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ EventInfoReader

Returns a new instance of EventInfoReader.



15
16
17
# File 'lib/sportdb/helpers/event_reader.rb', line 15

def initialize( txt )
  @txt = txt
end

Class Method Details

.parse(txt) ⇒ Object



11
12
13
# File 'lib/sportdb/helpers/event_reader.rb', line 11

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

.read(path) ⇒ Object



6
7
8
9
# File 'lib/sportdb/helpers/event_reader.rb', line 6

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

Instance Method Details

#parseObject



19
20
21
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sportdb/helpers/event_reader.rb', line 19

def parse
  recs = []

  parse_csv( @txt ).each do |row|
    league_col = row['League']
    season_col = row['Season'] || row['Year']
    dates_col  = row['Dates']

    season = Season.parse( season_col )
    league = League.find!( league_col )


    dates = []
    if dates_col.nil? || dates_col.empty?
       ## do nothing; no dates - keep dates array empty
    else
      ## squish spaces
      dates_col = dates_col.gsub( /[ ]{2,}/, ' ' )  ## squish/fold spaces

      puts "#{league.name} (#{league.key}) | #{season.key} | #{dates_col}"

      ### todo/check:  check what parts "Aug 15" return ???
      ###                       short form for "Aug 15 -" - works?

      ## todo/fix!!! - check EventInfo.include?
      ##    now allow dates with only start_date too!! (WITHOUT end_date)
      parts = dates_col.split( /[ ]*[–-][ ]*/ )
      if parts.size == 1
        pp parts
        dates << DateFormats.parse( parts[0], start: Date.new( season.start_year, 1, 1 ), lang: 'en' )
        pp dates
      elsif parts.size == 2
        pp parts
        dates << DateFormats.parse( parts[0], start: Date.new( season.start_year, 1, 1 ), lang: 'en' )
        dates << DateFormats.parse( parts[1], start: Date.new( season.end_year ? season.end_year : season.start_year, 1, 1 ), lang: 'en' )
        pp dates

        ## assert/check if period is less than 365 days for now
        diff = dates[1].to_date.jd - dates[0].to_date.jd
        puts "#{diff}d"
        if diff > 365
          puts "!! ERROR - date range / period assertion failed; expected diff < 365 days"
          exit 1
        end
      else
        puts "!! ERRROR - expected data range / period - one or two dates; got #{parts.size}:"
        pp dates_col
        pp parts
        exit 1
      end
    end


    teams_col    = row['Clubs'] || row['Teams']
    goals_col    = row['Goals']

    ## note: remove (and allow) all non-digits e.g. 370 goals, 20 clubs, etc.
    teams_col    = teams_col.gsub( /[^0-9]/, '' )    if teams_col
    goals_col    = goals_col.gsub( /[^0-9]/, '' )    if goals_col

    teams  =   (teams_col.nil? || teams_col.empty?) ? nil : teams_col.to_i
    goals  =   (goals_col.nil? || goals_col.empty?) ? nil : goals_col.to_i

    matches_col  = row['Matches']
    ## note: support additions in matches (played) e.g.
    #   132 + 63 Play-off-Spiele
    matches_col   = matches_col.gsub( /[^0-9+]/, '' )  if matches_col

    matches =  if matches_col.nil? || matches_col.empty?
                 nil
               else
                 if matches_col.index( '+' )  ### check for calculations
                    ## note: for now only supports additions
                    matches_col.split( '+' ).reduce( 0 ) do |sum,str|
                      sum + str.to_i
                    end
                 else            ## assume single (integer) number
                    matches_col.to_i
                 end
              end

    rec = EventInfo.new( league:     league,
                         season:     season,
                         start_date: dates[0],
                         end_date:   dates[1],
                         teams:      teams,
                         matches:    matches,
                         goals:      goals
                       )
    recs << rec
  end  # each row
  recs
end