Class: AcunoteConnection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeAcunoteConnection

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_urlObject

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_inObject (readonly)

Returns the value of attribute logged_in.



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

def logged_in
  @logged_in
end

#mechObject (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_sessionObject



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 && (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_sessionObject



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.cookie_jar.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 (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
     = get_page()
    STDERR.puts "Navigated to '#{.title}'" if DEBUG

    form = .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 == .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_urlObject



30
31
32
# File 'lib/acunote_connection.rb', line 30

def 
  "#{self.home_url}/login"
end

#logoutObject



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_urlObject



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