wikk_webbrowser 0.9.0 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/History.txt +30 -0
  3. data/Rakefile +1 -0
  4. data/lib/wikk_webbrowser.rb +175 -141
  5. data/version +1 -1
  6. metadata +28 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 840024711637a8a2433dec8d0fe52c0ed020dbdc0e209d6a002be8246990d32e
4
- data.tar.gz: cb560ab933a38261a04f8a9224d650057ddcd29762d23a9f5ec3b3a3544cafda
3
+ metadata.gz: b68922378ce9c4777c7810d94449ec8c4cdd738d9480b80430aa9f8829cd0d97
4
+ data.tar.gz: 33654b9c6d202036b39895c2b41facba850bfcee44e211866cca373a45443c8f
5
5
  SHA512:
6
- metadata.gz: dbc4adeb4a1004fded3741a4fbdf2ba30256a62b140358ab1ab988836b59ba06008f2403113b87b7bae79a1228c95855ca138f0166a053b75633555e36d14f24
7
- data.tar.gz: cff97297e84398529b2922918f44f86f4d5601f24fcd2f31c8f328a38d871e498e7b0c7c2fb834b417b4727801ebcb0d76daebc0355b8829f35e7ff9a9477f57
6
+ metadata.gz: a3ed4f35c54b8d96baddfe7e21afc0fdf674706daf36aa5a97a0da5717a3fb513a1bbda23c7840765b9891d314a3ff26601847272b8c146b34bf196c7c9f0534
7
+ data.tar.gz: 9bafdc4f3fd1b77d92b749b5970e224811a631a3f1fc8c9f48cbf59b07a884f8a12d9431620ef331bd8afc0e99d4cbb6a109f09c315f1e5722eb4a2ae569b680
data/History.txt CHANGED
@@ -1,3 +1,33 @@
1
+ robertburrowes Mon Sep 20 12:14:31 2021 +1200
2
+ Add aliases for put,post,get and delete
3
+ robertburrowes Mon Sep 20 12:14:09 2021 +1200
4
+ Added optional form arguments to delete_req Added optional data/content arguments to delete_req
5
+ robertburrowes Sat Sep 18 17:49:31 2021 +1200
6
+ Rubocop Beautifier
7
+ robertburrowes Sat Sep 18 17:23:58 2021 +1200
8
+ Rubocop beautifier reformat
9
+ robertburrowes Wed Apr 21 19:03:05 2021 +1200
10
+ Dependency version was wrong
11
+ robertburrowes Wed Apr 21 18:54:00 2021 +1200
12
+ Bug Fix: content_type was fixed, ignoring param
13
+ robertburrowes Sun Nov 8 18:59:06 2020 +1300
14
+ doc upload issue
15
+ robertburrowes Fri Oct 30 14:52:36 2020 +1300
16
+ #{PROJECT} release 0.9.3
17
+ robertburrowes Fri Oct 30 14:46:17 2020 +1300
18
+ rename header() to header_value
19
+ robertburrowes Fri Oct 30 13:16:07 2020 +1300
20
+ saving the last response in @response. Added header()
21
+ robertburrowes Sun Oct 25 21:26:56 2020 +1300
22
+ add in standard dev scripts to sbin
23
+ robertburrowes Sun Oct 25 21:26:34 2020 +1300
24
+ tidy up readme urls
25
+ robertburrowes Sun Oct 25 21:26:08 2020 +1300
26
+ add dependencies
27
+ robertburrowes Sun Oct 25 21:25:52 2020 +1300
28
+ Make into module.
29
+ robertburrowes Sun Oct 25 21:25:15 2020 +1300
30
+ Ignore autogenerated stuff
1
31
  robertburrowes Fri Oct 23 15:02:35 2020 +1300
2
32
  getting ready for publishing
3
33
  robertburrowes Tue Oct 20 08:38:12 2020 +1300
data/Rakefile CHANGED
@@ -14,6 +14,7 @@ Hoe.spec "#{PROJECT}" do
14
14
  self.yard_options = ['--markup', 'markdown', '--protected']
15
15
 
16
16
  self.dependency "nokogiri", ['~> 1.0', '>= 1.0.0']
17
+ self.dependency "wikk_json", ['~> 0.1', '>= 0.1.2']
17
18
  self.dependency 'hoe-yard', ['~> 0.1', '>= 0.1.3'], type=:dev
