Class: Akephalos::Client

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

Classes: Cookies, Filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/akephalos/client.rb', line 24

def initialize
  @_client = java.util.concurrent.FutureTask.new do
    client = HtmlUnit::WebClient.new

    Filter.new(client)                           
    client.setThrowExceptionOnFailingStatusCode(false)
    client.setAjaxController(HtmlUnit::NicelyResynchronizingAjaxController.new)
    client.setCssErrorHandler(HtmlUnit::SilentCssErrorHandler.new)
    client.setThrowExceptionOnScriptError(false);
    client
  end
  Thread.new { @_client.run }
end

Instance Attribute Details

#pagePage

Returns the current page.

Returns:

  • (Page)

    the current page



83
84
85
# File 'lib/akephalos/client.rb', line 83

def page
  @page
end

Instance Method Details

#configuration=(config) ⇒ Hash

Note:

This is only used when communicating over DRb, since just a

Set the global configuration settings for Akephalos.

single client instance is exposed.

Parameters:

  • config (Hash)

    the configuration settings

Returns:

  • (Hash)

    the configuration



44
45
46
# File 'lib/akephalos/client.rb', line 44

def configuration=(config)
  Akephalos.configuration = config
end

#cookiesCookies

Returns the cookies for this session.

Returns:

  • (Cookies)

    the cookies for this session



58
59
60
# File 'lib/akephalos/client.rb', line 58

def cookies
  @cookies ||= Cookies.new(client.getCookieManager)
end

#user_agentString

Returns the current user agent string.

Returns:

  • (String)

    the current user agent string



63
64
65
# File 'lib/akephalos/client.rb', line 63

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.

Parameters:

  • user_agent (:default)

    the default user agent

  • user_agent (String)

    the user agent string to use



72
73
74
75
76
77
78
79
80
# File 'lib/akephalos/client.rb', line 72

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

#visit(url) ⇒ Page

Visit the requested URL and return the page.

Parameters:

  • url (String)

    the URL to load

Returns:

  • (Page)

    the loaded page



52
53
54
55
# File 'lib/akephalos/client.rb', line 52

def visit(url)
  client.getPage(url)
  page
end