Class: Course

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

Instance Attribute Summary collapse

Attributes inherited from PCR

#api_endpt, #token

Instance Method Summary collapse

Methods inherited from PCR

#coursehistory, #makeURL

Constructor Details

#initialize(path, semester, api_endpt, token) ⇒ 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
# File 'lib/classes/course.rb', line 6

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

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



2
3
4
# File 'lib/classes/course.rb', line 2

def aliases
  @aliases
end

#creditsObject

Returns the value of attribute credits.



2
3
4
# File 'lib/classes/course.rb', line 2

def credits
  @credits
end

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/classes/course.rb', line 2

def description
  @description
end

#historyObject

Returns the value of attribute history.



2
3
4
# File 'lib/classes/course.rb', line 2

def history
  @history
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/classes/course.rb', line 2

def id
  @id
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/classes/course.rb', line 2

def name
  @name
end

#pathObject

Returns the value of attribute path.



2
3
4
# File 'lib/classes/course.rb', line 2

def path
  @path
end

#retrievedObject

Returns the value of attribute retrieved.



2
3
4
# File 'lib/classes/course.rb', line 2

def retrieved
  @retrieved
end

#reviewsObject

Returns the value of attribute reviews.



2
3
4
# File 'lib/classes/course.rb', line 2

def reviews
  @reviews
end

#sectionsObject

Returns the value of attribute sections.



2
3
4
# File 'lib/classes/course.rb', line 2

def sections
  @sections
end

#semesterObject

Returns the value of attribute semester.



2
3
4
# File 'lib/classes/course.rb', line 2

def semester
  @semester
end

#validObject

Returns the value of attribute valid.



2
3
4
# File 'lib/classes/course.rb', line 2

def valid
  @valid
end

#versionObject

Returns the value of attribute version.



2
3
4
# File 'lib/classes/course.rb', line 2

def version
  @version
end

Instance Method Details

#average(metric) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/classes/course.rb', line 54

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
  end
  
  # Return average value across all sections

  (total / num)
end

#compareSemester(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/classes/course.rb', line 33

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
  end
end