Class: Course
Overview
Course object matches up with the coursehistory request of the pcr api. A Course essentially is a signle curriculum and course code, and includes all Sections across time (semesters).
Instance Attribute Summary collapse
-
#course_code ⇒ Object
Returns the value of attribute course_code.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#reviews ⇒ Object
Returns the value of attribute reviews.
-
#sections ⇒ Object
Returns the value of attribute sections.
Instance Method Summary collapse
- #average(metric) ⇒ Object
-
#initialize(course_code) ⇒ Course
constructor
A new instance of Course.
- #recent(metric) ⇒ Object
Methods inherited from PCR
#course, #instructor, #section
Constructor Details
#initialize(course_code) ⇒ Course
Returns a new instance of Course.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/classes/course.rb', line 6 def initialize(course_code) if course_code.is_a? String and 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, containing all Sections found in the API JSON for the Course @sections = [] json["result"]["courses"].each do |c| @sections << Section.new(c["id"]) end #Set variables according to Course JSON data @id = json["result"]["id"] @name = json["result"]["name"] @path = json["result"]["path"] #Get reviews for the Course -- unfortunately 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-###]." end end |
Instance Attribute Details
#course_code ⇒ Object
Returns the value of attribute course_code.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def course_code @course_code end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def path @path end |
#reviews ⇒ Object
Returns the value of attribute reviews.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def reviews @reviews end |
#sections ⇒ Object
Returns the value of attribute sections.
4 5 6 |
# File 'lib/classes/course.rb', line 4 def sections @sections end |
Instance Method Details
#average(metric) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/classes/course.rb', line 35 def average(metric) #Ensure that we know argument type if metric.is_a? Symbol metric = metric.to_s end if metric.is_a? String #Loop vars total = 0 n = 0 #For each section, check if ratings include metric arg -- if so, add metric rating to total and increment counting variable self.reviews.each do |review| = review["ratings"] if .include? metric total = total + review["ratings"][metric].to_f n = n + 1 else raise CourseError, "No ratings found for \"#{metric}\" in #{self.name}." end end #Return average score as a float (total/n) else raise CourseError, "Invalid metric format. Metric must be a string or symbol." end end |
#recent(metric) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/classes/course.rb', line 65 def recent(metric) #Ensure that we know argument type if metric.is_a? Symbol metric = metric.to_s end 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 end raise CourseError, "No ratings found for #{metric} in #{section.semester}." else raise CourseError, "Invalid metric format. Metric must be a string or symbol." end end |