Skip to content
Browse files

API cleanup

  • Loading branch information...
1 parent 0a36916 commit c96b0a470ec0d6f95388602abe01126a54174a5b @cjolly committed
Showing with 51 additions and 34 deletions.
  1. +1 −1 Gemfile.lock
  2. +1 −1 bin/pickem
  3. +4 −4 lib/pickem.rb
  4. +20 −14 lib/pickem/scrapers/cbs_sports.rb
  5. +1 −1 lib/pickem/version.rb
  6. +9 −0 spec/pickem_spec.rb
  7. +15 −13 spec/scrapers/cbs_sports_spec.rb
View
2 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- pickem (0.0.1)
+ pickem (0.1.0)
nokogiri
terminal-table
View
2 bin/pickem
@@ -2,4 +2,4 @@
require './lib/pickem'
-Pickem.print_picks
+puts Pickem.results
View
8 lib/pickem.rb
@@ -5,15 +5,15 @@
require 'terminal-table'
module Pickem
- def self.print_picks
- scraper = Scrapers::CBSSports.new
+ def self.results
+ scraper = Scrapers::CBSSports.scrape!
table = Terminal::Table.new({
title: scraper.source,
headings: ['Away', 'Home', 'Current Line'],
- rows: scraper.sorted_games
+ rows: scraper.picks
})
table.align_column(2, :right)
- puts table
+ table
end
end
View
34 lib/pickem/scrapers/cbs_sports.rb
@@ -4,19 +4,35 @@
module Pickem
module Scrapers
class CBSSports
- attr_reader :source, :games, :doc, :sorted_games
+ attr_reader :source, :games, :doc, :picks
def initialize
@source = 'http://www.cbssports.com/nfl/odds'
@games = []
- scrape_cbs_odds_page
- parse
+ end
+
+ def self.scrape!
+ new.scrape!
end
def to_s
- sorted
+ picks
+ end
+
+ def scrape!
+ unless @doc
+ @doc = Nokogiri::HTML(open(source))
+ parse
+ end
+ return self
end
+ def picks
+ @games.sort_by {|game| game[2].to_f.abs}.reverse
+ end
+
+ private
+
def parse
tables = @doc.css('table.data')
tables.each do |table|
@@ -34,16 +50,6 @@ def parse
@games << [home_team, away_team, current_line]
end
end
-
- def sorted_games
- @games.sort_by {|game| game[2].to_f.abs}.reverse
- end
-
- private
-
- def scrape_cbs_odds_page
- @doc ||= Nokogiri::HTML(open(source))
- end
end
end
end
View
2 lib/pickem/version.rb
@@ -1,3 +1,3 @@
module Pickem
- VERSION = "0.0.1"
+ VERSION = "0.1.0"
end
View
9 spec/pickem_spec.rb
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe Pickem, vcr: {cassette_name: "cbs_sports"} do
+ it "returns a table of results" do
+ table = Pickem.results
+ table.should be_a(Terminal::Table)
+ table.rows.size.should == 16
+ end
+end
View
28 spec/scrapers/cbs_sports_spec.rb
@@ -2,26 +2,28 @@
module Pickem::Scrapers
describe CBSSports, vcr: {cassette_name: "cbs_sports"} do
- let(:scraper) { CBSSports.new }
+ let(:scraper) { CBSSports.scrape! }
it "knows it's source" do
- scraper.source.should == 'http://www.cbssports.com/nfl/odds'
+ CBSSports.new.source.should == 'http://www.cbssports.com/nfl/odds'
end
- it "should find 16 games" do
- scraper.games.size.should == 16
- end
+ context "scraped results" do
+ it "should find 16 games" do
+ scraper.games.size.should == 16
+ end
- it "should rank MIA @ SF as the highest weighted game of the week" do
- scraper.sorted_games.first.should == ['MIA', '*SF', '-10.5']
- end
+ it "should rank MIA @ SF as the highest weighted game of the week" do
+ scraper.picks.first.should == ['MIA', '*SF', '-10.5']
+ end
- it "should rank BAL @ WAS as the lowest weighted game of the week" do
- scraper.sorted_games.last.should == ['BAL', '*WAS', '-2.5']
- end
+ it "should rank BAL @ WAS as the lowest weighted game of the week" do
+ scraper.picks.last.should == ['BAL', '*WAS', '-2.5']
+ end
- it "should pick the away team if the line is positive" do
- scraper.sorted_games[1].first.should == '*DEN'
+ it "should pick the away team if the line is positive" do
+ scraper.picks[1].first.should == '*DEN'
+ end
end
end
end

0 comments on commit c96b0a4

Please sign in to comment.
Something went wrong with that request. Please try again.