Class: MyUW::SLNInfo
- Inherits:
-
Object
- Object
- MyUW::SLNInfo
- Defined in:
- lib/myuw/sln.rb
Overview
Synopsis
Gets information regarding a specific SLN, returning information in a SLN object.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#myuw ⇒ Object
Returns the value of attribute myuw.
-
#sln ⇒ Object
Returns the value of attribute sln.
-
#term ⇒ Object
Returns the value of attribute term.
Instance Method Summary collapse
-
#check_time_schedule_closed(page) ⇒ Object
Checks if the time schedule system is closed and if so, raises an exception.
-
#extract_info(keys, nodes) ⇒ Object
Extracts course information from each node and stores it as the proper key in the @data variable.
-
#fetch_data ⇒ Object
Fetches the information for the given SLN.
-
#get_course_info(page) ⇒ Object
Gets the basic SLN info (the top table in the current status page).
-
#get_course_notes(page) ⇒ Object
Gets the notes for a course.
-
#get_enrollment_info(page) ⇒ Object
Gets the enrollment information for an SLN such as current enrollment, space available, etc.
-
#get_sln_page ⇒ Object
Gets the SLN info page for the SLN and term, raising an exception if it is requested too soon.
-
#initialize(myuw) ⇒ SLNInfo
constructor
A new instance of SLNInfo.
Constructor Details
#initialize(myuw) ⇒ SLNInfo
Returns a new instance of SLNInfo.
11 12 13 14 15 16 |
# File 'lib/myuw/sln.rb', line 11 def initialize(myuw) @myuw = myuw @term = nil @data = nil @sln = nil end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/myuw/sln.rb', line 9 def data @data end |
#myuw ⇒ Object
Returns the value of attribute myuw.
8 9 10 |
# File 'lib/myuw/sln.rb', line 8 def myuw @myuw end |
#sln ⇒ Object
Returns the value of attribute sln.
7 8 9 |
# File 'lib/myuw/sln.rb', line 7 def sln @sln end |
#term ⇒ Object
Returns the value of attribute term.
6 7 8 |
# File 'lib/myuw/sln.rb', line 6 def term @term end |
Instance Method Details
#check_time_schedule_closed(page) ⇒ Object
Checks if the time schedule system is closed and if so, raises an exception
121 122 123 |
# File 'lib/myuw/sln.rb', line 121 def check_time_schedule_closed(page) raise TimeScheduleClosedError.new if page.uri.to_s == "http://www.washington.edu/students/timeschd/nots.html" end |
#extract_info(keys, nodes) ⇒ Object
Extracts course information from each node and stores it as the proper key in the @data variable
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/myuw/sln.rb', line 100 def extract_info(keys, nodes) return false if nodes.empty? nodes.each_with_index do |node, i| # Make sure we have keys left still if i < keys.length then data_key = keys[i] # If the key is nil, we skip this node unless data_key.nil? @data ||= {} @data[data_key] = node.inner_text.strip.gsub("\302\240", "") end end end true end |
#fetch_data ⇒ Object
Fetches the information for the given SLN.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/myuw/sln.rb', line 31 def fetch_data raise ArgumentError.new("SLN not set.") if @sln.nil? raise ArgumentError.new("Term not set.") if @term.nil? raise NotLoggedInError.new("User must be logged in to fetch SLN data") unless @myuw.logged_in? # Request the actual page page = get_sln_page # Check if the time is not available check_time_schedule_closed page # Get the actual course information get_course_info page get_enrollment_info page get_course_notes page end |
#get_course_info(page) ⇒ Object
Gets the basic SLN info (the top table in the current status page)
77 78 79 80 81 |
# File 'lib/myuw/sln.rb', line 77 def get_course_info(page) data_keys = [nil, :course, :section, :type, :credits, :title] info_nodes = page.search("//table[@border=1 and @cellpadding=3]//tr[@rowspan=1]//td") raise InvalidPageError.new(page, "Could not extract course info for SLN.") unless extract_info(data_keys, info_nodes) end |
#get_course_notes(page) ⇒ Object
Gets the notes for a course
92 93 94 95 96 |
# File 'lib/myuw/sln.rb', line 92 def get_course_notes(page) data_keys = [:notes] info_nodes = page.search("//table[@border=1 and @cellpadding=3]//tr[count(td)=1]//preceding-sibling::tr[count(th)=1]//following-sibling::tr//td") raise InvalidPageError.new(page, "Could not find course notes information.") unless extract_info(data_keys, info_nodes) end |
#get_enrollment_info(page) ⇒ Object
Gets the enrollment information for an SLN such as current enrollment, space available, etc.
85 86 87 88 89 |
# File 'lib/myuw/sln.rb', line 85 def get_enrollment_info(page) data_keys = [:current_enrollment, :limit_enrollment, :room_capacity, :space_available, :status] info_nodes = page.search("//table[@border=1 and @cellpadding=3]//tr[count(td)=5]//td") raise InvalidPageError.new(page, "Could not extract enrollment info for SLN.") unless extract_info(data_keys, info_nodes) end |
#get_sln_page ⇒ Object
Gets the SLN info page for the SLN and term, raising an exception if it is requested too soon
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/myuw/sln.rb', line 64 def get_sln_page page = @myuw.browser.get("https://sdb.admin.washington.edu/timeschd/uwnetid/sln.asp?QTRYR=#{@term}&SLN=#{@sln}") if page.uri.to_s == "http://www.washington.edu/students/timeschd/badrequest.html" raise RequestSLNTooSoonError.new("Attempted to fetch SLN data too soon.") elsif page.body.to_s =~ /SLN: #{@sln} does not exist./ raise InvalidSLNError.new("SLN does not exist.") end page end |