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"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAcunoteConnection

Returns a new instance of AcunoteConnection.



13
14
15
# File 'lib/acunote_connection.rb', line 13

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.



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

def logged_in
  @logged_in
end

#mechObject (readonly)

Returns the value of attribute mech.



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

def mech
  @mech
end

#usernameObject

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/acunote_connection.rb', line 95

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

#login(username, password, force = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
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
# File 'lib/acunote_connection.rb', line 38

def (username, password, force = false)
  @logged_in = nil if force
  return true if logged_in

  #Going to assume the session is good to save time here. The session will be
  #discarded and a force login will be performed if get_page fails.
  if !force && File.exists?(SESSION_FILE) && ! File.zero?(SESSION_FILE) && mech.cookie_jar.load(SESSION_FILE)
    STDERR.puts "Loaded session file" if DEBUG
    @username = username
    @logged_in = true
  end

  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)
    @username = username
    @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



75
76
77
78
79
80
81
82
# File 'lib/acunote_connection.rb', line 75

def logout()
  if File.exists?(SESSION_FILE)
    File.delete(SESSION_FILE)
  end
  get_page(logout_url)
  @username = nil
  @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



84
85
86
87
88
89
# File 'lib/acunote_connection.rb', line 84

def set_timeout(timeout = 60)
  mech.keep_alive = false
  mech.open_timeout = timeout
  mech.read_timeout = timeout
  mech.idle_timeout = timeout
end