class CFB::Client

Public Class Methods

new() click to toggle source
# File lib/cfb_api.rb, line 17
def initialize
  @conn = Faraday.new(url: 'https://api.collegefootballdata.com', headers: { 'Content-Type': 'application/json' })
end

Public Instance Methods

conferences() click to toggle source
# File lib/cfb_api.rb, line 21
def conferences
  resp = @conn.get('conferences')
  json_body = parse_response(resp)

  map_to_class(json_body, CFB::Conference)
end
drives(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, offense: nil, defense: nil, conference: nil, offense_conference: nil, defense_conference: nil) click to toggle source
# File lib/cfb_api.rb, line 101
def drives(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, offense: nil, defense: nil, conference: nil, offense_conference: nil, defense_conference: nil)
  year ||= Time.now.year

  params = {
    year: year,
    season_type: season_type,
    week: week,
    team: team,
    offense: offense,
    defense: defense,
    conference: conference,
    offense_conference: offense_conference,
    defense_conference: defense_conference
  }

  resp = @conn.get('drives', prepare_params(params))
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Drive)
end
fbs_teams(year: nil) click to toggle source
# File lib/cfb_api.rb, line 34
def fbs_teams(year: nil)
  year ||= Time.now.year
  resp = @conn.get('teams/fbs', year: year)
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Team)
end
game(game_id) click to toggle source
# File lib/cfb_api.rb, line 71
def game(game_id)
  resp = @conn.get('games', id: game_id)
  json_body = parse_response(resp)
  CFB::Game.new(json_body[0])
end
games(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, home: nil, away: nil, conference: nil) click to toggle source
# File lib/cfb_api.rb, line 60
def games(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, home: nil,
          away: nil, conference: nil)

  year ||= Time.now.year
  params = { year: year, season_type: season_type, week: week, team: team, home: home, away: away, conference: conference }
  resp = @conn.get('games', prepare_params(params))
  json_body = parse_response(resp)

  map_to_class(json_body, CFB::Game)
end
matchup(team1, team2, min_year: nil, max_year: nil) click to toggle source
# File lib/cfb_api.rb, line 41
def matchup(team1, team2, min_year: nil, max_year: nil)
  params = {
    team1: team1,
    team2: team2,
    min_year: min_year,
    max_year: max_year
  }

  resp = @conn.get('teams/matchup', prepare_params(params))
  json_body = parse_response(resp)
  CFB::MatchupHistory.new(json_body)
end
parse_response(resp) click to toggle source
# File lib/cfb_api.rb, line 134
def parse_response(resp)
  parsed_resp = JSON.parse(resp.body, symbolize_names: true)

  if parsed_resp.is_a? Hash
    prepare_hash(parsed_resp)
  elsif parsed_resp.is_a? Array
    parsed_resp.map { |hsh| prepare_hash(hsh) }
  end
end
play_by_play(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, offense: nil, defense: nil, conference: nil, offense_conference: nil, defense_conference: nil, play_type: nil) click to toggle source
# File lib/cfb_api.rb, line 77
def play_by_play(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, offense: nil,
                 defense: nil, conference: nil, offense_conference: nil,
                 defense_conference: nil, play_type: nil)

  year ||= Time.now.year

  params = {
    year: year,
    season_type: season_type,
    week: week,
    team: team,
    offense: offense,
    defense: defense,
    conference: conference,
    offense_conference: offense_conference,
    defense_conference: defense_conference,
    play_type: play_type
  }

  resp = @conn.get('plays', prepare_params(params))
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Play)
end
roster(team) click to toggle source
# File lib/cfb_api.rb, line 54
def roster(team)
  resp = @conn.get('roster', team: team)
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Player)
end
teams(conference: nil) click to toggle source
# File lib/cfb_api.rb, line 28
def teams(conference: nil)
  resp = @conn.get('teams', conference: conference)
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Team)
end
venues() click to toggle source
# File lib/cfb_api.rb, line 128
def venues
  resp = @conn.get('venues')
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Venue)
end

Private Instance Methods

camelize(str) click to toggle source
# File lib/cfb_api.rb, line 165
def camelize(str)
  Strings::Case.camelcase(str.to_s)
end
map_to_class(data, klass) click to toggle source
# File lib/cfb_api.rb, line 173
def map_to_class(data, klass)
  data.map { |d| klass.new(d) }
end
prepare_hash(hsh) click to toggle source
# File lib/cfb_api.rb, line 146
def prepare_hash(hsh)
  return hsh unless hsh.is_a? Hash

  hsh.each_with_object({}) do |(k, v), memo|
    v = prepare_hash(v) if v.is_a? Hash
    v = v.map { |vv| prepare_hash(vv) } if v.is_a? Array

    memo[underscore(k)] = v
  end
end
prepare_params(params) click to toggle source
# File lib/cfb_api.rb, line 157
def prepare_params(params)
  params = params.each_with_object({}) do |(k, v), memo|
    memo[camelize(k)] = v
  end

  params.reject { |k, v| v.nil? }
end
underscore(str) click to toggle source
# File lib/cfb_api.rb, line 169
def underscore(str)
  Strings::Case.underscore(str.to_s)
end