Class: MyUW::Session

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

Overview

Synopsis

This class encapsulates a MyUW session. It handles the login/logout of the MyUW portal.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(myuw = nil) ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
# File 'lib/myuw/session.rb', line 10

def initialize(myuw=nil)
  @myuw ||= myuw
  
  @email = @password = nil
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#myuwObject

Returns the value of attribute myuw.



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

def myuw
  @myuw
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

Instance Method Details

#logged_in?Boolean

Checks if a user is logged in. This method is actually pretty expensive in terms of time since it actually verifies by going to the MyUW homepage and seeing if the dashboard loads.

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/myuw/session.rb', line 20

def logged_in?
  home = 
  relay_page = home.form_with(:name => 'f').submit()
  !relay_page.search("span.greetingw").empty?
end

#loginObject

Logs a user in using the given email and password

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/myuw/session.rb', line 27

def 
  # Make sure that email and password are filled out or this is
  # useless
  if @email.nil? || @password.nil?
    raise ArgumentError.new("Email and password weren't specified for MyUW session")
  end
  
  # Log out first
  logout

  # Go to the MyUW page and get to the login form
   = 
   = .form_with(:name => 'query')
  raise InvalidPageError.new(, "Login form was not found on the MyUW page") if .nil?
  .user = @email
  .pass = @password
  relay_page = .submit()
  
  # Check if the login failed
  unless relay_page.form_with(:name => 'query').nil?
    return false
  end
  
  # Follow the relay
  follow_relay_page(relay_page)
  true
end

#logoutObject

Log out of the MyUW site



56
57
58
# File 'lib/myuw/session.rb', line 56

def logout
  @myuw.browser.cookie_jar.clear!
end