Skip to content

Commit

Permalink
Parse and cache all years of clinical law rankings
Browse files Browse the repository at this point in the history
  • Loading branch information
s2t2 committed Feb 12, 2018
1 parent e0e6393 commit 1848dc1
Show file tree
Hide file tree
Showing 10 changed files with 17,811 additions and 22 deletions.
9 changes: 6 additions & 3 deletions lib/us_news_rankings/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def html_dir
end

def pages
source_urls.each_with_index.map{|url, number| UsNewsRankings::Page.new({
source_urls.each_with_index.map{|url, i| UsNewsRankings::Page.new({
category: self,
url: url,
number: number + 1
number: i + 1
})}
end

Expand All @@ -31,7 +31,10 @@ def extract_rankings
extracted_rankings = []
pages.each do |page|
page.table_rows.each do |row|
ranking = UsNewsRankings::Education::GraduateSchools::LawClinical::Ranking.new(row)
ranking = UsNewsRankings::Education::GraduateSchools::LawClinical::Ranking.new({
year: year,
row: row
})
extracted_rankings << ranking.to_h if ranking.ranked?
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ module Education
module GraduateSchools
module LawClinical
class Ranking < UsNewsRankings::Ranking
def rank_selector
case year
when 2014, 2013, 2012
return ".rankings-score"
else
return ".rankscore-bronze"
end
end

def rank
@rank || row.at_css(rank_selector).text.strip.gsub("#","").gsub("Tie","")
end

def tie?
row.at_css(rank_selector).text.include?("Tie")
end

def tuition
row.at_css(".search_tuition").text.strip #.gsub(" per year (full-time)","")
end
Expand All @@ -14,7 +31,7 @@ def enrollment
def to_h
{
rank: rank.to_i, # assumes school is ranked
tie: tie,
tie: tie?,
school_name: school_name,
school_city: school_city,
tuition: tuition,
Expand Down
10 changes: 6 additions & 4 deletions lib/us_news_rankings/ranking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

module UsNewsRankings
class Ranking
attr_reader :row
attr_reader :year, :row

# @param year [Integer] the rankings year
# @param row [Nokogiri::XML::Element] a rankings table row ("tr") element
def initialize(row)
def initialize(year:, row:)
@year = year
@row = row
end

def rank
@rank || row.at_css(".rankscore-bronze").text.strip.gsub("#","").gsub("Tie","")
raise "Oh, please implement #rank on the child class."
end

def ranked?
Expand All @@ -29,7 +31,7 @@ def school_city
end

def tie
row.at_css(".rankscore-bronze").text.include?("Tie")
raise "Oh, please implement #tie on the child class."
end

def to_h
Expand Down
12 changes: 10 additions & 2 deletions spec/category_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
RSpec.describe UsNewsRankings::Category do
describe "#rankings" do
describe "#source_urls" do
let(:category){ described_class.new(2017) }

it "should be implemented in a child class" do
expect{category.rankings}.to raise_error("please implement #rankings on the child class")
expect{category.source_urls}.to raise_error("Oh, please implement #source_urls on the child class.")
end
end

describe "#html_dir" do
let(:category){ described_class.new(2017) }

it "should be implemented in a child class" do
expect{category.html_dir}.to raise_error("Oh, please implement #html_dir on the child class.")
end
end
end
121 changes: 109 additions & 12 deletions spec/education/graduate_schools/law_clinical/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,118 @@
end

describe "#rankings" do
let(:category){ described_class.new(2017) }
describe "for 2017" do
let(:category){ described_class.new(2017) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$57,576 per year (full-time)",
:enrollment=>"1,721"
})
end
end

describe "for 2016" do
let(:category){ described_class.new(2016) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$55,255 per year (full-time)",
:enrollment=>"1,725"
})
end
end

describe "for 2015" do
let(:category){ described_class.new(2015) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(12)
end

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$53,130 per year (full-time)",
:enrollment=>"1,719"
})
end
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$57,576 per year (full-time)",
:enrollment=>"1,721"
})
describe "for 2014" do
let(:category){ described_class.new(2014) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$50,890 per year (full-time)",
:enrollment=>"1,694"
})
end
end

describe "for 2013" do
let(:category){ described_class.new(2013) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"$48,835 per year (full-time)",
:enrollment=>"1,683"
})
end
end

describe "for 2012" do
let(:category){ described_class.new(2012) }

it "should return a list of ranked schools" do
expect(category.rankings.count).to eql(11)
end

it "should contain the expected rankings" do
expect(category.rankings.first).to eql({
:rank=>1,
:tie=>false,
:school_name=>"Georgetown University",
:school_city=>"Washington, DC",
:tuition=>"Full-time: $46,865 per year",
:enrollment=>"1,671"
})
end
end
end
end
Loading

0 comments on commit 1848dc1

Please sign in to comment.