Class: MineShaft::UserAgent

Inherits:
Object
  • Object
show all
Defined in:
lib/mine_shaft/user_agent.rb

Overview

Acts as a headless browser to log in and interact with a Redmine site.

Constant Summary collapse

LOGIN_ACTION =

The action on relative URL to hit for the login page on a Redmine site.

'/login'

Instance Method Summary collapse

Constructor Details

#initialize(username, password, base_uri) ⇒ UserAgent

Public: Creates a new instance of the UserAgent class.

username - The username to use for logging into the Redmine site. password - The password to use for logging into the Redmine site. base_uri - The URL of the Redmine site.

Returns a new instance of the UserAgent class.



14
15
16
17
18
19
20
# File 'lib/mine_shaft/user_agent.rb', line 14

def initialize(username, password, base_uri)
  @username = username
  @password = password
  @base_uri = base_uri
  @agent = Mechanize.new
  @logged_in = false
end

Instance Method Details

#get(page) ⇒ Object

Public: Retrieves the specified page from the Redmine site.

page - The relative URL of the page to retrieve as a String.

Returns the page pased into a Mechanize::Page object.



27
28
29
# File 'lib/mine_shaft/user_agent.rb', line 27

def get(page)
  @current_page = @agent.get("#{@base_uri}#{page}")
end

#log_inObject

Public: Logs into the Redmine site using credentials specified on object

instantiation.

Returns true if login process was successful. Raises FailedLogin if the login was not successful. Raises InvalidPage if the specified site returns a 404 response code.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mine_shaft/user_agent.rb', line 37

def 
  return true if logged_in?
  
  submit(, .buttons.first)

  if 
    raise FailedLogin, "Login failed. Please verify username & password"
  end
  @logged_in = true
rescue Mechanize::ResponseCodeError
  raise InvalidPage, "'#{@base_uri}' is returning a 404. Please verify the URL is a functioning Redmine installation"
end

#logged_in?Boolean

Public: Confirms whether or not the UserAgent instance is logged in.

Returns true if the UserAgent is currently logged in. Returns false if the UserAgent is not currently logged in.

Returns:

  • (Boolean)


66
67
68
# File 'lib/mine_shaft/user_agent.rb', line 66

def logged_in?
  @logged_in
end

#submit(form, button) ⇒ Object

Public: Submits the specified form by clicking the specified button on

said form.

form - A Mechanize::Form object. button - A Mechanize::Form::Submit object.

Returns the page resulting from the submission process as a

Mechanize::Page object.


58
59
60
# File 'lib/mine_shaft/user_agent.rb', line 58

def submit(form, button)
  @current_page = @agent.submit(form, button)
end