Class: CourseHistory
Instance Attribute Summary collapse
-
#course_code ⇒ Object
Returns the value of attribute course_code.
-
#courses ⇒ Object
Returns the value of attribute courses.
-
#id ⇒ Object
Returns the value of attribute id.
-
#path ⇒ Object
Returns the value of attribute path.
-
#retrieved ⇒ Object
Returns the value of attribute retrieved.
-
#valid ⇒ Object
Returns the value of attribute valid.
-
#version ⇒ Object
Returns the value of attribute version.
Attributes inherited from PCR
Instance Method Summary collapse
- #average(metric) ⇒ Object
-
#initialize(course_code, api_endpt, token) ⇒ CourseHistory
constructor
A new instance of CourseHistory.
- #name ⇒ Object
- #recent(metric) ⇒ Object
Methods inherited from PCR
Constructor Details
#initialize(course_code, api_endpt, token) ⇒ CourseHistory
Returns a new instance of CourseHistory.
4 5 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 |
# File 'lib/classes/coursehistory.rb', line 4 def initialize(course_code, api_endpt, token) @course_code = course_code @api_endpt = api_endpt @token = token # Read JSON from PCR API api_url = makeURL("coursehistories/#{self.course_code}") json = JSON.parse(open(api_url).read) # List of courses in coursehistory course_list = json['result']['courses'] @courses = [] course_list.each do |course| @courses << Course.new(course['path'], course['semester'], @api_endpt, @token) end # Sort course list by semester @courses.sort! { |a,b| a.compareSemester(b) } # Assign rest of attrs attrs = %w(id path 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 |
Instance Attribute Details
#course_code ⇒ Object
Returns the value of attribute course_code.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def course_code @course_code end |
#courses ⇒ Object
Returns the value of attribute courses.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def courses @courses end |
#id ⇒ Object
Returns the value of attribute id.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def id @id end |
#path ⇒ Object
Returns the value of attribute path.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def path @path end |
#retrieved ⇒ Object
Returns the value of attribute retrieved.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def retrieved @retrieved end |
#valid ⇒ Object
Returns the value of attribute valid.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def valid @valid end |
#version ⇒ Object
Returns the value of attribute version.
2 3 4 |
# File 'lib/classes/coursehistory.rb', line 2 def version @version end |
Instance Method Details
#average(metric) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/classes/coursehistory.rb', line 50 def average(metric) # Aggregate ratings across all sections total, num = 0, 0 courses.each do |course| course.sections.each do |section| section.reviews.each do |review| total += review.send(metric).to_f num += 1 end end end # Return average value across all sections (total / num) end |
#name ⇒ Object
66 67 68 |
# File 'lib/classes/coursehistory.rb', line 66 def name self.courses.last.name end |
#recent(metric) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/classes/coursehistory.rb', line 33 def recent(metric) # Select most recent course course = @courses[-1] # Aggregate ratings for metric total, num = 0, 0 course.sections.each do |section| section.reviews.each do |review| total += review.send(metric).to_f num += 1 end end # Return average value across most recent sections (total / num) end |