18
19
  end
19
20
 
@@ -1,28 +1,28 @@
1
- module WIKK
1
+ module WIKK # :nodoc:
2
2
  require 'net/http'
3
3
  require 'net/https'
4
4
  require 'uri'
5
5
  require 'cgi'
6
6
  require 'nokogiri'
7
7
  require 'base64'
8
+ require 'wikk_json'
8
9
 
9
10
  # WIKK WebBrowser class under MIT Lic. https://github.com/wikarekare.
10
11
  # Wrapper around ruby's http classes
11
12
  # WIKK_WebBrowser.new.https_session(host: 'www.blah.com') do |session|
12
13
  # response = get_page(query: ,'/')
13
14
  # end
14
-
15
15
  class WebBrowser
16
- VERSION = '0.9.0'
17
-
18
- class Error < RuntimeError
16
+ VERSION = '0.9.6'
17
+
18
+ class Error < RuntimeError # :nodoc:
19
19
  attr_accessor :web_return_code
20
+
20
21
  def initialize(web_return_code:, message:)
21
22
  super(message)
22
23
  @web_return_code = web_return_code
23
24
  end
24
25
  end
25
-
26
26
 
27
27
  attr_reader :host
28
28
  attr_accessor :session
@@ -33,7 +33,7 @@ module WIKK
33
33
  attr_accessor :verify_cert
34
34
  attr_accessor :port
35
35
  attr_accessor :use_ssl
36
-
36
+ attr_accessor :response
37
37
 
38
38
  # Create a WIKK_WebBrowser instance
39
39
  #
@@ -44,12 +44,13 @@ module WIKK
44
44
  # @return [WIKK_WebBrowser]
45
45
  #
46
46
  def initialize(host:, port: nil, use_ssl: false, cookies: {}, verify_cert: true, debug: false)
47
- @host = host #Need to do this, as passing nil is different to passing nothing to initialize!
48
- @cookies = cookies == nil ? {} : cookies
47
+ @host = host # Need to do this, as passing nil is different to passing nothing to initialize!
48
+ @cookies = cookies.nil? ? {} : cookies
49
49
  @debug = debug
50
50
  @use_ssl = use_ssl
51
- @port = port != nil ? port : ( use_ssl ? 443 : 80 )
51
+ @port = port.nil? ? ( use_ssl ? 443 : 80 ) : port
52
52
  @verify_cert = verify_cert
53
+ @response = nil
53
54
  end
54
55
 
55
56
  # Create a WIKK_WebBrowser instance, connect to the host via http, and yield the WIKK_WebBrowser instance.
@@ -75,7 +76,7 @@ module WIKK
75
76
  # @param host [String] the host we want to connect to
76
77
  # @param port [Fixnum] (443) the port the remote web server is running on
77
78
  # @param verify_cert [Boolean] Validate certificate if true (Nb lots of embedded devices have self signed certs, so verify will fail)
78
- # @param block [Proc]
79
+ # @param block [Proc]
79
80
  # @yieldparam [WIKK_WebBrowser] the session descriptor for further calls.
80
81
  def self.https_session(host:, port: nil, verify_cert: true, cookies: {}, debug: false)
81
82
  wb = self.new(host: host, port: port, cookies: cookies, use_ssl: true, verify_cert: verify_cert, debug: debug)
@@ -85,62 +86,70 @@ module WIKK
85
86
  end
86
87
 
87
88
  # Creating a session for http connection
88
- # attached block would then call get or post NET::HTTP calls
89
+ # attached block would then call get or post NET::HTTP calls
89
90
  # @param port [Fixnum] Optional http server port
90
91
  # @param use_ssl [Boolean] Use https, if true
91
92
  # @param verify_cert [Boolean] Validate certificate if true (Nb lots of embedded devices have self signed certs, so verify will fail)
92
- # @param block [Proc]
93
+ # @param block [Proc]
93
94
  def http_session
94
- @http = Net::HTTP.new(@host, @port)
95
+ @http = Net::HTTP.new(@host, @port)
95
96
  @http.set_debug_output($stdout) if @debug
96
- @http.use_ssl = @use_ssl
97
- @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ! @use_ssl || ! @verify_cert
98
- @http.start do |session| #ensure we close the session after the block
99
- @session = session
100
- yield
97
+ @http.use_ssl = @use_ssl
98
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ! @use_ssl || ! @verify_cert
99
+ @http.start do |session| # ensure we close the session after the block
100
+ @session = session
101
+ yield
101
102
  end
