Class: Akephalos::Client
- Inherits:
-
Object
- Object
- Akephalos::Client
- Defined in:
- lib/akephalos/client.rb,
lib/akephalos/client/filter.rb,
lib/akephalos/client/cookies.rb
Overview
Akephalos::Client wraps HtmlUnit’s WebClient class. It is the main entry point for all interaction with the browser, exposing its current page and allowing navigation.
Defined Under Namespace
Constant Summary collapse
- DEFAULT_OPTIONS =
The default configuration options for a new Client.
{ :browser => :firefox_3_6, :validate_scripts => true }
- BROWSER_VERSIONS =
Map of browser version symbols to their HtmlUnit::BrowserVersion instances.
{ :ie6 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_6, :ie7 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_7, :ie8 => HtmlUnit::BrowserVersion::INTERNET_EXPLORER_8, :firefox_3 => HtmlUnit::BrowserVersion::FIREFOX_3, :firefox_3_6 => HtmlUnit::BrowserVersion::FIREFOX_3_6 }
Instance Attribute Summary collapse
-
#browser_version ⇒ HtmlUnit::BrowserVersion
readonly
The configured browser version.
-
#page ⇒ Page
The current page.
-
#validate_scripts ⇒ true/false
readonly
Whether to raise errors on javascript failures.
Instance Method Summary collapse
-
#cookies ⇒ Cookies
The cookies for this session.
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#process_options!(options) ⇒ Object
Merges the DEFAULT_OPTIONS with those provided to initialize the Client state, namely, its browser version and whether it should validate scripts.
-
#user_agent ⇒ String
The current user agent string.
-
#user_agent=(user_agent) ⇒ Object
Set the User-Agent header for this session.
-
#validate_scripts? ⇒ true, false
Whether javascript errors will raise exceptions.
-
#visit(url) ⇒ Page
Visit the requested URL and return the page.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/akephalos/client.rb', line 55 def initialize( = {}) () @_client = java.util.concurrent.FutureTask.new do client = HtmlUnit::WebClient.new(browser_version) Filter.new(client) client.setThrowExceptionOnFailingStatusCode(false) client.setAjaxController(HtmlUnit::NicelyResynchronizingAjaxController.new) client.setCssErrorHandler(HtmlUnit::SilentCssErrorHandler.new) client.setThrowExceptionOnScriptError(validate_scripts) client end Thread.new { @_client.run } end |
Instance Attribute Details
#browser_version ⇒ HtmlUnit::BrowserVersion (readonly)
Returns the configured browser version.
27 28 29 |
# File 'lib/akephalos/client.rb', line 27 def browser_version @browser_version end |
#page ⇒ Page
Returns the current page.
24 25 26 |
# File 'lib/akephalos/client.rb', line 24 def page @page end |
#validate_scripts ⇒ true/false (readonly)
Returns whether to raise errors on javascript failures.
30 31 32 |
# File 'lib/akephalos/client.rb', line 30 def validate_scripts @validate_scripts end |
Instance Method Details
#cookies ⇒ Cookies
Returns the cookies for this session.
81 82 83 |
# File 'lib/akephalos/client.rb', line 81 def @cookies ||= Cookies.new(client.getCookieManager) end |
#process_options!(options) ⇒ Object
Merges the DEFAULT_OPTIONS with those provided to initialize the Client state, namely, its browser version and whether it should validate scripts.
132 133 134 135 136 137 |
# File 'lib/akephalos/client.rb', line 132 def () = DEFAULT_OPTIONS.merge() @browser_version = BROWSER_VERSIONS.fetch(.delete(:browser)) @validate_scripts = .delete(:validate_scripts) end |
#user_agent ⇒ String
Returns the current user agent string.
86 87 88 |
# File 'lib/akephalos/client.rb', line 86 def user_agent @user_agent || client.getBrowserVersion.getUserAgent end |
#user_agent=(user_agent) ⇒ Object
Set the User-Agent header for this session. If :default is given, the User-Agent header will be reset to the default browser’s user agent.
95 96 97 98 99 100 101 102 103 |
# File 'lib/akephalos/client.rb', line 95 def user_agent=(user_agent) if user_agent == :default @user_agent = nil client.removeRequestHeader("User-Agent") else @user_agent = user_agent client.addRequestHeader("User-Agent", user_agent) end end |
#validate_scripts? ⇒ true, false
Returns whether javascript errors will raise exceptions.
123 124 125 |
# File 'lib/akephalos/client.rb', line 123 def validate_scripts? !!validate_scripts end |
#visit(url) ⇒ Page
Visit the requested URL and return the page.
75 76 77 78 |
# File 'lib/akephalos/client.rb', line 75 def visit(url) client.getPage(url) page end |