class FootballCli::Handler

Attributes

client[R]
command[R]
file[R]
fixtures[R]
format[R]
league[R]
match_day[R]
players[R]
team[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/football_cli/handler.rb, line 12
def initialize(options={})
  @league = options[:league]
  @match_day = options[:match_day]
  @players = options[:players]
  @fixtures = options[:fixtures]
  @team = options[:team]
  @file = options[:file]
  @format = options.fetch(:format, 'table')
end

Public Instance Methods

league_table() click to toggle source
# File lib/football_cli/handler.rb, line 36
def league_table
  response = client.league_table(league_id, match_day: match_day)

  output(
    title: response[:leagueCaption],
    response: response[:standing],
    columns: %i(position teamName playedGames goalDifference points),
    qualification: qualification
  )
end
live_scores() click to toggle source
# File lib/football_cli/handler.rb, line 69
def live_scores
  response = client.live_scores

  output(
    title: 'Live scores',
    response: response[:games],
    columns: %i(league homeTeamName goalsHomeTeam goalsAwayTeam awayTeamName time)
  )
end
run() click to toggle source
# File lib/football_cli/handler.rb, line 22
def run
  check_for_api_token

  if league
    league_table
  elsif team
    if players
      team_players
    elsif fixtures
      team_fixtures
    end
  end
end
team_fixtures() click to toggle source
# File lib/football_cli/handler.rb, line 58
def team_fixtures
  response = client.team_fixtures(team_id)
  team_response = client.team(team_id)

  output(
    title: "#{team_response[:name]} fixtures. Total #{response[:count]}",
    response: response[:fixtures],
    columns: %i(matchday homeTeamName goalsHomeTeam goalsAwayTeam awayTeamName status date)
  )
end
team_players() click to toggle source
# File lib/football_cli/handler.rb, line 47
def team_players
  response = client.team_players(team_id)
  team_response = client.team(team_id)

  output(
    title: "#{team_response[:name]} players. Total #{response[:count]}",
    response: response[:players],
    columns: %i(jerseyNumber name position nationality dateOfBirth)
  )
end

Private Instance Methods

check_for_api_token() click to toggle source
# File lib/football_cli/handler.rb, line 114
def check_for_api_token
  raise Thor::Error ,"ERROR: You must config the api_token\nSOLUTION: Set up with `config api_token TOKEN`" if Configuration.api_token.to_s.empty?
end
league_id() click to toggle source
# File lib/football_cli/handler.rb, line 83
def league_id
  get_league_id(league)
end
output(opts = {}) click to toggle source
# File lib/football_cli/handler.rb, line 95
def output(opts = {})
  FootballCli::Format::FormatFactory.build(
    format,
    {
      qualification: opts[:qualification],
      response: opts[:response],
      columns: opts[:columns],
      title: opts[:title],
      file_name: file
    }
  ).output
end
qualification() click to toggle source
# File lib/football_cli/handler.rb, line 91
def qualification
  get_qualification(league)
end
team_id() click to toggle source
# File lib/football_cli/handler.rb, line 87
def team_id
  get_team_id(team)
end