102
103
  end
103
-
104
+
104
105
  # Web basic authentication (not exactly secure)
105
106
  # @param user [String] Account name
106
107
  # @param password [String] Accounts password
107
108
  # @return [String] Base64 encoded concatentation of user + ':' + password
108
109
  def basic_authorization(user:, password:)
109
- #req.basic_auth( user, password) if user != nil
110
+ # req.basic_auth( user, password) if user != nil
110
111
  'Basic ' + Base64.encode64( "#{user}:#{password}" )
111
112
  end
112
-
113
+
113
114
  # Dropbox style token authentication
114
115
  # @param token [String] Token, as issued by dropbox
115
116
  # @return [String] Concatenation of 'Bearer ' + token
116
117
  def bearer_authorization(token:)
117
- "Bearer " + token
118
+ 'Bearer ' + token
118
119
  end
119
-
120
+
120
121
  # Add additional cookies
121
122
  # @param cookies [Hash] cookie_name => cookie_value
122
123
  def add_cookies(cookies)
123
124
  cookies.each { |cookie_name, cookie_value| @cookies[cookie_name] = cookie_value }
124
125
  end
125
-
126
- # Save cookies returned by last html get/post.
126
+
127
+ # Save cookies returned by last html get/post.
127
128
  # Removes previous cookies.
128
129
  # @param response [Net::HTTPResponse] result from HTTP calls
129
130
  def save_cookies(response)
130
- if(cookie_lines = response.get_fields('set-cookie')) != nil
131
- cookie_lines.each do | cookie_line |
132
- cookies = cookie_line.split('; ').map { |v| v.split('=')}
131
+ if (cookie_lines = response.get_fields('set-cookie')) != nil
132
+ cookie_lines.each do |cookie_line|
133
+ cookies = cookie_line.split('; ').map { |v| v.split('=') }
133
134
  cookies.each { |c| @cookies[c[0]] = c[1] }
134
135
  end
135
136
  end
136
137
  end
137
-
138
+
138
139
  # Convert @cookies to ; separated strings
139
140
  # @return cookies string
140
141
  def cookies_to_s
141
142
  @cookies.to_a.map { |v| v.join('=') }.join('; ')
142
143
  end
143
144
 
145
+ # Get a header value, from the last response
146
+ #
147
+ # @param key [String] header key
148
+ # @return [String] header value, for the given key.
149
+ def header_value(key:)
150
+ @response.header[key]
151
+ end
152
+
144
153
  # send a GET query to the web server using an http get, and returns the response.
145
154
  # Cookies in the response get preserved in @cookies, so they will be sent along with subsequent calls
146
155
  # We are currently ignoring redirects from the PDU's we are querying.
@@ -150,13 +159,13 @@ module WIKK
150
159
  # @param extra_headers [Hash] Add these to standard headers
151
160
  # @param extra_cookies [Hash] Add these to standard cookies
152
161
  # @return [String] The Net::HTTPResponse.body text response from the web server
153
- def get_page(query: ,form_values: nil, authorization: nil, extra_headers: {}, extra_cookies: {})
154
- $stderr.puts "Debugging On" if @debug
155
- query += form_values_to_s(form_values, query.index('?') != nil) #Should be using req.set_form_data, but it seems to by stripping the leading / and then the query fails.
156
- url = URI.parse("#{@use_ssl ? "https" : "http"}://#{@host}/#{query.gsub(/^\//,'')}")
162
+ def get_page(query:, form_values: nil, authorization: nil, extra_headers: {}, extra_cookies: {})
163
+ $stderr.puts 'Debugging On' if @debug
164
+ query += form_values_to_s(form_values, query.index('?') != nil) # Should be using req.set_form_data, but it seems to by stripping the leading / and then the query fails.
165
+ url = URI.parse("#{@use_ssl ? 'https' : 'http'}://#{@host}/#{query.gsub(/^\//, '')}")
157
166
  $stderr.puts url if @debug
158
-
159
- req = Net::HTTP::Get.new(url.request_uri)
167
+
168
+ req = Net::HTTP::Get.new(url.request_uri)
160
169
 
161
170
  header = { 'HOST' => @host }
162
171
  header['Accept'] = '*/*'
