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.



25
26
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
# File 'lib/steam/browser/html_unit/client.rb', line 25

def initialize(connection = nil, options = {})
  @connection = connection
  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



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

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/steam/browser/html_unit/client.rb', line 23

def connection
  @connection
end

Instance Method Details

#get(request) ⇒ Object



53
54
55
# File 'lib/steam/browser/html_unit/client.rb', line 53

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

#log_level=(level) ⇒ Object

FIXME setLevel throws “Fail: unknown method name ‘setLevel’”. weird.



74
75
76
77
78
79
80
81
# File 'lib/steam/browser/html_unit/client.rb', line 74

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



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

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

#request_settings(request) ⇒ Object



61
62
63
64
65
66
# File 'lib/steam/browser/html_unit/client.rb', line 61

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

#wait_for_javascript(timeout) ⇒ Object



68
69
70
71
# File 'lib/steam/browser/html_unit/client.rb', line 68

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