Class: AcunoteConnection
- Inherits:
-
Object
- Object
- AcunoteConnection
- Includes:
- Singleton
- Defined in:
- lib/acunote_connection.rb
Overview
A singleton class to contain the acunote session information.
Constant Summary collapse
- SESSION_DIR =
For lack of a better place, put sessions in tmp
"/tmp"
- SESSION_FILE =
"#{SESSION_DIR}/acunote.session"
- LOGIN_FIELDS =
['login[username]', 'login[password]']
- LOGIN_FORM_NAME =
"login_form"
- DEBUG =
false
Instance Attribute Summary collapse
-
#home_url ⇒ Object
The home_url must be set after the instance is first retrieved.
-
#logged_in ⇒ Object
readonly
Returns the value of attribute logged_in.
-
#mech ⇒ Object
readonly
Returns the value of attribute mech.
Instance Method Summary collapse
- #clear_session ⇒ Object
-
#get_page(url, matcher = /.*/, retry_count = 1) ⇒ Object
Retrieves the requested page and verifies destination url to make sure there was no innapropriate redirect.
-
#initialize ⇒ AcunoteConnection
constructor
A new instance of AcunoteConnection.
- #load_session ⇒ Object
- #login(username, password, force = false) ⇒ Object
- #login_url ⇒ Object
- #logout ⇒ Object
- #logout_url ⇒ Object
- #set_timeout(timeout = 60) ⇒ Object
Constructor Details
#initialize ⇒ AcunoteConnection
Returns a new instance of AcunoteConnection.
12 13 14 |
# File 'lib/acunote_connection.rb', line 12 def initialize() @mech ||= Mechanize.new end |
Instance Attribute Details
#home_url ⇒ Object
The home_url must be set after the instance is first retrieved.
25 26 27 28 |
# File 'lib/acunote_connection.rb', line 25 def home_url raise "home_url not set" unless @home_url @home_url end |
#logged_in ⇒ Object (readonly)
Returns the value of attribute logged_in.
10 11 12 |
# File 'lib/acunote_connection.rb', line 10 def logged_in @logged_in end |
#mech ⇒ Object (readonly)
Returns the value of attribute mech.
10 11 12 |
# File 'lib/acunote_connection.rb', line 10 def mech @mech end |
Instance Method Details
#clear_session ⇒ Object
44 45 46 |
# 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) ⇒ Object
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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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 |
#load_session ⇒ Object
38 39 40 41 42 |
# File 'lib/acunote_connection.rb', line 38 def load_session if File.exists?(SESSION_FILE) && ! File.zero?(SESSION_FILE) && mech..load(SESSION_FILE) @logged_in = true end end |
#login(username, password, force = false) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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..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..save_as(SESSION_FILE) @logged_in = true end true end |
#login_url ⇒ Object
30 31 32 |
# File 'lib/acunote_connection.rb', line 30 def login_url "#{self.home_url}/login" end |
#logout ⇒ Object
79 80 81 82 83 84 85 |
# 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 ⇒ Object
34 35 36 |
# File 'lib/acunote_connection.rb', line 34 def logout_url "#{self.home_url}/login/logout" end |
#set_timeout(timeout = 60) ⇒ Object
87 88 89 90 91 92 |
# 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 |