@@ -164,41 +173,44 @@ module WIKK
164
173
  header['Accept-Language'] = 'en-US,en;q=0.5'
165
174
  header['Connection'] = 'keep-alive'
166
175
  header['User-Agent'] = 'Mozilla/5.0'
167
- header['Content-Type'] = 'application/x-www-form-urlencoded'
176
+ header['Content-Type'] = 'application/x-www-form-urlencoded'
168
177
  add_cookies(extra_cookies)
169
178
  header['Cookie'] = cookies_to_s if @cookies.length > 0
170
- header['DNT'] = "1"
179
+ header['DNT'] = '1'
171
180
  header['Authorization'] = authorization if authorization != nil
172
181
 
173
- extra_headers.each do |k,v|
182
+ extra_headers.each do |k, v|
174
183
  header[k] = v
175
184
  end
176
-
185
+
177
186
  req.initialize_http_header( header )
178
187
 
179
- response = @session.request(req)
180
- save_cookies(response)
181
-
182
- $stderr.puts response.code.to_i if @debug
183
-
184
- if(response.code.to_i >= 300)
185
- if(response.code.to_i == 302)
186
- #ignore the redirects.
187
- #$stderr.puts "302"
188
- #response.each {|key, val| $stderr.printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
189
- #$stderr.puts "Redirect to #{response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
190
- #$stderr.puts
188
+ @response = @session.request(req)
189
+ save_cookies(@response)
190
+
191
+ $stderr.puts @response.code.to_i if @debug
192
+
193
+ if @response.code.to_i >= 300
194
+ if @response.code.to_i == 302
195
+ # ignore the redirects.
196
+ # $stderr.puts "302"
197
+ # @response.each {|key, val| $stderr.printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
198
+ # $stderr.puts "Redirect to #{@response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
199
+ # $stderr.puts
191
200
  return
192
- elsif response.code.to_i >= 400 && response.code.to_i < 500
193
- return response.body
201
+ elsif @response.code.to_i >= 400 && @response.code.to_i < 500
202
+ return @response.body
194
203
  end
195
- raise Error.new(web_return_code: response.code.to_i, message: "#{response.code} #{response.message} #{query} #{form_values} #{response.body}")
204
+
205
+ raise Error.new(web_return_code: @response.code.to_i, message: "#{@response.code} #{@response.message} #{query} #{form_values} #{@response.body}")
196
206
  end
197
207
 
198
- return response.body
208
+ return @response.body
199
209
  end
200
210
 
201
- # send a POST query to the server and return the response.
211
+ alias get get_page
212
+
213
+ # send a POST query to the server and return the response.
202
214
  # @param query [String] URL, less the 'http://host/' part
203
215
  # @param authorization [String] If present, add Authorization header, using this string
204
216
  # @param content_type [String] Posted content type
@@ -207,92 +219,114 @@ module WIKK
207
219
  # @param extra_cookies [Hash] Add these to standard cookies
208
220
  # @return [String] The Net::HTTPResponse.body text response from the web server
209
221
  def post_page(query:, authorization: nil, content_type: 'application/x-www-form-urlencoded', data: nil, extra_headers: {}, extra_cookies: {})
210
- url = URI.parse("#{@use_ssl ? "https" : "http"}://#{@host}/#{query}")
222
+ url = URI.parse("#{@use_ssl ? 'https' : 'http'}://#{@host}/#{query}")
211
223
  req = Net::HTTP::Post.new(url.path)
212
-
224
+
213
225
  header = { 'HOST' => @host }
214
226
  header['Accept'] = '*/*'
215
227
  header['Accept-Encoding'] = 'gzip, deflate, br'
216
228
  header['Accept-Language'] = 'en-US,en;q=0.5'
217
229
  header['Connection'] = 'keep-alive'
218
230
  header['User-Agent'] = 'Mozilla/5.0'
219
- #header['Content-Type'] = data.class == Hash ? 'application/x-www-form-urlencoded' : "text/json"
220
- header['Content-Type'] = 'application/x-www-form-urlencoded'
231
+ header['Content-Type'] = content_type
221
232
  add_cookies(extra_cookies)
222
233
  header['Cookie'] = cookies_to_s if @cookies.length > 0
223
- header['DNT'] = "1"
234
+ header['DNT'] = '1'
224
235
  header['Authorization'] = authorization if authorization != nil
