@@ -21,40 +21,52 @@ module Selenium
21
21
module WebDriver
22
22
class Proxy
23
23
TYPES = {
24
- direct : 'DIRECT' , # Direct connection, no proxy (default on Windows).
25
- manual : 'MANUAL' , # Manual proxy settings (e.g., for httpProxy).
24
+ direct : 'DIRECT' , # Direct connection, no proxy (default on Windows).
25
+ manual : 'MANUAL' , # Manual proxy settings (e.g., for httpProxy).
26
26
pac : 'PAC' , # Proxy autoconfiguration from URL.
27
27
auto_detect : 'AUTODETECT' , # Proxy autodetection (presumably with WPAD).
28
28
system : 'SYSTEM' # Use system settings (default on Linux).
29
29
} . freeze
30
30
31
- attr_reader :type ,
32
- :ftp ,
33
- :http ,
34
- :socks ,
35
- :socks_username ,
36
- :socks_password ,
37
- :no_proxy ,
38
- :pac ,
39
- :ssl ,
40
- :auto_detect
31
+ ALLOWED = { type : 'proxyType' ,
32
+ ftp : 'ftpProxy' ,
33
+ http : 'httpProxy' ,
34
+ no_proxy : 'noProxy' ,
35
+ pac : 'proxyAutoconfigUrl' ,
36
+ ssl : 'sslProxy' ,
37
+ auto_detect : 'autodetect' ,
38
+ socks : 'socksProxy' ,
39
+ socks_username : 'socksUsername' ,
40
+ socks_password : 'socksPassword' } . freeze
41
+
42
+ ALLOWED . keys . each { |t | attr_reader t }
43
+
44
+ def self . json_create ( data )
45
+ data [ 'proxyType' ] = data [ 'proxyType' ] . downcase . to_sym
46
+ return if data [ 'proxyType' ] == :unspecified
47
+
48
+ proxy = new
49
+
50
+ ALLOWED . each do |k , v |
51
+ proxy . send ( "#{ k } =" , data [ v ] ) if data . key? ( v )
52
+ end
53
+
54
+ proxy
55
+ end
41
56
42
57
def initialize ( opts = { } )
43
- opts = opts . dup
44
-
45
- self . type = opts . delete ( :type ) if opts . key? :type
46
- self . ftp = opts . delete ( :ftp ) if opts . key? :ftp
47
- self . http = opts . delete ( :http ) if opts . key? :http
48
- self . no_proxy = opts . delete ( :no_proxy ) if opts . key? :no_proxy
49
- self . ssl = opts . delete ( :ssl ) if opts . key? :ssl
50
- self . pac = opts . delete ( :pac ) if opts . key? :pac
51
- self . auto_detect = opts . delete ( :auto_detect ) if opts . key? :auto_detect
52
- self . socks = opts . delete ( :socks ) if opts . key? :socks
53
- self . socks_username = opts . delete ( :socks_username ) if opts . key? :socks_username
54
- self . socks_password = opts . delete ( :socks_password ) if opts . key? :socks_password
55
-
56
- return if opts . empty?
57
- raise ArgumentError , "unknown option#{ 's' if opts . size != 1 } : #{ opts . inspect } "
58
+ not_allowed = [ ]
59
+
60
+ opts . each do |k , v |
61
+ if ALLOWED . key? ( k )
62
+ send ( "#{ k } =" , v )
63
+ else
64
+ not_allowed << k
65
+ end
66
+ end
67
+
68
+ return if not_allowed . empty?
69
+ raise ArgumentError , "unknown option#{ 's' if not_allowed . size != 1 } : #{ not_allowed . inspect } "
58
70
end
59
71
60
72
def ==( other )
@@ -121,46 +133,24 @@ def type=(type)
121
133
122
134
def as_json ( *)
123
135
json_result = {
124
- 'proxyType' => TYPES [ type ]
125
- }
126
-
127
- json_result [ 'ftpProxy' ] = ftp if ftp
128
- json_result [ 'httpProxy' ] = http if http
129
- json_result [ 'noProxy' ] = no_proxy if no_proxy
130
- json_result [ 'proxyAutoconfigUrl' ] = pac if pac
131
- json_result [ 'sslProxy' ] = ssl if ssl
132
- json_result [ 'autodetect' ] = auto_detect if auto_detect
133
- json_result [ 'socksProxy' ] = socks if socks
134
- json_result [ 'socksUsername' ] = socks_username if socks_username
135
- json_result [ 'socksPassword' ] = socks_password if socks_password
136
+ 'proxyType' => TYPES [ type ] ,
137
+ 'ftpProxy' => ftp ,
138
+ 'httpProxy' => http ,
139
+ 'noProxy' => no_proxy ,
140
+ 'proxyAutoconfigUrl' => pac ,
141
+ 'sslProxy' => ssl ,
142
+ 'autodetect' => auto_detect ,
143
+ 'socksProxy' => socks ,
144
+ 'socksUsername' => socks_username ,
145
+ 'socksPassword' => socks_password
146
+ } . delete_if { |_k , v | v . nil? }
136
147
137
148
json_result if json_result . length > 1
138
149
end
139
150
140
151
def to_json ( *)
141
152
JSON . generate as_json
142
153
end
143
-
144
- class << self
145
- def json_create ( data )
146
- return if data [ 'proxyType' ] == 'UNSPECIFIED'
147
-
148
- proxy = new
149
-
150
- proxy . type = data [ 'proxyType' ] . downcase . to_sym if data . key? 'proxyType'
151
- proxy . ftp = data [ 'ftpProxy' ] if data . key? 'ftpProxy'
152
- proxy . http = data [ 'httpProxy' ] if data . key? 'httpProxy'
153
- proxy . no_proxy = data [ 'noProxy' ] if data . key? 'noProxy'
154
- proxy . pac = data [ 'proxyAutoconfigUrl' ] if data . key? 'proxyAutoconfigUrl'
155
- proxy . ssl = data [ 'sslProxy' ] if data . key? 'sslProxy'
156
- proxy . auto_detect = data [ 'autodetect' ] if data . key? 'autodetect'
157
- proxy . socks = data [ 'socksProxy' ] if data . key? 'socksProxy'
158
- proxy . socks_username = data [ 'socksUsername' ] if data . key? 'socksUsername'
159
- proxy . socks_password = data [ 'socksPassword' ] if data . key? 'socksPassword'
160
-
161
- proxy
162
- end
163
- end # class << self
164
154
end # Proxy
165
155
end # WebDriver
166
156
end # Selenium
0 commit comments