Class: Instructor

Inherits:
PCR
  • Object
show all
Defined in:
lib/classes/instructor.rb

Overview

Instructor is a professor. Instructors are not tied to a course or section, but will have to be referenced from Sections.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PCR

#course, #instructor, #section

Constructor Details

#initialize(id) ⇒ Instructor

Returns a new instance of Instructor.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/classes/instructor.rb', line 5

def initialize(id)    
  #Assign args. ID is necessary because that's how we look up Instructors in the PCR API.
  if id.is_a? String
    @id = id
  else
    raise InstructorError("Invalid Instructor ID specified.")
  end
  
  #Hit PCR API to get missing info based on id
  self.getInfo
  self.getReviews

end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/classes/instructor.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/classes/instructor.rb', line 3

def name
  @name
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/classes/instructor.rb', line 3

def path
  @path
end

#reviewsObject

Returns the value of attribute reviews.



3
4
5
# File 'lib/classes/instructor.rb', line 3

def reviews
  @reviews
end

#sectionsObject

Returns the value of attribute sections.



3
4
5
# File 'lib/classes/instructor.rb', line 3

def sections
  @sections
end

Instance Method Details

#average(metric) ⇒ Object

Get average value of a certain rating for Instructor



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/classes/instructor.rb', line 41

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.getReviews
    self.reviews.each do |review|
      ratings = review["ratings"]
      if ratings.include? metric
        total = total + review["ratings"][metric].to_f
        n = n + 1
      else
        raise CourseError, "No ratings found for \"#{metric}\" for #{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

#getInfoObject

Hit the PCR API to get all missing info Separate method in case we want to conduct it separately from a class init



21
22
23
24
25
26
27
28
# File 'lib/classes/instructor.rb', line 21

def getInfo
  api_url = @@api_endpt + "instructors/" + self.id + "?token=" + @@token
  json = JSON.parse(open(api_url).read)
  
  @name = json["result"]["name"].downcase.titlecase unless @name
  @path = json["result"]["path"] unless @path
  @sections = json["result"]["reviews"]["values"] unless @sections #Mislabeled reviews in PCR API
end

#getReviewsObject

Separate method for getting review data in case we don’t want to make an extra API hit each init



31
32
33
34
35
36
37
38
# File 'lib/classes/instructor.rb', line 31

def getReviews
  if not self.reviews #make sure we don't already have reviews
    api_url = @@api_endpt + "instructors/" + self.id + "/reviews?token=" + @@token
    json = JSON.parse(open(api_url).read)
    
    @reviews = json["result"]["values"] #gets array
  end
end

#recent(metric) ⇒ Object

Get most recent value of a certain rating for Instructor



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/classes/instructor.rb', line 73

def recent(metric)
  #Ensure that we know argument type
  if metric.is_a? Symbol
    metric = metric.to_s
  end
  
  if metric.is_a? String
    #Iterate through reviews and create Section for each section reviewed, presented in an array
    sections = []
    section_ids = []
    self.getReviews
    self.reviews.each do |review|
      if section_ids.index(review["section"]["id"].to_i).nil?
        s = PCR::Section.new(review["section"]["id"].to_i, false)
        sections << s
        section_ids << s.id
      end
    end
    
    #Get only most recent Section(s) in the array
    sections.reverse! #Newest first
    targets = []
    sections.each do |s|
      s.hit_api(:get_reviews => true)
      if sections.index(s) == 0
        targets << s
      elsif s.semester == sections[0].semester and s.id != sections[0].id
        targets << s
      else
        break
      end
    end
    
    #Calculate recent rating
    total = 0
    num = 0
    targets.each do |section|
      #Make sure we get the rating for the right Instructor
      section.ratings.each do |rating|
        if rating.key?(self.id)
          if rating[self.id][metric].nil?
            raise InstructorError, "No ratings found for #{metric} for #{self.name}."
          else
            total = total + rating[self.id][metric].to_f
            num += 1
          end
        end
      end
    end

    # Return recent rating
    total / num
    
  else
    raise CourseError, "Invalid metric format. Metric must be a string or symbol."
  end
end