Skip to content

Commit

Permalink
Modified course to work with the new PCR
Browse files Browse the repository at this point in the history
  • Loading branch information
gterrono committed Jan 17, 2013
1 parent ce3d84f commit db63a90
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
118 changes: 56 additions & 62 deletions lib/classes/course.rb
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
2 changes: 2 additions & 0 deletions lib/classes/coursehistory.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module PCR
class CourseHistory
#TODO: attr_reader
attr_accessor :course_code, :courses, :id, :path, :retrieved, :valid, :version

def initialize(course_code)
Expand All @@ -18,6 +19,7 @@ def initialize(course_code)
@courses.sort! { |a,b| a.compareSemester(b) }

# Assign rest of attrs
# TODO: Use mixins
attrs = %w(id path reviews retrieved valid version)
attrs.each do |attr|
if json['result'][attr]
Expand Down
2 changes: 1 addition & 1 deletion lib/pcr-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'json'
require 'open-uri'
require 'classes/coursehistory'
#require 'classes/course'
require 'classes/course'
#require 'classes/errors'
#require 'classes/section'
#require 'classes/review'
Expand Down

0 comments on commit db63a90

Please sign in to comment.