Class: Undead::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/undead/driver.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Driver

Returns a new instance of Driver.



11
12
13
14
# File 'lib/undead/driver.rb', line 11

def initialize(options = {})
  @options = options
  headers = options.fetch(:headers, {})
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/undead/driver.rb', line 9

def options
  @options
end

Instance Method Details

#browserObject



16
17
18
19
20
21
22
23
# File 'lib/undead/driver.rb', line 16

def browser
  @browser ||= begin
    browser = Undead::Browser.new(server, client, logger)
    browser.js_errors  = options[:js_errors] if options.key?(:js_errors)
    browser.debug      = true if options[:debug]
    browser
  end
end

#clientObject



29
30
31
32
33
34
35
36
# File 'lib/undead/driver.rb', line 29

def client
  @client ||= Undead::Client.start(server,
    :path              => options[:phantomjs],
    :window_size       => options[:window_size],
    :phantomjs_options => phantomjs_options,
    :phantomjs_logger  => phantomjs_logger
  )
end

#headersObject



57
58
59
# File 'lib/undead/driver.rb', line 57

def headers
  browser.get_headers
end

#headers=(headers) ⇒ Object



61
62
63
# File 'lib/undead/driver.rb', line 61

def headers=(headers)
  browser.set_headers(headers)
end

#htmlObject



69
70
71
# File 'lib/undead/driver.rb', line 69

def html
  browser.body
end

#loggerObject



49
50
51
# File 'lib/undead/driver.rb', line 49

def logger
  options[:logger] || (options[:debug] && STDERR)
end

#phantomjs_loggerObject



53
54
55
# File 'lib/undead/driver.rb', line 53

def phantomjs_logger
  options.fetch(:phantomjs_logger, nil)
end

#phantomjs_optionsObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/undead/driver.rb', line 38

def phantomjs_options
  list = options[:phantomjs_options] || []

  # PhantomJS defaults to only using SSLv3, which since POODLE (Oct 2014)
  # many sites have dropped from their supported protocols (eg PayPal,
  # Braintree).
  list += ["--ignore-ssl-errors=yes"] unless list.grep(/ignore-ssl-errors/).any?
  list += ["--ssl-protocol=TLSv1"] unless list.grep(/ssl-protocol/).any?
  list
end

#serverObject



25
26
27
# File 'lib/undead/driver.rb', line 25

def server
  @server ||= Undead::Server.new(options[:port], options.fetch(:timeout) { DEFAULT_TIMEOUT })
end

#visit(url) ⇒ Object



65
66
67
# File 'lib/undead/driver.rb', line 65

def visit(url)
  browser.visit(url)
end