Class: Selenium::WebDriver::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/proxy.rb

Constant Summary collapse

TYPES =
{
  direct: 'DIRECT', # Direct connection, no proxy (default on Windows).
  manual: 'MANUAL', # Manual proxy settings (e.g., for httpProxy).
  pac: 'PAC', # Proxy autoconfiguration from URL.
  auto_detect: 'AUTODETECT', # Proxy autodetection (presumably with WPAD).
  system: 'SYSTEM' # Use system settings (default on Linux).
}.freeze
ALLOWED =
{type: 'proxyType',
http: 'httpProxy',
no_proxy: 'noProxy',
pac: 'proxyAutoconfigUrl',
ssl: 'sslProxy',
auto_detect: 'autodetect',
socks: 'socksProxy',
socks_username: 'socksUsername',
socks_password: 'socksPassword',
socks_version: 'socksVersion'}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Proxy

Returns a new instance of Proxy.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/selenium/webdriver/common/proxy.rb', line 57

def initialize(opts = {})
  not_allowed = []

  opts.each do |k, v|
    if ALLOWED.key?(k)
      send(:"#{k}=", v)
    else
      not_allowed << k
    end
  end

  return if not_allowed.empty?

  raise ArgumentError, "unknown option#{'s' if not_allowed.size != 1}: #{not_allowed.inspect}"
end

Class Method Details

.json_create(data) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/selenium/webdriver/common/proxy.rb', line 44

def self.json_create(data)
  data['proxyType'] = data['proxyType'].downcase.to_sym
  return if data['proxyType'] == :unspecified

  proxy = new

  ALLOWED.each do |k, v|
    proxy.send(:"#{k}=", data[v]) if data.key?(v)
  end

  proxy
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



73
74
75
# File 'lib/selenium/webdriver/common/proxy.rb', line 73

def ==(other)
  other.is_a?(self.class) && as_json == other.as_json
end

#as_jsonObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/selenium/webdriver/common/proxy.rb', line 136

def as_json(*)
  json_result = {
    'proxyType' => TYPES[type].downcase,
    'httpProxy' => http,
    'noProxy' => no_proxy.is_a?(String) ? no_proxy.split(',').map(&:strip).reject(&:empty?) : no_proxy,
    'proxyAutoconfigUrl' => pac,
    'sslProxy' => ssl,
    'autodetect' => auto_detect,
    'socksProxy' => socks,
    'socksUsername' => socks_username,
    'socksPassword' => socks_password,
    'socksVersion' => socks_version
  }.compact

  json_result if json_result.length > 1
end

#auto_detect=(bool) ⇒ Object



98
99
100
101
# File 'lib/selenium/webdriver/common/proxy.rb', line 98

def auto_detect=(bool)
  self.type = :auto_detect
  @auto_detect = bool
end

#http=(value) ⇒ Object



78
79
80
81
# File 'lib/selenium/webdriver/common/proxy.rb', line 78

def http=(value)
  self.type = :manual
  @http = value
end

#no_proxy=(value) ⇒ Object



83
84
85
86
# File 'lib/selenium/webdriver/common/proxy.rb', line 83

def no_proxy=(value)
  self.type = :manual
  @no_proxy = value
end

#pac=(url) ⇒ Object



93
94
95
96
# File 'lib/selenium/webdriver/common/proxy.rb', line 93

def pac=(url)
  self.type = :pac
  @pac = url
end

#socks=(value) ⇒ Object



103
104
105
106
# File 'lib/selenium/webdriver/common/proxy.rb', line 103

def socks=(value)
  self.type = :manual
  @socks = value
end

#socks_password=(value) ⇒ Object



113
114
115
116
# File 'lib/selenium/webdriver/common/proxy.rb', line 113

def socks_password=(value)
  self.type = :manual
  @socks_password = value
end

#socks_username=(value) ⇒ Object



108
109
110
111
# File 'lib/selenium/webdriver/common/proxy.rb', line 108

def socks_username=(value)
  self.type = :manual
  @socks_username = value
end

#socks_version=(value) ⇒ Object



118
119
120
121
# File 'lib/selenium/webdriver/common/proxy.rb', line 118

def socks_version=(value)
  self.type = :manual
  @socks_version = value
end

#ssl=(value) ⇒ Object



88
89
90
91
# File 'lib/selenium/webdriver/common/proxy.rb', line 88

def ssl=(value)
  self.type = :manual
  @ssl = value
end

#to_jsonObject



153
154
155
# File 'lib/selenium/webdriver/common/proxy.rb', line 153

def to_json(*)
  JSON.generate as_json
end

#type=(type) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/selenium/webdriver/common/proxy.rb', line 123

def type=(type)
  unless TYPES.key? type
    raise ArgumentError,
          "invalid proxy type: #{type.inspect}, expected one of #{TYPES.keys.inspect}"
  end

  if defined?(@type) && type != @type
    raise ArgumentError, "incompatible proxy type #{type.inspect} (already set to #{@type.inspect})"
  end

  @type = type
end