Class: MyUW::CurriculumEnrollment

Inherits:
Object
  • Object
show all
Defined in:
lib/myuw/curriculum_enrollment.rb

Overview

Synopsis

Gets information regarding enrollment within a specific subject.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(myuw) ⇒ CurriculumEnrollment

Returns a new instance of CurriculumEnrollment.



11
12
13
14
15
16
# File 'lib/myuw/curriculum_enrollment.rb', line 11

def initialize(myuw)
  @myuw = myuw
  @term = nil
  @data = nil
  @curriculum = nil
end

Instance Attribute Details

#curriculumObject

Returns the value of attribute curriculum.



7
8
9
# File 'lib/myuw/curriculum_enrollment.rb', line 7

def curriculum
  @curriculum
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/myuw/curriculum_enrollment.rb', line 9

def data
  @data
end

#myuwObject

Returns the value of attribute myuw.



8
9
10
# File 'lib/myuw/curriculum_enrollment.rb', line 8

def myuw
  @myuw
end

#termObject

Returns the value of attribute term.



6
7
8
# File 'lib/myuw/curriculum_enrollment.rb', line 6

def term
  @term
end

Instance Method Details

#data_by_sln(sln, reload = false) ⇒ Object

Get data for a specific SLN



43
44
45
46
# File 'lib/myuw/curriculum_enrollment.rb', line 43

def data_by_sln(sln, reload = false)
  fetch_data if @data.nil? || reload
  @data[sln]
end

#extract_info(keys, nodes) ⇒ Object

Extracts course information from each node and stores it as the proper key in the @data variable



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

def extract_info(keys, nodes)
  return false if nodes.empty?
  
  data = {}
  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_key] = node.inner_text.strip.gsub("\302\240", "")
      end
    end
  end
  
  data
end

#fetch_dataObject

Fetches the information for the given SLN.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'lib/myuw/curriculum_enrollment.rb', line 31

def fetch_data
  raise ArgumentError.new("Curriculum not set.") if @curriculum.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_dept_page

  # Get the actual course information
end

#get_all_courses_data(page) ⇒ Object

Gets and stores all course data

Raises:



60
61
62
63
64
65
66
67
68
69
# File 'lib/myuw/curriculum_enrollment.rb', line 60

def get_all_courses_data(page)
  nodes = page.search('//table//tr[count(th)>1 and @bgcolor="#d0d0d0"]//following-sibling::tr')
  raise InvalidPageError.new("Couldn't find any courses listed.") if nodes.empty?
  
  @data = {}
  nodes.each do |node|
    node_data = get_course_data(node)
    @data[node_data[:sln]] = node_data
  end
end

#get_course_data(row) ⇒ Object

Gets a single course row’s data

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/myuw/curriculum_enrollment.rb', line 72

def get_course_data(row)
  nodes = row.search('td')
  raise InvalidPageError.new("Couldn't find course information for a single row.") if nodes.empty?
  
  data_keys = [:sln, :course, :section, :type, :title, :current_enrollment, :limit_enrollment,
      :room_capacity, :space_available, nil, :notes]
  
  data = extract_info(data_keys, nodes)
  raise InvalidPageError.new("Failed to extract data for single row.") if !data
  
  data
end

#get_dept_pageObject

Gets the SLN info page for the SLN and term, raising an exception if it is requested too soon



50
51
52
53
54
55
56
57
# File 'lib/myuw/curriculum_enrollment.rb', line 50

def get_dept_page
  page = @myuw.browser.get("https://sdb.admin.washington.edu/timeschd/uwnetid/tsstat.asp?QTRYR=#{@term}&CURRIC=#{@curriculum}")
  if page.body.to_s =~ /No sections found for #{@curriculum}./
    raise InvalidCurriculumError.new("Curriculum does not exist.")
  end
  
  page
end