class AcunoteConnection
A singleton class to contain the acunote session information.
Constants
- DEBUG
- LOGIN_FIELDS
- LOGIN_FORM_NAME
- SESSION_DIR
For lack of a better place, put sessions in tmp
- SESSION_FILE
Attributes
home_url[W]
logged_in[R]
mech[R]
Public Class Methods
new()
click to toggle source
# File lib/acunote_connection.rb, line 12 def initialize() @mech ||= Mechanize.new end
Public Instance Methods
clear_session()
click to toggle source
# File lib/acunote_connection.rb, line 44 def clear_session File.delete(SESSION_FILE) if File.exists?(SESSION_FILE) end
get_page(url, matcher = /.*/, retry_count = 1)
click to toggle source
Retrieves the requested page and verifies destination url to make sure there was no innapropriate redirect. If redirected, a force login will be performed (assuming credentials are passed in as arguments) and the page will be retrieved again.
# File lib/acunote_connection.rb, line 98 def get_page(url, matcher = /.*/, retry_count = 1) begin page = mech.get(url) if page.uri.to_s =~ matcher page else #try a force login and retry once (in case the session is stale) if retry_count > 0 && login(true) STDERR.puts "Attn: get_page problem, overwrote stale session, retrying..." get_page(url, matcher, retry_count - 1) else STDERR.puts "Error: Can't retrieve valid response page for <#{url}>" end end rescue Mechanize::ResponseCodeError => e STDERR.puts "ResponseError!" puts url if DEBUG puts e if DEBUG end end
home_url()
click to toggle source
The home_url
must be set after the instance is first retrieved.
# File lib/acunote_connection.rb, line 25 def home_url raise "home_url not set" unless @home_url @home_url end
load_session()
click to toggle source
# File lib/acunote_connection.rb, line 38 def load_session if File.exists?(SESSION_FILE) && ! File.zero?(SESSION_FILE) && mech.cookie_jar.load(SESSION_FILE) @logged_in = true end end
login(username, password, force = false)
click to toggle source
# File lib/acunote_connection.rb, line 48 def login(username, password, force = false) @logged_in = nil if force return true if logged_in # Try to load an existing session. load_session unless force unless logged_in #try to log in login_page = get_page(login_url) STDERR.puts "Navigated to '#{login_page.title}'" if DEBUG form = login_page.forms.first form[LOGIN_FIELDS[0]] = username form[LOGIN_FIELDS[1]] = password dest_page = form.submit(form.buttons.first) STDERR.puts "Navigated to '#{dest_page.title}'" if DEBUG if dest_page.uri == login_page.uri STDERR.puts "Error: Bad login!" return false end #serialize session and save for later reuse mech.cookie_jar.save_as(SESSION_FILE) @logged_in = true end true end
login_url()
click to toggle source
# File lib/acunote_connection.rb, line 30 def login_url "#{self.home_url}/login" end
logout()
click to toggle source
# File lib/acunote_connection.rb, line 79 def logout() if File.exists?(SESSION_FILE) File.delete(SESSION_FILE) end get_page(logout_url) @logged_in = false end
logout_url()
click to toggle source
# File lib/acunote_connection.rb, line 34 def logout_url "#{self.home_url}/login/logout" end
set_timeout(timeout = 60)
click to toggle source
# File lib/acunote_connection.rb, line 87 def set_timeout(timeout = 60) mech.keep_alive = false mech.open_timeout = timeout mech.read_timeout = timeout mech.idle_timeout = timeout end