cfb_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +9 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +78 -0
- data/LICENSE.txt +21 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/cfb_api.gemspec +36 -0
- data/lib/cfb_api.rb +177 -0
- data/lib/cfb_api/errors.rb +10 -0
- data/lib/cfb_api/middleware.rb +59 -0
- data/lib/cfb_api/models.rb +175 -0
- data/lib/cfb_api/version.rb +3 -0
- metadata +186 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: be13fb8715bfc40777297a705cbf86331710a95448d2101d6e1d22aeebf0e3fb
|
4
|
+
data.tar.gz: cdafe81f1739a6fa293d902cca1476c07a491bd38c7b8f34bb946b7a996c67b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3730e748d6aab0d29e6ac3caf55c7d9a401dbb428d6eb4fe990e0aea600345a175d9daff1ad82d92f9f7a42172a2d20410d2b048572bdf4df7f0c2bc949ab653
|
7
|
+
data.tar.gz: 052aed7becf48fcfdfc23454472f65307d913617b59a2c1ed544f143cb5e1c9912a36103c4ce35df893e3b82bd70975cfe0e9348506824ce4f29924b7bf46e9f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cfb_api (0.1.0)
|
5
|
+
faraday
|
6
|
+
strings-case
|
7
|
+
virtus
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
ast (2.4.0)
|
13
|
+
axiom-types (0.1.1)
|
14
|
+
descendants_tracker (~> 0.0.4)
|
15
|
+
ice_nine (~> 0.11.0)
|
16
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
17
|
+
byebug (11.0.1)
|
18
|
+
coercible (1.0.0)
|
19
|
+
descendants_tracker (~> 0.0.1)
|
20
|
+
descendants_tracker (0.0.4)
|
21
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
22
|
+
diff-lcs (1.3)
|
23
|
+
equalizer (0.0.11)
|
24
|
+
faraday (0.17.0)
|
25
|
+
multipart-post (>= 1.2, < 3)
|
26
|
+
ice_nine (0.11.2)
|
27
|
+
jaro_winkler (1.5.3)
|
28
|
+
multipart-post (2.1.1)
|
29
|
+
parallel (1.18.0)
|
30
|
+
parser (2.6.5.0)
|
31
|
+
ast (~> 2.4.0)
|
32
|
+
rainbow (3.0.0)
|
33
|
+
rake (10.5.0)
|
34
|
+
rspec (3.9.0)
|
35
|
+
rspec-core (~> 3.9.0)
|
36
|
+
rspec-expectations (~> 3.9.0)
|
37
|
+
rspec-mocks (~> 3.9.0)
|
38
|
+
rspec-core (3.9.0)
|
39
|
+
rspec-support (~> 3.9.0)
|
40
|
+
rspec-expectations (3.9.0)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.9.0)
|
43
|
+
rspec-mocks (3.9.0)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.9.0)
|
46
|
+
rspec-support (3.9.0)
|
47
|
+
rubocop (0.75.1)
|
48
|
+
jaro_winkler (~> 1.5.1)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 2.6)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
54
|
+
ruby-progressbar (1.10.1)
|
55
|
+
strings-case (0.1.0)
|
56
|
+
thread_safe (0.3.6)
|
57
|
+
unicode-display_width (1.6.0)
|
58
|
+
vcr (3.0.3)
|
59
|
+
virtus (1.0.5)
|
60
|
+
axiom-types (~> 0.1)
|
61
|
+
coercible (~> 1.0)
|
62
|
+
descendants_tracker (~> 0.0, >= 0.0.3)
|
63
|
+
equalizer (~> 0.0, >= 0.0.9)
|
64
|
+
|
65
|
+
PLATFORMS
|
66
|
+
ruby
|
67
|
+
|
68
|
+
DEPENDENCIES
|
69
|
+
bundler (~> 1.17)
|
70
|
+
byebug
|
71
|
+
cfb_api!
|
72
|
+
rake (~> 10.0)
|
73
|
+
rspec (~> 3.0)
|
74
|
+
rubocop
|
75
|
+
vcr
|
76
|
+
|
77
|
+
BUNDLED WITH
|
78
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Wyatt Kirby
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# CFB API Client [![Build Status](https://travis-ci.org/apsislabs/cfb_api.svg?branch=master)](https://travis-ci.org/apsislabs/cfb_api)
|
2
|
+
|
3
|
+
A Ruby client for the `collegefootballapi`. API Specs can be found [here](https://api.collegefootballdata.com/api/docs/?url=/api-docs.json#/).
|
4
|
+
|
5
|
+
This gem also provides a small library of models for interacting with the returned data, and normalizes some of the responses.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cfb_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install CFB
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Initialize a client like so:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'cfb_api'
|
29
|
+
cfb = CFB::Client.new
|
30
|
+
```
|
31
|
+
|
32
|
+
Once you have a client, you can issue requests to the following endpoints:
|
33
|
+
|
34
|
+
### Conferences
|
35
|
+
|
36
|
+
Returns an array of `CFB::Conference` objects.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
cfb.conferences
|
40
|
+
```
|
41
|
+
|
42
|
+
### Teams
|
43
|
+
|
44
|
+
Returns an array of `CFB::Team` objects.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
teams = cfb.teams
|
48
|
+
sec_teams = cfb.teams(conference: 'SEC')
|
49
|
+
```
|
50
|
+
|
51
|
+
### FBS Teams
|
52
|
+
|
53
|
+
Returns an array of FBS teams as `CFB::Team` objects.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
cfb.fbs_teams
|
57
|
+
cfb.fbs_teams(year: 2012)
|
58
|
+
```
|
59
|
+
|
60
|
+
### Matchup
|
61
|
+
|
62
|
+
Returns a history of a matchup between any two teams as a `CFB::MatchupHistory` object.
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
cfb.matchup('Oregon', 'Washington')
|
66
|
+
cfb.matchup('Oklahoma', 'Texas', min_year: 1960, max_year: 1969)
|
67
|
+
```
|
68
|
+
|
69
|
+
### Roster
|
70
|
+
|
71
|
+
Returns a team's roster as an array of `CFB::Player` objects.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
cfb.roster('Oregon')
|
75
|
+
```
|
76
|
+
|
77
|
+
### Games
|
78
|
+
|
79
|
+
Returns an array of `CFB::Game` objects.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
cfb.games
|
83
|
+
cfb.games(year: 2004, week: 10)
|
84
|
+
cfb.games(season_type: CFB::POST_SEASON, team: 'Oregon')
|
85
|
+
cfb.games(season_type: CFB::REGULAR_SEASON, conference: 'AAC')
|
86
|
+
```
|
87
|
+
|
88
|
+
### Game
|
89
|
+
|
90
|
+
Returns a single `CFB::Game` object.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
cfb.game(1)
|
94
|
+
```
|
95
|
+
|
96
|
+
### Play-by-Play
|
97
|
+
|
98
|
+
Returns an array of `CFB::Play` objects.
|
99
|
+
|
100
|
+
**Note:** This returns a ton of data, and is recommended that you apply several filters to reduce the size of your response.
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
cfb.play_by_play(year: 2016, week: 1, team: 'Oregon')
|
104
|
+
```
|
105
|
+
|
106
|
+
### Drives
|
107
|
+
|
108
|
+
Returns an array of `CFB::Drive` objects.
|
109
|
+
|
110
|
+
**Note:** This returns a ton of data, and is recommended that you apply several filters to reduce the size of your response.
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
cfb.drives(year: 2011, week: 10, offense: 'Alabama')
|
114
|
+
```
|
115
|
+
|
116
|
+
### Player Search
|
117
|
+
|
118
|
+
Returns an array of players matching the search query as `CFB::Player` objects.
|
119
|
+
|
120
|
+
**Note:** This endpoint is... unreliable... in its responses. Not every player is available.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
cfb.player_search('mariota')
|
124
|
+
```
|
125
|
+
|
126
|
+
### Venues
|
127
|
+
|
128
|
+
Returns an array of stadium data as `CFB::Venue` objects.
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
cfb.venues
|
132
|
+
```
|
133
|
+
|
134
|
+
## Development
|
135
|
+
|
136
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
137
|
+
|
138
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
139
|
+
|
140
|
+
## License
|
141
|
+
|
142
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "CFB"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/cfb_api.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'cfb_api/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'cfb_api'
|
9
|
+
spec.version = CFB::VERSION
|
10
|
+
spec.authors = ['Wyatt Kirby']
|
11
|
+
spec.email = ['wyatt@apsis.io']
|
12
|
+
|
13
|
+
spec.summary = 'A Ruby client for collegefootballdata.com.'
|
14
|
+
# spec.homepage = "TODO: Put your gem's website or public repo URL here."
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_runtime_dependency 'faraday'
|
27
|
+
spec.add_runtime_dependency 'virtus'
|
28
|
+
spec.add_runtime_dependency 'strings-case'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
31
|
+
spec.add_development_dependency 'byebug'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
34
|
+
spec.add_development_dependency 'vcr'
|
35
|
+
spec.add_development_dependency 'rubocop'
|
36
|
+
end
|
data/lib/cfb_api.rb
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'byebug'
|
4
|
+
require 'json'
|
5
|
+
require 'faraday'
|
6
|
+
require 'strings-case'
|
7
|
+
require 'cfb_api/version'
|
8
|
+
require 'cfb_api/errors'
|
9
|
+
require 'cfb_api/middleware'
|
10
|
+
require 'cfb_api/models'
|
11
|
+
|
12
|
+
module CFB
|
13
|
+
REGULAR_SEASON = 'regular'
|
14
|
+
POST_SEASON = 'postseason'
|
15
|
+
|
16
|
+
class Client
|
17
|
+
def initialize
|
18
|
+
@conn = Faraday.new(url: 'https://api.collegefootballdata.com', headers: { 'Content-Type': 'application/json' })
|
19
|
+
end
|
20
|
+
|
21
|
+
def conferences
|
22
|
+
resp = @conn.get('conferences')
|
23
|
+
json_body = parse_response(resp)
|
24
|
+
|
25
|
+
map_to_class(json_body, CFB::Conference)
|
26
|
+
end
|
27
|
+
|
28
|
+
def teams(conference: nil)
|
29
|
+
resp = @conn.get('teams', conference: conference)
|
30
|
+
json_body = parse_response(resp)
|
31
|
+
map_to_class(json_body, CFB::Team)
|
32
|
+
end
|
33
|
+
|
34
|
+
def fbs_teams(year: nil)
|
35
|
+
year ||= Time.now.year
|
36
|
+
resp = @conn.get('teams/fbs', year: year)
|
37
|
+
json_body = parse_response(resp)
|
38
|
+
map_to_class(json_body, CFB::Team)
|
39
|
+
end
|
40
|
+
|
41
|
+
def matchup(team1, team2, min_year: nil, max_year: nil)
|
42
|
+
params = {
|
43
|
+
team1: team1,
|
44
|
+
team2: team2,
|
45
|
+
min_year: min_year,
|
46
|
+
max_year: max_year
|
47
|
+
}
|
48
|
+
|
49
|
+
resp = @conn.get('teams/matchup', prepare_params(params))
|
50
|
+
json_body = parse_response(resp)
|
51
|
+
CFB::MatchupHistory.new(json_body)
|
52
|
+
end
|
53
|
+
|
54
|
+
def roster(team)
|
55
|
+
resp = @conn.get('roster', team: team)
|
56
|
+
json_body = parse_response(resp)
|
57
|
+
map_to_class(json_body, CFB::Player)
|
58
|
+
end
|
59
|
+
|
60
|
+
def games(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, home: nil,
|
61
|
+
away: nil, conference: nil)
|
62
|
+
|
63
|
+
year ||= Time.now.year
|
64
|
+
params = { year: year, season_type: season_type, week: week, team: team, home: home, away: away, conference: conference }
|
65
|
+
resp = @conn.get('games', prepare_params(params))
|
66
|
+
json_body = parse_response(resp)
|
67
|
+
|
68
|
+
map_to_class(json_body, CFB::Game)
|
69
|
+
end
|
70
|
+
|
71
|
+
def game(game_id)
|
72
|
+
resp = @conn.get('games', id: game_id)
|
73
|
+
json_body = parse_response(resp)
|
74
|
+
CFB::Game.new(json_body[0])
|
75
|
+
end
|
76
|
+
|
77
|
+
def play_by_play(year: nil, season_type: CFB::REGULAR_SEASON, week: nil, team: nil, offense: nil,
|
78
|
+
defense: nil, conference: nil, offense_conference: nil,
|
79
|
+
defense_conference: nil, play_type: nil)
|
80
|
+
|
81
|
+
year ||= Time.now.year
|
82
|
+
|
83
|
+
params = {
|
84
|
+
year: year,
|
85
|
+
season_type: season_type,
|
86
|
+
week: week,
|
87
|
+
team: team,
|
88
|
+
offense: offense,
|
89
|
+
defense: defense,
|
90
|
+
conference: conference,
|
91
|
+
offense_conference: offense_conference,
|
92
|
+
defense_conference: defense_conference,
|
93
|
+
play_type: play_type
|
94
|
+
}
|
95
|
+
|
96
|
+
resp = @conn.get('plays', prepare_params(params))
|
97
|
+
json_body = parse_response(resp)
|
98
|
+
map_to_class(json_body, CFB::Play)
|
99
|
+
end
|
100
|
+
|
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)
|
102
|
+
year ||= Time.now.year
|
103
|
+
|
104
|
+
params = {
|
105
|
+
year: year,
|
106
|
+
season_type: season_type,
|
107
|
+
week: week,
|
108
|
+
team: team,
|
109
|
+
offense: offense,
|
110
|
+
defense: defense,
|
111
|
+
conference: conference,
|
112
|
+
offense_conference: offense_conference,
|
113
|
+
defense_conference: defense_conference
|
114
|
+
}
|
115
|
+
|
116
|
+
resp = @conn.get('drives', prepare_params(params))
|
117
|
+
json_body = parse_response(resp)
|
118
|
+
map_to_class(json_body, CFB::Drive)
|
119
|
+
end
|
120
|
+
|
121
|
+
def player_search(search, position: nil, team: nil)
|
122
|
+
params = { search_term: search, position: position, team: team }
|
123
|
+
resp = @conn.get('player/search', prepare_params(params))
|
124
|
+
json_body = parse_response(resp)
|
125
|
+
map_to_class(json_body, CFB::Player)
|
126
|
+
end
|
127
|
+
|
128
|
+
def venues
|
129
|
+
resp = @conn.get('venues')
|
130
|
+
json_body = parse_response(resp)
|
131
|
+
map_to_class(json_body, CFB::Venue)
|
132
|
+
end
|
133
|
+
|
134
|
+
def parse_response(resp)
|
135
|
+
parsed_resp = JSON.parse(resp.body, symbolize_names: true)
|
136
|
+
|
137
|
+
if parsed_resp.is_a? Hash
|
138
|
+
prepare_hash(parsed_resp)
|
139
|
+
elsif parsed_resp.is_a? Array
|
140
|
+
parsed_resp.map { |hsh| prepare_hash(hsh) }
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def prepare_hash(hsh)
|
147
|
+
return hsh unless hsh.is_a? Hash
|
148
|
+
|
149
|
+
hsh.each_with_object({}) do |(k, v), memo|
|
150
|
+
v = prepare_hash(v) if v.is_a? Hash
|
151
|
+
v = v.map { |vv| prepare_hash(vv) } if v.is_a? Array
|
152
|
+
|
153
|
+
memo[underscore(k)] = v
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def prepare_params(params)
|
158
|
+
params = params.each_with_object({}) do |(k, v), memo|
|
159
|
+
memo[camelize(k)] = v
|
160
|
+
end
|
161
|
+
|
162
|
+
params.reject { |k, v| v.nil? }
|
163
|
+
end
|
164
|
+
|
165
|
+
def camelize(str)
|
166
|
+
Strings::Case.camelcase(str.to_s)
|
167
|
+
end
|
168
|
+
|
169
|
+
def underscore(str)
|
170
|
+
Strings::Case.underscore(str.to_s)
|
171
|
+
end
|
172
|
+
|
173
|
+
def map_to_class(data, klass)
|
174
|
+
data.map { |d| klass.new(d) }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module CFB
|
2
|
+
class Error < StandardError; end
|
3
|
+
class NotFound < Error; end
|
4
|
+
class BadRequest < Error; end
|
5
|
+
class TooManyRequests < Error; end
|
6
|
+
class InternalServerError < Error; end
|
7
|
+
class BadGateway < Error; end
|
8
|
+
class ServiceUnavailable < Error; end
|
9
|
+
class GatewayTimeout < Error; end
|
10
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module CFB
|
6
|
+
class ErrorMiddleware < Faraday::Middleware
|
7
|
+
def call(env)
|
8
|
+
@app.call(env).on_complete do |response|
|
9
|
+
case response[:status].to_i
|
10
|
+
when 400
|
11
|
+
raise CFB::BadRequest, error_message_400(response)
|
12
|
+
when 404
|
13
|
+
raise CFB::NotFound, error_message_400(response)
|
14
|
+
when 429
|
15
|
+
raise CFB::TooManyRequests, error_message_400(response)
|
16
|
+
when 500
|
17
|
+
raise CFB::InternalServerError, error_message_500(response, 'Something is technically wrong.')
|
18
|
+
when 502
|
19
|
+
raise CFB::BadGateway, error_message_500(response, 'The server returned an invalid or incomplete response.')
|
20
|
+
when 503
|
21
|
+
raise CFB::ServiceUnavailable, error_message_500(response, 'Your requests are rate limited.')
|
22
|
+
when 504
|
23
|
+
raise CFB::GatewayTimeout, error_message_500(response, '504 Gateway Time-out')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(app)
|
29
|
+
super app
|
30
|
+
@parser = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def error_message_400(response)
|
36
|
+
"#{response[:method].to_s.upcase} #{response[:url]}: #{response[:status]}#{error_body(response[:body])}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def error_body(body)
|
40
|
+
# body gets passed as a string, not sure if it is passed as something else from other spots?
|
41
|
+
if !body.nil? && !body.empty? && body.is_a?(String)
|
42
|
+
# removed multi_json thanks to wesnolte's commit
|
43
|
+
body = ::JSON.parse(body)
|
44
|
+
end
|
45
|
+
|
46
|
+
if body.nil?
|
47
|
+
nil
|
48
|
+
elsif body['meta'] && body['meta']['error_message'] && !body['meta']['error_message'].empty?
|
49
|
+
": #{body['meta']['error_message']}"
|
50
|
+
elsif body['error_message'] && !body['error_message'].empty?
|
51
|
+
": #{body['error_type']}: #{body['error_message']}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def error_message_500(response, body = nil)
|
56
|
+
"#{response[:method].to_s.upcase} #{response[:url]}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'virtus'
|
4
|
+
|
5
|
+
module CFB
|
6
|
+
class Model
|
7
|
+
include Virtus.model
|
8
|
+
end
|
9
|
+
|
10
|
+
class ApiModel < Model
|
11
|
+
private
|
12
|
+
|
13
|
+
def connection
|
14
|
+
@connection ||= CFB::Client.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class LatLng < Model
|
19
|
+
attribute :x, Float
|
20
|
+
attribute :y, Float
|
21
|
+
end
|
22
|
+
|
23
|
+
class ClockTime < Model
|
24
|
+
attribute :minutes, Integer
|
25
|
+
attribute :seconds, Integer
|
26
|
+
end
|
27
|
+
|
28
|
+
class Conference < ApiModel
|
29
|
+
attribute :id, Integer
|
30
|
+
attribute :name, String
|
31
|
+
attribute :short_name, String
|
32
|
+
attribute :abbreviation, String
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
short_nam
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Team < ApiModel
|
40
|
+
attribute :id, Integer
|
41
|
+
attribute :school, String
|
42
|
+
attribute :mascot, String
|
43
|
+
attribute :abbreviation, String
|
44
|
+
attribute :alt_name_1, String
|
45
|
+
attribute :alt_name_2, String
|
46
|
+
attribute :alt_name_3, String
|
47
|
+
attribute :conference, String
|
48
|
+
attribute :division, String
|
49
|
+
attribute :color, String
|
50
|
+
attribute :alt_color, String
|
51
|
+
attribute :logos, Array[String]
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
school
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Game < ApiModel
|
59
|
+
attribute :id, Integer
|
60
|
+
attribute :season, Integer
|
61
|
+
attribute :week, Integer
|
62
|
+
attribute :season_type, String
|
63
|
+
attribute :start_date, Date
|
64
|
+
attribute :neutral_site, Boolean
|
65
|
+
attribute :conference_game, Boolean
|
66
|
+
attribute :attendance, Integer
|
67
|
+
attribute :venue_id, Integer
|
68
|
+
attribute :venue, String
|
69
|
+
attribute :home_team, String
|
70
|
+
attribute :home_conference, String
|
71
|
+
attribute :home_points, Integer
|
72
|
+
attribute :home_line_scores, Array[Integer]
|
73
|
+
attribute :away_team, String
|
74
|
+
attribute :away_conference, String
|
75
|
+
attribute :away_points, Integer
|
76
|
+
attribute :away_line_scores, Array[Integer]
|
77
|
+
|
78
|
+
def start_date=(val)
|
79
|
+
val = Date.strptime(val) if val.is_a? String
|
80
|
+
super(val)
|
81
|
+
end
|
82
|
+
|
83
|
+
def to_s
|
84
|
+
"#{away_team} @ #{home_team} (Week #{week}, #{season})"
|
85
|
+
end
|
86
|
+
|
87
|
+
alias :date= :start_date=
|
88
|
+
alias :home_score= :home_points=
|
89
|
+
alias :away_score= :away_points=
|
90
|
+
end
|
91
|
+
|
92
|
+
class MatchupHistory < ApiModel
|
93
|
+
attribute :team1, String
|
94
|
+
attribute :team2, String
|
95
|
+
attribute :start_year, Integer
|
96
|
+
attribute :end_year, Integer
|
97
|
+
attribute :team1_wins, Integer
|
98
|
+
attribute :team2_wins, Integer
|
99
|
+
attribute :ties, Integer
|
100
|
+
attribute :games, Array[Game]
|
101
|
+
end
|
102
|
+
|
103
|
+
class Player < ApiModel
|
104
|
+
attribute :id, Integer
|
105
|
+
attribute :first_name, String
|
106
|
+
attribute :last_name, String
|
107
|
+
attribute :height, Integer
|
108
|
+
attribute :weight, Integer
|
109
|
+
attribute :jersey, Integer
|
110
|
+
attribute :year, Integer
|
111
|
+
attribute :position, String
|
112
|
+
attribute :city, String
|
113
|
+
attribute :state, String
|
114
|
+
attribute :country, String
|
115
|
+
end
|
116
|
+
|
117
|
+
class Venue < ApiModel
|
118
|
+
attribute :id, Integer
|
119
|
+
attribute :name, String
|
120
|
+
attribute :capacity, Integer
|
121
|
+
attribute :grass, Boolean
|
122
|
+
attribute :city, String
|
123
|
+
attribute :state, String
|
124
|
+
attribute :zip, String
|
125
|
+
attribute :country_code, String
|
126
|
+
attribute :location, LatLng
|
127
|
+
attribute :elevation, Integer
|
128
|
+
attribute :year, Integer
|
129
|
+
attribute :dome, Boolean
|
130
|
+
end
|
131
|
+
|
132
|
+
class Drive < ApiModel
|
133
|
+
attribute :offense, String
|
134
|
+
attribute :offense_conference, String
|
135
|
+
attribute :defense, String
|
136
|
+
attribute :defense_conference, String
|
137
|
+
attribute :id, Integer
|
138
|
+
attribute :game_id, Integer
|
139
|
+
attribute :scoring, Boolean
|
140
|
+
attribute :start_period, Integer
|
141
|
+
attribute :start_yardline, Integer
|
142
|
+
attribute :start_time, ClockTime
|
143
|
+
attribute :end_period, Integer
|
144
|
+
attribute :end_yardline, Integer
|
145
|
+
attribute :end_time, ClockTime
|
146
|
+
attribute :plays, Integer
|
147
|
+
attribute :yards, Integer
|
148
|
+
attribute :drive_result, String
|
149
|
+
|
150
|
+
def game
|
151
|
+
@game ||= connection.game(game_id)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class Play < ApiModel
|
156
|
+
attribute :id, Integer
|
157
|
+
attribute :drive_id, Integer
|
158
|
+
attribute :offense, String
|
159
|
+
attribute :offense_conference, String
|
160
|
+
attribute :offense_score, Integer
|
161
|
+
attribute :defense, String
|
162
|
+
attribute :home, String
|
163
|
+
attribute :away, String
|
164
|
+
attribute :defense_conference, String
|
165
|
+
attribute :defense_points, Integer
|
166
|
+
attribute :period, Integer
|
167
|
+
attribute :clock, ClockTime
|
168
|
+
attribute :yard_line, Integer
|
169
|
+
attribute :down, Integer
|
170
|
+
attribute :distance, Integer
|
171
|
+
attribute :yards_gained, Integer
|
172
|
+
attribute :play_type, String
|
173
|
+
attribute :play_text, String
|
174
|
+
end
|
175
|
+
end
|
metadata
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cfb_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wyatt Kirby
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: virtus
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: strings-case
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.17'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.17'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: vcr
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- wyatt@apsis.io
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- Gemfile
|
150
|
+
- Gemfile.lock
|
151
|
+
- LICENSE.txt
|
152
|
+
- README.md
|
153
|
+
- Rakefile
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- cfb_api.gemspec
|
157
|
+
- lib/cfb_api.rb
|
158
|
+
- lib/cfb_api/errors.rb
|
159
|
+
- lib/cfb_api/middleware.rb
|
160
|
+
- lib/cfb_api/models.rb
|
161
|
+
- lib/cfb_api/version.rb
|
162
|
+
homepage:
|
163
|
+
licenses:
|
164
|
+
- MIT
|
165
|
+
metadata: {}
|
166
|
+
post_install_message:
|
167
|
+
rdoc_options: []
|
168
|
+
require_paths:
|
169
|
+
- lib
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
requirements: []
|
181
|
+
rubyforge_project:
|
182
|
+
rubygems_version: 2.7.6.2
|
183
|
+
signing_key:
|
184
|
+
specification_version: 4
|
185
|
+
summary: A Ruby client for collegefootballdata.com.
|
186
|
+
test_files: []
|