Class: MyUW::Registration

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

Overview

Synopsis

The Registration class can automate the registration process for a user. It does NOT support initial setup of the registration process such as verifying addresses and such (and doesnt plan to, that is an important step that should be done manually)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(myuw = nil) ⇒ Registration

Returns a new instance of Registration.



12
13
14
15
# File 'lib/myuw/registration.rb', line 12

def initialize(myuw=nil)
  @myuw ||= myuw
  @errors = nil
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/myuw/registration.rb', line 10

def errors
  @errors
end

#myuwObject

Returns the value of attribute myuw.



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

def myuw
  @myuw
end

Instance Method Details

#extract_registration_errors(result_page) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/myuw/registration.rb', line 44

def extract_registration_errors(result_page)
  # //input[@type='hidden' and @name='action4']/following-sibling::input[@type='hidden']/following-sibling::td
  registration_form = result_page.form_with(:name => 'regform')
  start_index = get_add_section_index(registration_form)
  current_index = start_index.to_i
  
  @errors = {}
  
  loop do
    # Get current SLN, and if its empty, then return out
    current_sln = registration_form["sln#{current_index}"]
    return if current_sln.nil? || current_sln.empty?
    
    error_cell = result_page.search("//input[@name='action#{current_index}']/parent::tr//td[5]")
    raise InvalidPageError.new(result_page, "Couldn't extract errors (couldn't get data cells)") if error_cell.empty?
    
    error_text = error_cell[0].inner_text.strip.gsub("\302\240", '')
    @errors[current_sln] = error_text
    
    current_index += 1
  end
end

#fill_registration_form(register_form, courses) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/myuw/registration.rb', line 67

def fill_registration_form(register_form, courses)
  # Determine where to begin adding SLNs (what fields)
  start_index = get_add_section_index(register_form)
  
  current_index = start_index.to_i
  courses.each do |course|
    register_form["sln#{current_index}"] = course.to_s
    current_index += 1
  end
end

#get_add_section_index(register_form) ⇒ Object

The registration page is one giant form. We need to figure out which index fields to begin filling in with SLN data. This method returns the index.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/myuw/registration.rb', line 82

def get_add_section_index(register_form)
  # First way is to add maxadds and maxdrops and
  # determine the offset.
  maxdrops = register_form["maxdrops"].to_i
  index_by_hiddens = maxdrops + 1
  
  # Verify by inspecting actions
  1.upto(11) do |i|
    # Find the version action with an "A" (meaning add)
    if register_form["action#{i}"].to_s == "A"
      raise InvalidPageError.new(nil, "Couldn't determine add offset.") unless i == index_by_hiddens
      return i.to_s
    end
  end
end

#get_registration_result(result_page) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
# File 'lib/myuw/registration.rb', line 35

def get_registration_result(result_page)
  return true if result_page.body =~ /ALT="OK">Schedule updated\./i
  raise InvalidPageError.new(result_page, "Unknown registration result.") unless result_page.body =~ /ALT="ERROR">Schedule not updated\./i
  
  # Otherwise, get the errors out of the result page
  extract_registration_errors(result_page)
  false
end

#register(courses) ⇒ Object

Registers for one or more courses. The courses parameter should be an Array of strings (the SLNs)

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/myuw/registration.rb', line 20

def register(courses)
  raise NotLoggedInError.new("User must be logged in to register for classes.") unless @myuw.logged_in?
  
  # Get registration page
  register_page = @myuw.browser.get("https://sdb.admin.washington.edu/students/uwnetid/register.asp")
  register_form = register_page.form_with(:name => 'regform')
  raise InvalidPageError.new(register_page, "Could not verify registration page") if register_form.nil?
  
  # Fill in the form
  fill_registration_form(register_form, courses)
  
  # Submit it and analyze result
  get_registration_result(register_form.submit())
end