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

Overview

Resolves and normalizes the options hash passed to Browser.new, applying defaults for connection settings (host, port, timeouts), window size, and other launch behavior. Used throughout the browser to look up user-configured settings.

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
DEFAULT_PROTOCOL_TIMEOUT =
ENV.fetch("FERRUM_PROTOCOL_TIMEOUT", DEFAULT_TIMEOUT).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



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
55
56
57
58
59
60
61
# File 'lib/ferrum/browser/options.rb', line 28

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)
  @protocol_timeout = @options.fetch(:protocol_timeout, DEFAULT_PROTOCOL_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)
  @dockerize = @options.fetch(:dockerize, false)
  @flatten = @options.fetch(:flatten, true)
  @pending_connection_errors = @options.fetch(:pending_connection_errors, false)
  @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.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def base_url
  @base_url
end

#browser_nameObject (readonly)

Returns the value of attribute browser_name.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def browser_name
  @browser_name
end

#browser_optionsObject (readonly)

Returns the value of attribute browser_options.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def browser_options
  @browser_options
end

#browser_pathObject (readonly)

Returns the value of attribute browser_path.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def browser_path
  @browser_path
end

#default_user_agentObject

Returns the value of attribute default_user_agent.



26
27
28
# File 'lib/ferrum/browser/options.rb', line 26

def default_user_agent
  @default_user_agent
end

#dockerizeObject (readonly)

Returns the value of attribute dockerize.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def dockerize
  @dockerize
end

#envObject (readonly)

Returns the value of attribute env.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def env
  @env
end

#flattenObject (readonly)

Returns the value of attribute flatten.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def flatten
  @flatten
end

#headlessObject (readonly)

Returns the value of attribute headless.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def headless
  @headless
end

#hostObject (readonly)

Returns the value of attribute host.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def host
  @host
end

#ignore_default_browser_optionsObject (readonly)

Returns the value of attribute ignore_default_browser_options.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def ignore_default_browser_options
  @ignore_default_browser_options
end

#incognitoObject (readonly)

Returns the value of attribute incognito.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def incognito
  @incognito
end

#js_errorsObject (readonly)

Returns the value of attribute js_errors.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def js_errors
  @js_errors
end

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def logger
  @logger
end

#pending_connection_errorsObject (readonly)

Returns the value of attribute pending_connection_errors.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def pending_connection_errors
  @pending_connection_errors
end

#portObject (readonly)

Returns the value of attribute port.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def port
  @port
end

#process_timeoutObject (readonly)

Returns the value of attribute process_timeout.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def process_timeout
  @process_timeout
end

#protocol_timeoutObject

Returns the value of attribute protocol_timeout.



26
27
28
# File 'lib/ferrum/browser/options.rb', line 26

def protocol_timeout
  @protocol_timeout
end

#proxyObject (readonly)

Returns the value of attribute proxy.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def proxy
  @proxy
end

#save_pathObject (readonly)

Returns the value of attribute save_path.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def save_path
  @save_path
end

#slowmoObject (readonly)

Returns the value of attribute slowmo.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def slowmo
  @slowmo
end

#timeoutObject

Returns the value of attribute timeout.



26
27
28
# File 'lib/ferrum/browser/options.rb', line 26

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def url
  @url
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def window_size
  @window_size
end

#ws_max_receive_sizeObject (readonly)

Returns the value of attribute ws_max_receive_size.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def ws_max_receive_size
  @ws_max_receive_size
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def ws_url
  @ws_url
end

#xvfbObject (readonly)

Returns the value of attribute xvfb.



21
22
23
# File 'lib/ferrum/browser/options.rb', line 21

def xvfb
  @xvfb
end

Instance Method Details

#extensionsArray<String>

JS source to preload into the browser, read from :extensions option.



81
82
83
84
85
# File 'lib/ferrum/browser/options.rb', line 81

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

#to_hHash{Symbol => Object}

Raw options hash used to initialize the browser.



116
117
118
# File 'lib/ferrum/browser/options.rb', line 116

def to_h
  @options
end

#validate_proxy(options) ⇒ Hash?

Validates the :proxy option, if given.

Raises:

  • (ArgumentError)

    If options is not a Hash, or is a Hash without :host or :port.



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ferrum/browser/options.rb', line 99

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