Class: Footty::Client

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/footty/client.rb

Constant Summary collapse

APIS =
{
  worldcup: 'https://github.com/openfootball/worldcup.json/raw/master/$year$/worldcup.json',
  euro:     'https://github.com/openfootball/euro.json/raw/master/$year$/euro.json'
}

Instance Method Summary collapse

Constructor Details

#initialize(league:, year:) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/footty/client.rb', line 9

def initialize( league:, year: )
  @worker = Fetcher::Worker.new

  @league = league
  @year   = year
end

Instance Method Details

#get_matchesObject

note:

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


24
25
26
27
28
29
30
31
# File 'lib/footty/client.rb', line 24

def get_matches
  @data ||= begin
              str = APIS[ @league.downcase.to_sym ]
              str = str.gsub( '$year$', @year.to_s )

              get( str )    ## use "memoized" / cached result
            end
end

#matches_for(date) ⇒ Object



48
49
50
51
52
# File 'lib/footty/client.rb', line 48

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

#past_matches(date: Date.today) ⇒ Object



62
63
64
65
66
67
# File 'lib/footty/client.rb', line 62

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

#round(num) ⇒ Object

for testing lets you use /round/1 etc.



37
38
39
40
41
# File 'lib/footty/client.rb', line 37

def round( num )
  h = get_matches
  matches = h[ 'rounds' ][ num-1 ]    ## note: rounds hash starts with zero (not 1)
  matches
end

#todays_matches(date: Date.today) ⇒ Object



44
# File 'lib/footty/client.rb', line 44

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

#tomorrows_matches(date: Date.today) ⇒ Object



45
# File 'lib/footty/client.rb', line 45

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

#upcoming_matches(date: Date.today) ⇒ Object



55
56
57
58
59
60
# File 'lib/footty/client.rb', line 55

def upcoming_matches( date: Date.today )
  ## note: includes todays matches for now
  hash  = get_matches
  matches = select_matches( hash[ 'rounds' ] ) { |match| date <= Date.parse( match['date'] ) }
  matches
end

#yesterdays_matches(date: Date.today) ⇒ Object



46
# File 'lib/footty/client.rb', line 46

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