225
-
226
- extra_headers.each do |k,v|
236
+
237
+ extra_headers.each do |k, v|
227
238
  header[k] = v
228
239
  end
229
240
  req.initialize_http_header( header )
230
-
231
- if data != nil
232
- if data.class == Hash
233
- req.set_form_data(data, '&')
241
+
242
+ if data.nil?
243
+ req.body = ''
244
+ elsif data.instance_of?(Hash)
245
+ if content_type =~ /application\/octet-stream/
246
+ req.set_form_data(data, '&')
234
247
  else
235
- req.body = data #If json as a string or raw string
248
+ req.set_form_data.to_j
236
249
  end
237
250
  else
238
- req.body = ''
251
+ req.body = data # If json as a string or raw string
239
252
  end
240
-
241
- response = @session.request(req)
242
- save_cookies(response)
243
-
244
- if(response.code.to_i >= 300)
245
- if(response.code.to_i == 302)
246
- #ignore the redirects.
247
- #puts "302"
248
- #response.each {|key, val| printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
249
- #puts "Redirect of Post to #{response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
253
+
254
+ @response = @session.request(req)
255
+ save_cookies(@response)
256
+
257
+ if @response.code.to_i >= 300
258
+ if @response.code.to_i == 302
259
+ # ignore the redirects.
260
+ # puts "302"
261
+ # @response.each {|key, val| printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
262
+ # puts "Redirect of Post to #{@response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
250
263
  return
251
264
  end
252
- raise Error.new(web_return_code: response.code, message: "#{response.code} #{response.message} #{query} #{data} #{response.body}")
265
+
266
+ raise Error.new(web_return_code: @response.code, message: "#{@response.code} #{@response.message} #{query} #{data} #{@response.body}")
253
267
  end
254
268
 
255
- @response = response
256
-
257
- return response.body
269
+ return @response.body
258
270
  end
259
271
 
260
- # send a DELETE query to the server and return the response.
272
+ alias post post_page
273
+
274
+ # send a DELETE query to the server and return the response.
261
275
  # @param query [String] URL, less the 'http://host/' part
262
276
  # @param authorization [String] If present, add Authorization header, using this string
263
- # @param content_type [String] Posted content type
277
+ # @param form_values [Hash{String=>Object-with-to_s}] The parameter passed to the web server eg. ?key1=value1&key2=value2...
278
+ # @param content_type [String] Posted content type. Only meaningful if there is data
279
+ # @param data [String] Text to add to body of DELETE to the web server. Not always supported at the server end.
264
280
  # @param extra_headers [Hash] Add these to standard headers
265
281
  # @param extra_cookies [Hash] Add these to standard cookies
266
282
  # @return [String] The Net::HTTPResponse.body text response from the web server
267
- def delete_req(query:, authorization: nil, extra_headers: {}, extra_cookies: {})
268
- url = URI.parse("#{@use_ssl ? "https" : "http"}://#{@host}/#{query.gsub(/^\//,'')}")
283
+ def delete_req(query:, authorization: nil, form_values: nil, content_type: '"application/octet-stream"', data: nil, extra_headers: {}, extra_cookies: {})
284
+ $stderr.puts 'Debugging On' if @debug
285
+
286
+ query += form_values_to_s(form_values, query.index('?') != nil)
287
+ url = URI.parse("#{@use_ssl ? 'https' : 'http'}://#{@host}/#{query.gsub(/^\//, '')}")
269
288
  req = Net::HTTP::Delete.new(query)
270
-
289
+
271
290
  header = { 'HOST' => @host }
272
291
  add_cookies(extra_cookies)
273
292
  header['Cookie'] = cookies_to_s if @cookies.length > 0
274
293
  header['Authorization'] = authorization if authorization != nil
275
-
276
- extra_headers.each do |k,v|
294
+
295
+ extra_headers.each do |k, v|
277
296
  header[k] = v
278
297
  end
279
298
  req.initialize_http_header( header )
280
299
 
300
+ if data.nil?
301
+ req.body = ''
302
+ elsif data.instance_of?(Hash)
303
+ if content_type =~ /application\/octet-stream/
304
+ req.set_form_data(data, '&')
305
+ else
306
+ req.set_form_data.to_j
307
+ end
308
+ else
309
+ req.body = data # If json as a string or raw string
310
+ end
311
+
281
312
  begin
