Class: CFB::Client

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

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



17
18
19
# File 'lib/cfb_api.rb', line 17

def initialize
  @conn = Faraday.new(url: 'https://api.collegefootballdata.com', headers: { 'Content-Type': 'application/json' })
end

Instance Method Details

#conferencesObject



21
22
23
24
25
26
# 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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



34
35
36
37
38
39
# 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) ⇒ Object



71
72
73
74
75
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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

#player_search(search, position: nil, team: nil) ⇒ Object



121
122
123
124
125
126
# File 'lib/cfb_api.rb', line 121

def player_search(search, position: nil, team: nil)
  params = { search_term: search, position: position, team: team }
  resp = @conn.get('player/search', prepare_params(params))
  json_body = parse_response(resp)
  map_to_class(json_body, CFB::Player)
end

#roster(team) ⇒ Object



54
55
56
57
58
# 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) ⇒ Object



28
29
30
31
32
# 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

#venuesObject



128
129
130
131
132
# 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