Class: Steam::Browser::HtmlUnit::Client

Inherits:
Object
  • Object
show all
Includes:
Java::Com::Gargoylesoftware::Htmlunit
Defined in:
lib/steam/browser/html_unit/client.rb

Defined Under Namespace

Classes: SilencingListener

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection = nil, options = {}) ⇒ Client

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/steam/browser/html_unit/client.rb', line 27

def initialize(connection = nil, options = {})
  @connection = connection
  @handlers   = {}
  options = Steam.config[:html_unit].merge(options)

  @java = WebClient.new(BrowserVersion.send(options[:browser_version]))
  @java.setCssEnabled(options[:css])
  @java.setJavaScriptEnabled(options[:javascript])
  @java.setPrintContentOnFailingStatusCode(options[:on_error_status] == :print)
  @java.setThrowExceptionOnFailingStatusCode(options[:on_error_status] == :raise)
  @java.setThrowExceptionOnScriptError(options[:on_script_error] == :raise)

  self.log_level = options[:log_level]
  unless options[:log_incorrectness]
    listener = Rjb::bind(SilencingListener.new, 'com.gargoylesoftware.htmlunit.IncorrectnessListener')
    @java.setIncorrectnessListener(listener)
  end

  if options[:resynchronize]
    controller = NicelyResynchronizingAjaxController.new
    @java.setAjaxController(controller)
  end

  if connection
    connection = Rjb::bind(HtmlUnit::Connection.new(connection), 'com.gargoylesoftware.htmlunit.WebConnection')
    @java.setWebConnection(connection)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



109
110
111
# File 'lib/steam/browser/html_unit/client.rb', line 109

def method_missing(method, *args)
  @java.send(method, *args)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



25
26
27
# File 'lib/steam/browser/html_unit/client.rb', line 25

def connection
  @connection
end

#handlersObject (readonly)

Returns the value of attribute handlers.



25
26
27
# File 'lib/steam/browser/html_unit/client.rb', line 25

def handlers
  @handlers
end

Instance Method Details

TODO what about domain, path, expires etc?



89
90
91
92
93
94
# File 'lib/steam/browser/html_unit/client.rb', line 89

def add_cookie(name, value) # TODO what about domain, path, expires etc?
  cookie = Cookie.new
  cookie.setName(name)
  cookie.setValue(value)
  @java.getCookieManager.addCookie(cookie)
end

#clear_cookiesObject



96
97
98
# File 'lib/steam/browser/html_unit/client.rb', line 96

def clear_cookies
  @java.getCookieManager.clearCookies
end

#get(request) ⇒ Object



56
57
58
# File 'lib/steam/browser/html_unit/client.rb', line 56

def get(request)
  perform(self.request_settings(request))
end


84
85
86
87
# File 'lib/steam/browser/html_unit/client.rb', line 84

def get_cookie(name)
  cookie = @java.getCookieManager.getCookie(name)
  cookie ? cookie.getValue : nil
end

#log_level=(level) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/steam/browser/html_unit/client.rb', line 100

def log_level=(level)
  [ 'com.gargoylesoftware.htmlunit',
    'com.gargoylesoftware.htmlunit.html',
    'com.gargoylesoftware.htmlunit.javascript',
    'org.apache.commons.httpclient'
  ].each { |classifier|
    Java.logger(classifier).setLevel(Java.log_level(level)) }
end

#perform(request_settings) ⇒ Object



60
61
62
# File 'lib/steam/browser/html_unit/client.rb', line 60

def perform(request_settings)
  @java._invoke('getPage', 'Lcom.gargoylesoftware.htmlunit.WebRequestSettings;', request_settings)
end

#remove_handler(type) ⇒ Object



80
81
82
# File 'lib/steam/browser/html_unit/client.rb', line 80

def remove_handler(type)
  @java.send(:"set#{type.to_s.camelize}Handler", nil)
end

#request_settings(request) ⇒ Object



64
65
66
67
68
69
# File 'lib/steam/browser/html_unit/client.rb', line 64

def request_settings(request)
  url      = Java::Net::Url.new(request.url)
  settings = WebRequestSettings.new(url)
  request.headers.each { |name, value| settings.setAdditionalHeader(name.to_s, value.to_s) } if request.headers
  settings
end

#set_handler(type, &block) ⇒ Object



76
77
78
# File 'lib/steam/browser/html_unit/client.rb', line 76

def set_handler(type, &block)
  @java.send(:"set#{type.to_s.camelize}Handler", Handler.create(type, block))
end

#wait_for_javascript(timeout) ⇒ Object



71
72
73
74
# File 'lib/steam/browser/html_unit/client.rb', line 71

def wait_for_javascript(timeout)
  waitForBackgroundJavaScript(timeout)
  yield if block_given?
end