add divisions. add standings. add football.
This commit is contained in:
parent
5af5260a21
commit
f10bcdbd1a
6
Gemfile
6
Gemfile
|
@ -1,6 +1,8 @@
|
|||
source "https://rubygems.org"
|
||||
# frozen_string_literal: true
|
||||
|
||||
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
||||
source 'https://rubygems.org'
|
||||
|
||||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
||||
|
||||
# Specify your gem's dependencies in ncaa.gemspec
|
||||
gemspec
|
||||
|
|
8
Rakefile
8
Rakefile
|
@ -1,6 +1,8 @@
|
|||
require "bundler/gem_tasks"
|
||||
require "rspec/core/rake_task"
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'bundler/gem_tasks'
|
||||
require 'rspec/core/rake_task'
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task :default => :spec
|
||||
task default: :spec
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "bundler/setup"
|
||||
require "ncaa"
|
||||
require 'bundler/setup'
|
||||
require 'ncaa'
|
||||
|
||||
# You can add fixtures and/or initialization code here to make experimenting
|
||||
# with your gem easier. You can also use a different console, if you like.
|
||||
|
@ -10,5 +11,5 @@ require "ncaa"
|
|||
# require "pry"
|
||||
# Pry.start
|
||||
|
||||
require "irb"
|
||||
require 'irb'
|
||||
IRB.start(__FILE__)
|
||||
|
|
|
@ -10,7 +10,9 @@ module Ncaa
|
|||
attr_reader :client, :logger, :division, :sport
|
||||
|
||||
# @param client [Ncaa::Client::Base]
|
||||
# @param division [String] Vaild values: d1, d2, d3
|
||||
# @param division [String]
|
||||
# basketball-men: d1, d2, d3
|
||||
# football: fbs, fcs, d2, d3
|
||||
def initialize(client:, division: 'd1', **args)
|
||||
@client = client
|
||||
@logger = client.logger
|
||||
|
|
|
@ -2,6 +2,23 @@
|
|||
|
||||
module Ncaa
|
||||
class Basketball < Base
|
||||
DIVISIONS = %w[d1 d2 d3].freeze
|
||||
RANKINGS = D1_RANKINGS + D2_RANKINGS + D3_RANKINGS
|
||||
D1_RANKINGS = %w[associated-press ncaa-mens-basketball-net-rankings usa-today-coaches].freeze
|
||||
D2_RANKINGS = %w[nabc-coaches regional-ranking d2sida].freeze
|
||||
D3_RANKINGS = %w[d3hoopscom regional-rankings-0].freeze
|
||||
|
||||
def metadata
|
||||
{
|
||||
divisions: DIVISIONS,
|
||||
rankings: {
|
||||
d1: D1_RANKINGS,
|
||||
d2: D2_RANKINGS,
|
||||
d3: D3_RANKINGS
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def sport_uri
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Ncaa
|
||||
class Football < Base
|
||||
DIVISIONS = %w[fbs fcs d2 d3].freeze
|
||||
RANKINGS = FBS_RANKINGS + FCS_RANKINGS + D2_RANKINGS + D3_RANKINGS
|
||||
FBS_RANKINGS = %w[associated-press college-football-playoff usa-today-coaches-poll].freeze
|
||||
FCS_RANKINGS = %w[fcs-coaches-poll stats-fcs-top-25 simple-ratings-system].freeze
|
||||
D2_RANKINGS = %w[regional-rankings afca-coaches d2footballcom].freeze
|
||||
D3_RANKINGS = %w[regional-rankings afca-coaches d3footballcom].freeze
|
||||
|
||||
def metadata
|
||||
{
|
||||
divisions: DIVISIONS,
|
||||
rankings: {
|
||||
fbs: FBS_RANKINGS,
|
||||
fcs: FCS_RANKINGS,
|
||||
d2: D2_RANKINGS,
|
||||
d3: D3_RANKINGS
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def sport_uri
|
||||
'football'
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,7 +5,80 @@ module Ncaa
|
|||
module Html
|
||||
class Standings < Base
|
||||
def build
|
||||
raise NotImplementedError.new
|
||||
{
|
||||
type: 'standings',
|
||||
sort: 'by_conference',
|
||||
last_updated: last_updated,
|
||||
data: standings
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def conferences
|
||||
@conferences ||= html.css('figure.standings-conference')
|
||||
end
|
||||
|
||||
def table(conference)
|
||||
conference.next_element.css('table')
|
||||
end
|
||||
|
||||
def team_rows(table)
|
||||
table.css('tbody tr')
|
||||
end
|
||||
|
||||
# @return [Array<Symbol>]
|
||||
def keys(table)
|
||||
@keys ||= table.css('thead th').map { |x| x.text.remove(/\s/).underscore.to_sym }
|
||||
end
|
||||
|
||||
# @return [Time]
|
||||
def last_updated
|
||||
@last_updated ||= begin
|
||||
updated = html.css('figure.standings-last-updated').text
|
||||
date = updated[/\w+{3,}\.? \d+{1,2}, \d+{2,4} .+/]
|
||||
Time.parse(date)
|
||||
end
|
||||
end
|
||||
|
||||
def team_standing(row)
|
||||
standings = row.css('td')
|
||||
|
||||
{
|
||||
school: standings[0].text.strip,
|
||||
school_image_url: standings[0].css('img').attr('src').value,
|
||||
conference: {
|
||||
wins: standings[1].text.to_i,
|
||||
losses: standings[2].text.to_i,
|
||||
percentage: standings[3].text.to_f
|
||||
},
|
||||
overall: {
|
||||
wins: standings[4].text.to_i,
|
||||
losses: standings[5].text.to_i,
|
||||
percentage: standings[6].text.to_f
|
||||
},
|
||||
streak: standings[7].text
|
||||
}
|
||||
end
|
||||
|
||||
# @return [Array<Hash>]
|
||||
def standings
|
||||
@standings ||= begin
|
||||
conferences.each.with_object([]) do |conference, arr|
|
||||
standing = {
|
||||
conference: conference.text.strip,
|
||||
conference_image_url: conference.css('img').attr('src').value,
|
||||
standings: []
|
||||
}
|
||||
|
||||
conference_table = table(conference)
|
||||
teams = team_rows(conference_table)
|
||||
|
||||
teams.each { |team| standing[:standings] << team_standing(team) }
|
||||
|
||||
arr << standing
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Ncaa
|
||||
VERSION = "0.1.0"
|
||||
VERSION = '0.1.0'
|
||||
end
|
||||
|
|
|
@ -1,11 +1,32 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Ncaa do
|
||||
let(:type) { :basketball }
|
||||
|
||||
it 'has a version number' do
|
||||
expect(Ncaa::VERSION).not_to be nil
|
||||
end
|
||||
|
||||
it 'does something useful' do
|
||||
expect(false).to eq(true)
|
||||
describe '.new' do
|
||||
it 'returns sport client' do
|
||||
sub = described_class.new(type: type)
|
||||
expect(sub).to be_a(Ncaa::Basketball)
|
||||
expect(sub.client).to be_a(Ncaa::Client::Base)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.klass' do
|
||||
it 'returns valid class' do
|
||||
sub = described_class.klass(type: type)
|
||||
expect(sub).to eq(Ncaa::Basketball)
|
||||
end
|
||||
|
||||
it 'raises error when type is missing' do
|
||||
expect { described_class.klass }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it 'raises error when type is invalid' do
|
||||
expect { described_class.klass(type: 'foobar') }.to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue