class Footty::Dataset

Constants

SOURCES

Public Class Methods

leagues() click to toggle source

return built-in league keys

# File lib/footty/dataset.rb, line 44
def self.leagues()  SOURCES.keys; end
new( league:, year: nil ) click to toggle source
# File lib/footty/dataset.rb, line 47
def initialize( league:,  year: nil )
  @league = league

  spec = SOURCES[ @league.downcase ]
  urls = if spec.is_a?( Hash )  ## assume lookup by year
            if year.nil?  ## lookup default first entry
                 year = spec.keys[0].to_i
                 spec.values[0]
            else
                 spec[ year.to_s ]
            end
          else  ## assume vanilla urls (no lookup by year)
               ## default to 2024 for now
               year = 2024  if year.nil?
               spec
          end
  raise ArgumentError, "no dataset (source) for league #{league} found"    if urls.nil?

  ## wrap single sources (strings) in array
  urls = urls.is_a?( Array ) ? urls : [urls]
  ## expand shortened url and fill-in template vars
  @urls = urls.map { |url| openfootball_url( url, year: year ) }
end

Public Instance Methods

end_date() click to toggle source
# File lib/footty/dataset.rb, line 100
def end_date
   @end_date ||= begin
                    end_date = nil
                    matches.each do |match|
                       date = Date.strptime(match['date'], '%Y-%m-%d' )
                       end_date = date  if end_date.nil? ||
                                           date > end_date
                    end
                    end_date
                 end
end
matches() click to toggle source

note:

cache ALL methods - only do one web request for match schedule & results
# File lib/footty/dataset.rb, line 85
def matches
  @data ||= begin
              matches = []
              @urls.each do |url|
                txt = get!( url )    ## use "memoized" / cached result
                matches += SportDb::QuickMatchReader.parse( txt )
              end
              data = matches.map {|match| match.as_json }  # convert to json
              ## quick hack to get keys as strings not symbols!!
              ##  fix upstream
              JSON.parse( JSON.generate( data ))
            end
end
matches_for( date ) click to toggle source
# File lib/footty/dataset.rb, line 128
def matches_for( date )
  matches = select_matches { |match| date == Date.parse( match['date'] ) }
  matches
end
openfootball_url( path, year: ) click to toggle source
# File lib/footty/dataset.rb, line 72
def openfootball_url( path, year: )
   repo, local_path = path.split( '/',  2)
   url = "https://raw.githubusercontent.com/openfootball/#{repo}/master/#{local_path}"
   ## check for template vars too
   season = Season( "#{year}/#{year+1}" )
   url = url.gsub( '$year$', year.to_s )
   url = url.gsub( '$season$', season.to_path )
   url
end
past_matches( date: Date.today ) click to toggle source
# File lib/footty/dataset.rb, line 146
def past_matches( date: Date.today )
  matches = select_matches { |match| date > Date.parse( match['date'] ) }
  ## note reveserve matches (chronological order/last first)
  ## matches.reverse
  matches
end
start_date() click to toggle source
# File lib/footty/dataset.rb, line 112
def start_date
   @start_date ||= begin
                    start_date = nil
                    matches.each do |match|
                       date = Date.strptime(match['date'], '%Y-%m-%d' )
                       start_date = date  if start_date.nil? ||
                                           date < start_date
                    end
                    start_date
                   end
end
todays_matches( date: Date.today ) click to toggle source
# File lib/footty/dataset.rb, line 124
def todays_matches( date: Date.today )      matches_for( date ); end
tomorrows_matches( date: Date.today ) click to toggle source
# File lib/footty/dataset.rb, line 125
def tomorrows_matches( date: Date.today )   matches_for( date+1 );  end
upcoming_matches( date: Date.today, limit: nil ) click to toggle source
# File lib/footty/dataset.rb, line 134
def upcoming_matches( date: Date.today,
                      limit: nil )
  ## note: includes todays matches for now
  matches = select_matches { |match| date <= Date.parse( match['date'] ) }

  if limit
    matches[0, limit]  ## cut-off
  else
    matches
  end
end
yesterdays_matches( date: Date.today ) click to toggle source
# File lib/footty/dataset.rb, line 126
def yesterdays_matches( date: Date.today )  matches_for( date-1 );  end

Private Instance Methods

get!( url ) click to toggle source
# File lib/footty/dataset.rb, line 168
def get!( url )
    ##  use cached urls for 12h by default
    ##  if expired in cache (or not present) than get/fetch
   if Webcache.expired_in_12h?( url )
     response = Webget.text( url )

     if response.status.ok?
       response.text   # note - return text (utf-8)
     else
       ## dump headers
       response.headers.each do |key,value|
         puts "   #{key}:  #{value}"
       end
       puts "!! HTTP ERROR - #{response.status.code} #{response.status.message}"
       exit 1
     end
   else
     Webcache.read( url )
   end
end
select_matches( &blk) click to toggle source
# File lib/footty/dataset.rb, line 155
def select_matches( &blk)
  selected = []
  matches.each do |match|
     selected << match   if blk.call( match )
  end

  ## todo/fix:
  ##  sort matches here; might not be chronologicial (by default)
  selected
end