Class: Ferrum::Browser::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/browser/options.rb,
lib/ferrum/browser/options/base.rb,
lib/ferrum/browser/options/chrome.rb,
lib/ferrum/browser/options/firefox.rb

Defined Under Namespace

Classes: Base, Chrome, Firefox

Constant Summary collapse

BROWSER_PORT =
"0"
BROWSER_HOST =
"127.0.0.1"
WINDOW_SIZE =
[1024, 768].freeze
BASE_URL_SCHEMA =
%w[http https].freeze
DEFAULT_TIMEOUT =
ENV.fetch("FERRUM_DEFAULT_TIMEOUT", 5).to_i
PROCESS_TIMEOUT =
ENV.fetch("FERRUM_PROCESS_TIMEOUT", 10).to_i
DEBUG_MODE =
!ENV.fetch("FERRUM_DEBUG", nil).nil?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Options

Returns a new instance of Options.



21
22
23
24
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
52
# File 'lib/ferrum/browser/options.rb', line 21

def initialize(options = nil)
  @options = Hash(options&.dup)

  @port = @options.fetch(:port, BROWSER_PORT)
  @host = @options.fetch(:host, BROWSER_HOST)
  @timeout = @options.fetch(:timeout, DEFAULT_TIMEOUT)
  @window_size = @options.fetch(:window_size, WINDOW_SIZE)
  @js_errors = @options.fetch(:js_errors, false)
  @headless = @options.fetch(:headless, true)
  @incognito = @options.fetch(:incognito, true)
  @flatten = @options.fetch(:flatten, true)
  @pending_connection_errors = @options.fetch(:pending_connection_errors, true)
  @process_timeout = @options.fetch(:process_timeout, PROCESS_TIMEOUT)
  @slowmo = @options[:slowmo].to_f

  @env = @options[:env]
  @xvfb = @options[:xvfb]
  @save_path = @options[:save_path]
  @browser_name = @options[:browser_name]
  @browser_path = @options[:browser_path]
  @ws_max_receive_size = @options[:ws_max_receive_size]
  @ignore_default_browser_options = @options[:ignore_default_browser_options]

  @proxy = validate_proxy(@options[:proxy])
  @logger = parse_logger(@options[:logger])
  @base_url = parse_base_url(@options[:base_url]) if @options[:base_url]
  @url = @options[:url].to_s if @options[:url]
  @ws_url = @options[:ws_url].to_s if @options[:ws_url]

  @options = @options.merge(window_size: @window_size).freeze
  @browser_options = @options.fetch(:browser_options, {}).freeze
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def base_url
  @base_url
end

#browser_nameObject (readonly)

Returns the value of attribute browser_name.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def browser_name
  @browser_name
end

#browser_optionsObject (readonly)

Returns the value of attribute browser_options.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def browser_options
  @browser_options
end

#browser_pathObject (readonly)

Returns the value of attribute browser_path.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def browser_path
  @browser_path
end

#default_user_agentObject

Returns the value of attribute default_user_agent.



19
20
21
# File 'lib/ferrum/browser/options.rb', line 19

def default_user_agent
  @default_user_agent
end

#envObject (readonly)

Returns the value of attribute env.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def env
  @env
end

#flattenObject (readonly)

Returns the value of attribute flatten.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def flatten
  @flatten
end

#headlessObject (readonly)

Returns the value of attribute headless.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def headless
  @headless
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def host
  @host
end

#ignore_default_browser_optionsObject (readonly)

Returns the value of attribute ignore_default_browser_options.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def ignore_default_browser_options
  @ignore_default_browser_options
end

#incognitoObject (readonly)

Returns the value of attribute incognito.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def incognito
  @incognito
end

#js_errorsObject (readonly)

Returns the value of attribute js_errors.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def js_errors
  @js_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def logger
  @logger
end

#pending_connection_errorsObject (readonly)

Returns the value of attribute pending_connection_errors.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def pending_connection_errors
  @pending_connection_errors
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def port
  @port
end

#process_timeoutObject (readonly)

Returns the value of attribute process_timeout.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def process_timeout
  @process_timeout
end

#proxyObject (readonly)

Returns the value of attribute proxy.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def proxy
  @proxy
end

#save_pathObject (readonly)

Returns the value of attribute save_path.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def save_path
  @save_path
end

#slowmoObject (readonly)

Returns the value of attribute slowmo.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def slowmo
  @slowmo
end

#timeoutObject

Returns the value of attribute timeout.



19
20
21
# File 'lib/ferrum/browser/options.rb', line 19

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def url
  @url
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def window_size
  @window_size
end

#ws_max_receive_sizeObject (readonly)

Returns the value of attribute ws_max_receive_size.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def ws_max_receive_size
  @ws_max_receive_size
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def ws_url
  @ws_url
end

#xvfbObject (readonly)

Returns the value of attribute xvfb.



14
15
16
# File 'lib/ferrum/browser/options.rb', line 14

def xvfb
  @xvfb
end

Instance Method Details

#extensionsObject



58
59
60
61
62
# File 'lib/ferrum/browser/options.rb', line 58

def extensions
  @extensions ||= Array(@options[:extensions]).map do |extension|
    (extension.is_a?(Hash) && extension[:source]) || File.read(extension)
  end
end

#to_hObject



76
77
78
# File 'lib/ferrum/browser/options.rb', line 76

def to_h
  @options
end

#validate_proxy(options) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ferrum/browser/options.rb', line 64

def validate_proxy(options)
  return unless options

  raise ArgumentError, "proxy options must be a Hash" unless options.is_a?(Hash)

  if options[:host].nil? && options[:port].nil?
    raise ArgumentError, "proxy options must be a Hash with at least :host | :port"
  end

  options
end