282
- response = @session.request(req)
283
- save_cookies(response)
284
-
285
- if(response.code.to_i >= 300)
286
- raise "#{url} : #{response.code} #{response.message}"
313
+ @response = @session.request(req)
314
+ save_cookies(@response)
315
+
316
+ if @response.code.to_i >= 300
317
+ raise "#{url} : #{@response.code} #{@response.message}"
287
318
  end
288
- return response.body
319
+
320
+ return @response.body
289
321
  rescue StandardError => e
290
322
  puts "#{e}"
291
323
  return nil
292
324
  end
293
325
  end
294
-
295
- # send a PUT query to the server and return the response.
326
+
327
+ alias delete delete_req
328
+
329
+ # send a PUT query to the server and return the response.
296
330
  # @param query [String] URL, less the 'http://host/' part
297
331
  # @param authorization [String] If present, add Authorization header, using this string
298
332
  # @param content_type [String] Posted content type
@@ -301,96 +335,96 @@ module WIKK
301
335
  # @param extra_cookies [Hash] Add these to standard cookies
302
336
  # @return [String] The Net::HTTPResponse.body text response from the web server
303
337
  def put_req(query:, authorization: nil, content_type: '"application/octet-stream"', data: nil, extra_headers: {}, extra_cookies: {})
304
- url = URI.parse("#{@use_ssl ? "https" : "http"}://#{@host}/#{query}")
338
+ url = URI.parse("#{@use_ssl ? 'https' : 'http'}://#{@host}/#{query}")
305
339
  req = Net::HTTP::Put.new(url.path)
306
-
340
+
307
341
  header = { 'HOST' => @host }
308
342
  header['Accept'] = '*/*'
309
343
  header['Accept-Encoding'] = 'gzip, deflate, br'
310
344
  header['Accept-Language'] = 'en-US,en;q=0.5'
311
345
  header['Connection'] = 'keep-alive'
312
346
  header['User-Agent'] = 'Mozilla/5.0'
313
- #header['Content-Type'] = data.class == Hash ? 'application/x-www-form-urlencoded' : "text/json"
314
- header['Content-Type'] = 'application/x-www-form-urlencoded'
347
+ header['Content-Type'] = content_type
315
348
  add_cookies(extra_cookies)
316
349
  header['Cookie'] = cookies_to_s if @cookies.length > 0
317
- header['DNT'] = "1"
350
+ header['DNT'] = '1'
318
351
  header['Authorization'] = authorization if authorization != nil
319
-
320
- extra_headers.each do |k,v|
352
+
353
+ extra_headers.each do |k, v|
321
354
  header[k] = v
322
355
  end
323
356
  req.initialize_http_header( header )
324
-
325
- if data != nil
326
- if data.class == Hash
327
- req.set_form_data(data, '&')
357
+
358
+ if data.nil?
359
+ req.body = ''
360
+ elsif data.instance_of?(Hash)
361
+ if content_type =~ /application\/octet-stream/
362
+ req.set_form_data(data, '&')
328
363
  else
329
- req.body = data #If json as a string or raw string
364
+ req.set_form_data.to_j
330
365
  end
331
366
  else
332
- req.body = ''
367
+ req.body = data # If json as a string or raw string
333
368
  end
334
-
335
- response = @session.request(req)
336
- save_cookies(response)
337
-
338
- if(response.code.to_i >= 300)
339
- if(response.code.to_i == 302)
340
- #ignore the redirects.
341
- #puts "302"
342
- #response.each {|key, val| printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
343
- #puts "Redirect of Post to #{response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
369
+
370
+ @response = @session.request(req)
371
+ save_cookies(@response)
372
+
373
+ if @response.code.to_i >= 300
374
+ if @response.code.to_i == 302
375
+ # ignore the redirects.
376
+ # puts "302"
377
+ # @response.each {|key, val| printf "%s = %s\n", key, val } #Location seems to have cgi params removed. End up with .../cginame?&
378
+ # puts "Redirect of Post to #{@response['location']}" #Location seems to have cgi params removed. End up with .../cginame?&
344
379
  return
345
380
  end
346
- raise Error.new(web_return_code: response.code, message: "#{response.code} #{response.message} #{query} #{data} #{response.body}")
381
+
382
+ raise Error.new(web_return_code: @response.code, message: "#{@response.code} #{@response.message} #{query} #{data} #{@response.body}")
347
383
  end
