Class: Footty::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/footty/dataset.rb

Constant Summary collapse

APIS =
{
  world:    'https://raw.githubusercontent.com/openfootball/worldcup.json/master/$year$/worldcup.json',
  euro:     'https://raw.githubusercontent.com/openfootball/euro.json/master/$year$/euro.json',
  de:       'https://raw.githubusercontent.com/openfootball/deutschland/master/$season$/1-bundesliga.txt',
  en:       'https://raw.githubusercontent.com/openfootball/england/master/$season$/1-premierleague.txt',
  at:       'https://raw.githubusercontent.com/openfootball/austria/master/$season$/1-bundesliga-i.txt',
}

Instance Method Summary collapse

Constructor Details

#initialize(league:, year:) ⇒ Dataset

Returns a new instance of Dataset.



7
8
9
10
11
# File 'lib/footty/dataset.rb', line 7

def initialize( league:, year: )
  @league = league
  @year   = year
  @season = Season( "#{year}/#{year+1}" )
end

Instance Method Details

#end_dateObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/footty/dataset.rb', line 43

def end_date
   @end_date ||= begin
                    end_date = nil
                    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

#matchesObject

note:

cache ALL methods - only do one web request for match schedule & results


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/footty/dataset.rb', line 24

def matches
  @data ||= begin
              url = APIS[ @league.downcase.to_sym ]
              url = url.gsub( '$year$', @year.to_s )
              url = url.gsub( '$season$', @season.to_path )

              res = get!( url )    ## use "memoized" / cached result
              if url.end_with?( '.json' )
                  JSON.parse( res.text )
              else  ## assume football.txt format
                  matches = SportDb::QuickMatchReader.parse( res.text )
                  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
end

#matches_for(date) ⇒ Object



71
72
73
74
# File 'lib/footty/dataset.rb', line 71

def matches_for( date )
  matches = select_matches { |match| date == Date.parse( match['date'] ) }
  matches
end

#past_matches(date: Date.today) ⇒ Object



89
90
91
92
93
# File 'lib/footty/dataset.rb', line 89

def past_matches( date: Date.today )
  matches = select_matches { |match| date > Date.parse( match['date'] ) }
  ## note reveserve matches (chronological order/last first)
  matches.reverse
end

#start_dateObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/footty/dataset.rb', line 55

def start_date
   @start_date ||= begin
                    start_date = nil
                    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



67
# File 'lib/footty/dataset.rb', line 67

def todays_matches( date: Date.today )      matches_for( date ); end

#tomorrows_matches(date: Date.today) ⇒ Object



68
# File 'lib/footty/dataset.rb', line 68

def tomorrows_matches( date: Date.today )   matches_for( date+1 );  end

#upcoming_matches(date: Date.today, limit: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/footty/dataset.rb', line 77

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



69
# File 'lib/footty/dataset.rb', line 69

def yesterdays_matches( date: Date.today )  matches_for( date-1 );  end