lib/classes/course.rb in pcr-ruby-0.2.1 vs lib/classes/course.rb in pcr-ruby-0.5
- old
+ new
@@ -1,79 +1,68 @@
class Course < PCR
- attr_accessor :course_code, :sections, :id, :name, :path, :reviews
+ attr_accessor :aliases, :credits, :description, :history, :id,
+ :name, :path, :reviews, :sections, :semester,
+ :retrieved, :valid, :version
- def initialize(course_code)
- if course_code.is_a? String && course_code.isValidCourseCode?
- @course_code = course_code
-
- #Read JSON from the PCR API
- api_url = @@api_endpt + "coursehistories/" + self.course_code + "/?token=" + @@token
- json = JSON.parse(open(api_url).read)
-
- #Create array of Section objects
- @sections = []
- json["result"]["courses"].each { |c| @sections << Section.new(c["id"]) }
-
- #Set variables according to Course JSON data
- @id = json["result"]["id"]
- @name = json["result"]["name"]
- @path = json["result"]["path"]
-
- #Get reviews for the Course -- this has to be a separate query
- api_url_reviews = @@api_endpt + "coursehistories/" +
- self.id.to_s + "/reviews?token=" + @@token
- json_reviews = JSON.parse(open(api_url_reviews).read)
- @reviews = json_reviews["result"]["values"]
- else
- raise CourseError, "Invalid course code specified. Use format [DEPT-###]."
+ 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])
+ end
+ end
end
- def average(metric)
- #Ensure that we know argument type
- metric = metric.to_s if metric.is_a? Symbol
-
- if metric.is_a? String
- #Loop vars
- total, n = 0, 0
-
- #For each section, check if ratings include metric arg
- #if so, add metric rating to total && increment counting variable
- self.reviews.each do |review|
- if review["ratings"].include? metric
- total = total + review["ratings"][metric].to_f
- n = n + 1
- else
- raise CourseError, "No ratings found for \"#{metric}\" in #{self.name}."
- 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
end
-
- #Return average score as a float
- (total / n)
- else
- raise CourseError, "Invalid metric format. Metric must be a string or symbol."
end
end
- def recent(metric)
- #Ensure that we know argument type
- metric = metric.to_s if metric.is_a? Symbol
-
- if metric.is_a? String
- #Get the most recent section
- section = self.sections[-1]
-
- #Iterate through all the section reviews, and if the section review id matches
- #the id of the most recent section, return that rating
- self.reviews.each do |review|
- if review["section"]["id"].to_s[0..4].to_i == section.id
- return review["ratings"][metric]
- 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
end
-
- #Else, metric hasn't been found
- raise CourseError, "No ratings found for #{metric} in #{section.semester}."
- else
- raise CourseError, "Invalid metric format. Metric must be a string or symbol."
end
+
+ # Return average value across all sections
+ (total / num)
end
+
end
\ No newline at end of file