348
384
 
349
- @response = response
350
-
351
- return response.body
385
+ return @response.body
352
386
  end
353
387
 
354
- #Extract form field values from the html body.
388
+ alias put put_req
389
+
390
+ # Extract form field values from the html body.
355
391
  # @param body [String] The html response body
356
392
  # @return [Hash] Keys are the field names, values are the field values
357
393
  def extract_input_fields(body)
358
- entry = true
359
394
  @inputs = {}
360
395
  doc = Nokogiri::HTML(body)
361
- doc.xpath("//form/input").each do |f|
396
+ doc.xpath('//form/input').each do |f|
362
397
  @inputs[f.get_attribute('name')] = f.get_attribute('value')
363
398
  end
364
399
  end
365
400
 
366
- #Extract links from the html body.
401
+ # Extract links from the html body.
367
402
  # @param body [String] The html response body
368
403
  # @return [Hash] Keys are the link text, values are the html links
369
404
  def extract_link_fields(body)
370
- entry = true
371
405
  @inputs = {}
372
406
  doc = Nokogiri::HTML(body)
373
- doc.xpath("//a").each do |f|
374
- return URI.parse( f.get_attribute('href') ).path if(f.get_attribute('name') == 'URL$1')
407
+ doc.xpath('//a').each do |f|
408
+ return URI.parse( f.get_attribute('href') ).path if f.get_attribute('name') == 'URL$1'
375
409
  end
376
410
  return nil
377
411
  end
378
412
 
379
- #Take a hash of the params to the post and generate a safe URL string.
413
+ # Take a hash of the params to the post and generate a safe URL string.
380
414
  # @param form_values [Hash] Keys are the field names, values are the field values
381
415
  # @param has_q [Boolean] We have a leading ? for the html get, so don't need to add one.
382
416
  # @return [String] The 'safe' text for fields the get or post query to the web server
383
- def form_values_to_s(form_values=nil, has_q = false)
384
- return "" if form_values == nil || form_values.length == 0
385
- s = (has_q == true ? "" : "?")
417
+ def form_values_to_s(form_values = nil, has_q = false)
418
+ return '' if form_values.nil? || form_values.length == 0
419
+
420
+ s = (has_q == true ? '' : '?')
386
421
  first = true
387
- form_values.each do |key,value|
388
- s += "&" if !first
389
- s += "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"
422
+ form_values.each do |key, value|
423
+ s += '&' unless first
424
+ s += "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
390
425
  first = false
391
426
  end
392
427
  return s
393
428
  end
394
429
  end
395
430
  end
396
-
data/version CHANGED
@@ -1,2 +1,2 @@
1
1
  PROJECT="wikk_webbrowser"
2
- VERSION="0.9.0"
2
+ VERSION="0.9.6"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikk_webbrowser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Burrowes
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-25 00:00:00.000000000 Z
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -30,6 +30,26 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 1.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: wikk_json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.1.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.1.2
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: hoe-yard
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -50,14 +70,14 @@ dependencies:
50
70
  requirements:
51
71
  - - "~>"
52
72
  - !ruby/object:Gem::Version
53
- version: '3.22'
73
+ version: '3.23'
54
74
  type: :development
55
75
  prerelease: false
56
76
  version_requirements: !ruby/object:Gem::Requirement
57
77
  requirements:
58
78
  - - "~>"
59
79
  - !ruby/object:Gem::Version
60
- version: '3.22'
80
+ version: '3.23'
61
81
  description: |-
62
82
  Wrapper around ruby http and https libraries.
63
83
 
@@ -81,7 +101,7 @@ homepage: https://wikarekare.github.io/wikk_webbrowser/
81
101
  licenses:
82
102
  - MIT
83
103
  metadata: {}
84
- post_install_message:
104
+ post_install_message:
85
105
  rdoc_options:
86
106
  - "--markup"
87
107
  - markdown
@@ -102,8 +122,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
122
  - !ruby/object:Gem::Version
103
123
  version: '0'
104
124
  requirements: []
105
- rubygems_version: 3.1.2
106
- signing_key:
125
+ rubygems_version: 3.1.4
126
+ signing_key:
107
127
  specification_version: 4
108
128
  summary: Wrapper around ruby http and https libraries
109
129
  test_files: []