Class: SportDb::Import::EventInfoReader

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(txt) ⇒ EventInfoReader

Returns a new instance of EventInfoReader.



81
82
83
# File 'lib/sportdb/formats/event/event_reader.rb', line 81

def initialize( txt )
  @txt = txt
end

Class Method Details

.parse(txt) ⇒ Object



77
78
79
# File 'lib/sportdb/formats/event/event_reader.rb', line 77

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

.read(path) ⇒ Object



72
73
74
75
# File 'lib/sportdb/formats/event/event_reader.rb', line 72

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

Instance Method Details

#catalogObject



69
# File 'lib/sportdb/formats/event/event_reader.rb', line 69

def catalog() Import.catalog; end

#parseObject



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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/sportdb/formats/event/event_reader.rb', line 85

def parse
  recs = []

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

    season = Import::Season.parse( season_col )
    league = catalog.leagues.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