Class: Stellar::Course

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/courses.rb

Overview

Stellar client scoped to a course.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, course_url, course_number) ⇒ Course

Creates a scoped Stellar client from detailed specifications.

Parameters:

  • client (Stellar::Client)

    generic Stellar client

  • course_url (String)

    HTTP URI to the course’s main Stellar page

  • course_number (String)

    official course ID, e.g. “6.006”

Raises:

  • ArgumentError if the course URL does not point to a course page



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/stellar/courses.rb', line 88

def initialize(client, course_url, course_number)
  @client = client
  @url = course_url
  @number = course_number
  
  course_page = @client.get_nokogiri course_url
  
  @is_admin = course_page.css('p#toolset').length > 0
  
  navbar_elems = course_page.css('#mainnav')
  unless navbar_elems.length == 1
    raise ArgumentError, "#{course_url} is not a course page"
  end
  @navigation = Hash[navbar_elems.first.css('a').map do |link|
    [link.inner_text, URI.join(course_page.url, link['href'])]
  end]
end

Instance Attribute Details

#clientObject (readonly)

The generic Stellar client used to query the server.



39
40
41
# File 'lib/stellar/courses.rb', line 39

def client
  @client
end

#is_adminObject (readonly)

True if the client has administrative rights for this course.



36
37
38
# File 'lib/stellar/courses.rb', line 36

def is_admin
  @is_admin
end

Maps the text in navigation links to URI objects.

Example: navigation => <# URI: …/ >



33
34
35
# File 'lib/stellar/courses.rb', line 33

def navigation
  @navigation
end

#numberObject (readonly)

Official MIT course ID, e.g. “6.006”.



23
24
25
# File 'lib/stellar/courses.rb', line 23

def number
  @number
end

#urlObject (readonly)

URL to the course’s main page on Stellar.

Example: “stellar.mit.edu/S/course/6/fa11/6.006/”



28
29
30
# File 'lib/stellar/courses.rb', line 28

def url
  @url
end

Class Method Details

.for(number, year, semester, client) ⇒ Stellar::Course

Creates a scoped Stellar client from a link to the course’s page.

Parameters:

  • number (String)

    the official MIT course ID, e.g. “6.006”

  • year (Fixnum)

    the year the course was taught e.g. 2011

  • semester (Symbol)

    :fall, :iap, :spring, :summer

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stellar/courses.rb', line 64

def self.for(number, year, semester, client)
  semester_string = case semester
  when :fall
    'fa'
  when :spring
    'sp'
  when :summer
    'su'
  when :iap
    'ia'
  end
  term = "#{semester_string}#{year.to_s[-2..-1]}"
  major = number.split('.', 2).first
  url = "/S/course/#{major}/#{term}/#{number}/index.html"
  
  return self.new(client, url, number)
end

Creates a scoped Stellar client from a link to the course’s page.

Parameters:

  • nokogiri_link (Nokogiri::XML::Element)

    a link pointing to the course’s main page

  • client (Stellar::Client)

    generic Stellar client

Returns:

  • (Stellar::Course)

    client scoped to the course, or nil if the link is not valid



48
49
50
51
52
53
54
55
56
# File 'lib/stellar/courses.rb', line 48

def self.from_link(nokogiri_link, client)
  number = nokogiri_link.css('span.courseNo').inner_text
  return nil if number.empty?
  url = nokogiri_link['href']
  return nil unless url.index(number)
  return nil unless /\/S\/course\// =~ url
  
  return self.new(client, url, number)
end

Instance Method Details

#gradebookObject

Client scoped to the course’s Gradebook module.



112
113
114
# File 'lib/stellar/courses.rb', line 112

def gradebook
  @gradebook ||= Stellar::Gradebook.new self
end

#homeworkObject

Client scoped to the course’s Homework module.



107
108
109
# File 'lib/stellar/courses.rb', line 107

def homework
  @homework ||= Stellar::HomeworkList.new self
end