class Stellar::Course
Stellar
client scoped to a course.
Attributes
The generic Stellar
client used to query the server.
True if the client has administrative rights for this course.
Official MIT course ID, e.g. “6.006”.
URL to the course's main page on Stellar
.
Example: “stellar.mit.edu/S/course/6/fa11/6.006/”
Public Class Methods
Creates a scoped Stellar
client from a link to the course's page.
@param [String] number the official MIT course ID, e.g. “6.006” @param [Fixnum] year the year the course was taught e.g. 2011 @param [Symbol] semester :fall, :iap, :spring, :summer @return [Stellar::Course] client scoped to the course
# 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.
@param [Nokogiri::XML::Element] nokogiri_link a link pointing to the
course's main page
@param [Stellar::Client] client generic Stellar
client @return [Stellar::Course] client scoped to the course, or nil if the link
is not valid
# 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
Creates a scoped Stellar
client from detailed specifications.
@param [Stellar::Client] client generic Stellar
client @param [String] course_url HTTP URI to the course's main Stellar
page @param [String] course_number official course ID, e.g. “6.006” @raise ArgumentError if the course URL does not point to a course page
# 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.strip, URI.join(course_page.url, link['href'])] end] end