forked from mattparmett/pcr-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified course to work with the new PCR
- Loading branch information
Showing
3 changed files
with
59 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,62 @@ | ||
class Course < PCR | ||
attr_accessor :aliases, :credits, :description, :history, :id, | ||
:name, :path, :reviews, :sections, :semester, | ||
:retrieved, :valid, :version | ||
|
||
def initialize(path, semester, api_endpt, token) | ||
@path, @semester = path, semester | ||
@api_endpt, @token = api_endpt, token | ||
|
||
# Hit api | ||
api_url = makeURL(self.path) | ||
json = JSON.parse(open(api_url).read) | ||
|
||
# List of sections | ||
section_list = json['result']['sections']['values'] | ||
@sections = [] | ||
section_list.each do |section| | ||
@sections << Section.new(section['path'], @api_endpt, @token) | ||
end | ||
|
||
# Assign attrs | ||
attrs = %w(aliases credits description history id name reviews | ||
retrieved valid version) | ||
attrs.each do |attr| | ||
if json['result'][attr] | ||
self.instance_variable_set("@#{attr}", json['result'][attr]) | ||
else | ||
self.instance_variable_set("@#{attr}", json[attr]) | ||
module PCR | ||
class Course | ||
include Comparable | ||
attr_reader :aliases, :credits, :description, :history, :id, | ||
:name, :path, :reviews, :sections, :semester, | ||
:retrieved, :valid, :version | ||
|
||
def initialize(path, semester) | ||
#TODO: Don't need to pass in semester | ||
@path, @semester = path, semester | ||
|
||
# Hit api | ||
json = PCR.get_json(self.path) | ||
|
||
# List of sections | ||
@sections = json['result']['sections']['values'].map do |section| | ||
Section.new(section['path']) | ||
end | ||
end | ||
end | ||
|
||
def compareSemester(other) | ||
year = self.semester[0..3] | ||
season = self.semester[4] | ||
compYear = other.semester[0..3] | ||
compSeason = other.semester[4] | ||
|
||
if year.to_i > compYear.to_i #Later year | ||
return 1 | ||
elsif year.to_i < compYear.to_i #Earlier year | ||
return -1 | ||
elsif year.to_i == compYear.to_i #Same year, so test season | ||
if season > compSeason #Season is later | ||
return 1 | ||
elsif season = compSeason #Exact same time | ||
return 0 | ||
elsif season < compSeason #compSeason is later | ||
return -1 | ||
|
||
# Assign attrs | ||
# TODO: Use mixins | ||
attrs = %w(aliases credits description history id name reviews | ||
retrieved valid version) | ||
attrs.each do |attr| | ||
if json['result'][attr] | ||
self.instance_variable_set("@#{attr}", json['result'][attr]) | ||
else | ||
self.instance_variable_set("@#{attr}", json[attr]) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
self.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
|
||
def <=>(other) | ||
#TODO: Throw error if not same course | ||
return year <=> other.year unless year == other.year | ||
season <=> other.season | ||
end | ||
|
||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
#TODO: inject | ||
self.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
end | ||
end | ||
|
||
# Return average value across all sections | ||
(total / num) | ||
end | ||
|
||
# Return average value across all sections | ||
(total / num) | ||
end | ||
|
||
end | ||
def year | ||
@semester[0..3].to_i | ||
end | ||
|
||
def season | ||
@semester[4] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters