class Footty::Client

Constants

APIS

Public Class Methods

new( league:, year: ) click to toggle source
# File lib/footty/client.rb, line 9
def initialize( league:, year: )
  @worker = Fetcher::Worker.new

  @league = league
  @year   = year
end

Public Instance Methods

get_matches() click to toggle source

note:

cache ALL methods - only do one web request for match schedule & results
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source

for testing lets you use /round/1 etc.

# 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 ) click to toggle source
# File lib/footty/client.rb, line 44
def todays_matches( date: Date.today )      matches_for( date ); end
tomorrows_matches( date: Date.today ) click to toggle source
# File lib/footty/client.rb, line 45
def tomorrows_matches( date: Date.today )   matches_for( date+1 );  end
upcoming_matches( date: Date.today ) click to toggle source
# 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 ) click to toggle source
# File lib/footty/client.rb, line 46
def yesterdays_matches( date: Date.today )  matches_for( date-1 );  end

Private Instance Methods

get( str ) click to toggle source
# File lib/footty/client.rb, line 88
def get( str )
  response = @worker.get_response( str )

  if response.code == '200'
    ##
    ## fix/fix/todo/check:
    ##  do we need to force utf-8 encoding? yes!!!!
    ##   check for teams w/ non-ascii names
    hash = JSON.parse( response.body )
    ## pp hash
    hash
  else
    logger.error "fetch HTTP - #{response.code} #{response.message}"
    nil
  end
end
select_matches( rounds ) { |match| ... } click to toggle source
# File lib/footty/client.rb, line 71
def select_matches( rounds )
  matches = []
  rounds.each do |round|
    round['matches'].each do |match|
      if yield( match )
        ## hack: add (outer) round to match
        match['round'] = round['name']
        matches << match
      else
        ## puts " skipping game   play_date #{play_date}"
      end
    end
  end
  matches
end