Class: Footty::Dataset
- Inherits:
-
Object
- Object
- Footty::Dataset
- Defined in:
- lib/footty/dataset.rb
Constant Summary collapse
- SOURCES =
{ 'world' => { '2022' => [ 'worldcup/2022--qatar/cup.txt', 'worldcup/2022--qatar/cup_finals.txt'], '2018' => [ 'worldcup/2018--russia/cup.txt', 'worldcup/2018--russia/cup_finals.txt'] }, 'euro' => { '2024' => 'euro/2024--germany/euro.txt', '2021' => 'euro/2021--europe/euro.txt' }, 'de' => 'deutschland/$season$/1-bundesliga.txt', 'de2' => 'deutschland/$season$/2-bundesliga2.txt', 'en'=> 'england/$season$/1-premierleague.txt', 'es'=> 'espana/$season$/1-liga.txt', 'it'=> 'italy/$season$/1-seriea.txt', 'at'=> 'austria/$season$/1-bundesliga.txt', 'at2' => 'austria/$season$/2-liga2.txt', 'at3o' => 'austria/$season$/3-regionalliga-ost.txt', 'atcup' => 'austria/$season$/cup.txt', 'fr'=> 'europe/france/$season$/1-ligue1.txt', 'nl'=> 'europe/netherlands/$season$/1-eredivisie.txt', 'be'=> 'europe/belgium/$season$/1-firstdivisiona.txt', 'champs'=> 'champions-league/$season$/cl.txt', 'br' => 'south-america/brazil/$year$/1-seriea.txt', 'ar' => 'south-america/argentina/$year$/1-primeradivision.txt', 'co' => 'south-america/colombia/$year$/1-primeraa.txt', ## use a different code for copa libertadores? why? why not? 'copa' => 'south-america/copa-libertadores/$year$/libertadores.txt', 'mx' => 'mexico/$season$/1-ligamx.txt', 'eg' => 'africa/egypt/$season$/1-premiership.txt', 'ma' => 'africa/morocco/$season$/1-botolapro1.txt', }
Class Method Summary collapse
-
.leagues ⇒ Object
return built-in league keys.
Instance Method Summary collapse
- #end_date ⇒ Object
-
#initialize(league:, year: nil) ⇒ Dataset
constructor
A new instance of Dataset.
-
#matches ⇒ Object
note: cache ALL methods - only do one web request for match schedule & results.
- #matches_for(date) ⇒ Object
- #openfootball_url(path, year:) ⇒ Object
- #past_matches(date: Date.today) ⇒ Object
- #start_date ⇒ Object
- #todays_matches(date: Date.today) ⇒ Object
- #tomorrows_matches(date: Date.today) ⇒ Object
- #upcoming_matches(date: Date.today, limit: nil) ⇒ Object
- #yesterdays_matches(date: Date.today) ⇒ Object
Constructor Details
#initialize(league:, year: nil) ⇒ Dataset
Returns a new instance of Dataset.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 |
Class Method Details
.leagues ⇒ Object
return built-in league keys
44 |
# File 'lib/footty/dataset.rb', line 44 def self.leagues() SOURCES.keys; end |
Instance Method Details
#end_date ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# 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 ⇒ Object
note:
cache ALL methods - only do one web request for match schedule & results
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# 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) ⇒ Object
128 129 130 131 |
# 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:) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
146 147 148 149 150 151 |
# 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 ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
124 |
# File 'lib/footty/dataset.rb', line 124 def todays_matches( date: Date.today ) matches_for( date ); end |
#tomorrows_matches(date: Date.today) ⇒ Object
125 |
# 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) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# 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) ⇒ Object
126 |
# File 'lib/footty/dataset.rb', line 126 def yesterdays_matches( date: Date.today ) matches_for( date-1 ); end |