wikk_webbrowser 0.9.0
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.
- checksums.yaml +7 -0
- data/History.txt +4 -0
- data/Manifest.txt +6 -0
- data/README.md +66 -0
- data/Rakefile +31 -0
- data/lib/wikk_webbrowser.rb +396 -0
- data/version +2 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 840024711637a8a2433dec8d0fe52c0ed020dbdc0e209d6a002be8246990d32e
|
4
|
+
data.tar.gz: cb560ab933a38261a04f8a9224d650057ddcd29762d23a9f5ec3b3a3544cafda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbc4adeb4a1004fded3741a4fbdf2ba30256a62b140358ab1ab988836b59ba06008f2403113b87b7bae79a1228c95855ca138f0166a053b75633555e36d14f24
|
7
|
+
data.tar.gz: cff97297e84398529b2922918f44f86f4d5601f24fcd2f31c8f328a38d871e498e7b0c7c2fb834b417b4727801ebcb0d76daebc0355b8829f35e7ff9a9477f57
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# webbrowser
|
2
|
+
|
3
|
+
* Docs :: https://wikarekare.github.io/wikk_webbrowser/
|
4
|
+
* Source :: https://github.com/wikarekare/wikk_webbrowser
|
5
|
+
* Gem :: https://rubygems.org/gems/wikk_webbrowser
|
6
|
+
|
7
|
+
## DESCRIPTION:
|
8
|
+
|
9
|
+
Wrapper around ruby http and https libraries.
|
10
|
+
|
11
|
+
Converted to a gem from a mixture of versions I've used over the years. Might need some work yet :)
|
12
|
+
|
13
|
+
## FEATURES/PROBLEMS:
|
14
|
+
|
15
|
+
* session block
|
16
|
+
* get, post, put, delete
|
17
|
+
|
18
|
+
## SYNOPSIS:
|
19
|
+
|
20
|
+
call with WebBrowser.http_session() or https_session().
|
21
|
+
e.g.
|
22
|
+
```
|
23
|
+
WebBrowser.https_session( host: @hostname, verify_cert: false ) do |ws|
|
24
|
+
result = ws.get_page( query: "#{api_query}",
|
25
|
+
authorization: "token #{@auth_token}",
|
26
|
+
form_values: {
|
27
|
+
"page_size"=>page_size,
|
28
|
+
"page"=>page,
|
29
|
+
}.merge(args)
|
30
|
+
)
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
If the server supports keeping the connection open, then the block can have multiple calls per session.
|
35
|
+
|
36
|
+
## REQUIREMENTS:
|
37
|
+
|
38
|
+
## INSTALL:
|
39
|
+
|
40
|
+
* sudo gem install wikk_webbrowser
|
41
|
+
|
42
|
+
## LICENSE:
|
43
|
+
|
44
|
+
(The MIT License)
|
45
|
+
|
46
|
+
Copyright (c) 2020 FIX
|
47
|
+
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
49
|
+
a copy of this software and associated documentation files (the
|
50
|
+
'Software'), to deal in the Software without restriction, including
|
51
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
52
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
53
|
+
permit persons to whom the Software is furnished to do so, subject to
|
54
|
+
the following conditions:
|
55
|
+
|
56
|
+
The above copyright notice and this permission notice shall be
|
57
|
+
included in all copies or substantial portions of the Software.
|
58
|
+
|
59
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
60
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
61
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
62
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
63
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
64
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
65
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
66
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
# -*- ruby -*-
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe' # gem install hoe
|
5
|
+
Hoe.plugin :yard
|
6
|
+
load "#{__dir__}/version"
|
7
|
+
|
8
|
+
Hoe.spec "#{PROJECT}" do
|
9
|
+
self.readme_file = "README.md"
|
10
|
+
self.developer( "Rob Burrowes","r.burrowes@auckland.ac.nz")
|
11
|
+
remote_rdoc_dir = '' # Release to root
|
12
|
+
|
13
|
+
self.yard_title = "#{PROJECT}"
|
14
|
+
self.yard_options = ['--markup', 'markdown', '--protected']
|
15
|
+
|
16
|
+
self.dependency "nokogiri", ['~> 1.0', '>= 1.0.0']
|
17
|
+
self.dependency 'hoe-yard', ['~> 0.1', '>= 0.1.3'], type=:dev
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
#Validate manfest.txt
|
22
|
+
#rake check_manifest
|
23
|
+
|
24
|
+
#Local checking. Creates pkg/
|
25
|
+
#rake gem
|
26
|
+
|
27
|
+
#create doc/
|
28
|
+
#rake docs
|
29
|
+
|
30
|
+
#Copy up to rubygem.org
|
31
|
+
#rake release VERSION=0.0.1
|
@@ -0,0 +1,396 @@
|
|
1
|
+
module WIKK
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'uri'
|
5
|
+
require 'cgi'
|
6
|
+
require 'nokogiri'
|
7
|
+
require 'base64'
|
8
|
+
|
9
|
+
# WIKK WebBrowser class under MIT Lic. https://github.com/wikarekare.
|
10
|
+
# Wrapper around ruby's http classes
|
11
|
+
# WIKK_WebBrowser.new.https_session(host: 'www.blah.com') do |session|
|
12
|
+
# response = get_page(query: ,'/')
|
13
|
+
# end
|
14
|
+
|
15
|
+
class WebBrowser
|
16
|
+
VERSION = '0.9.0'
|
17
|
+
|
18
|
+
class Error < RuntimeError
|
19
|
+
attr_accessor :web_return_code
|
20
|
+
def initialize(web_return_code:, message:)
|
21
|
+
super(message)
|
22
|
+
@web_return_code = web_return_code
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
attr_reader :host
|
28
|
+
attr_accessor :session
|
29
|
+
attr_accessor :cookies
|
30
|
+
attr_reader :page
|
31
|
+
attr_accessor :referer
|
32
|
+
attr_accessor :debug
|
33
|
+
attr_accessor :verify_cert
|
34
|
+
attr_accessor :port
|
35
|
+
attr_accessor :use_ssl
|
36
|
+
|
37
|
+
|
38
|
+
# Create a WIKK_WebBrowser instance
|
39
|
+
#
|
40
|
+
# @param host [String] the host we want to connect to
|
41
|
+
# @param port [Fixnum] Optional http server port
|
42
|
+
# @param use_ssl [Boolean] Use https, if true
|
43
|
+
# @param verify_cert [Boolean] Validate certificate if true (Nb lots of embedded devices have self signed certs, so verify will fail)
|
44
|
+
# @return [WIKK_WebBrowser]
|
45
|
+
#
|
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
|
49
|
+
@debug = debug
|
50
|
+
@use_ssl = use_ssl
|
51
|
+
@port = port != nil ? port : ( use_ssl ? 443 : 80 )
|
52
|
+
@verify_cert = verify_cert
|
53
|
+
end
|
54
|
+
|
55
|
+
# Create a WIKK_WebBrowser instance, connect to the host via http, and yield the WIKK_WebBrowser instance.
|
56
|
+
# Automatically closes the http session on returning from the block passed to it.
|
57
|
+
#
|
58
|
+
# @param host [String] the host we want to connect to
|
59
|
+
#
|
60
|
+
# @param port [Fixnum] (80) the port the remote web server is running on
|
61
|
+
#
|
62
|
+
# @param block [Proc]
|
63
|
+
#
|
64
|
+
# @yieldparam [WIKK_WebBrowser] the session descriptor for further calls.
|
65
|
+
#
|
66
|
+
def self.http_session(host:, port: nil, debug: false, cookies: {})
|
67
|
+
wb = self.new(host: host, port: port, debug: debug, use_ssl: false, cookies: cookies)
|
68
|
+
wb.http_session do
|
69
|
+
yield wb
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Create a WIKK_WebBrowser instance, connect to the host via https, and yield the WIKK_WebBrowser instance.
|
74
|
+
# Automatically closes the http session on returning from the block passed to it.
|
75
|
+
# @param host [String] the host we want to connect to
|
76
|
+
# @param port [Fixnum] (443) the port the remote web server is running on
|
77
|
+
# @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
|
+
# @yieldparam [WIKK_WebBrowser] the session descriptor for further calls.
|
80
|
+
def self.https_session(host:, port: nil, verify_cert: true, cookies: {}, debug: false)
|
81
|
+
wb = self.new(host: host, port: port, cookies: cookies, use_ssl: true, verify_cert: verify_cert, debug: debug)
|
82
|
+
wb.http_session do
|
83
|
+
yield wb
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Creating a session for http connection
|
88
|
+
# attached block would then call get or post NET::HTTP calls
|
89
|
+
# @param port [Fixnum] Optional http server port
|
90
|
+
# @param use_ssl [Boolean] Use https, if true
|
91
|
+
# @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
|
+
def http_session
|
94
|
+
@http = Net::HTTP.new(@host, @port)
|
95
|
+
@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
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Web basic authentication (not exactly secure)
|
105
|
+
# @param user [String] Account name
|
106
|
+
# @param password [String] Accounts password
|
107
|
+
# @return [String] Base64 encoded concatentation of user + ':' + password
|
108
|
+
def basic_authorization(user:, password:)
|
109
|
+
#req.basic_auth( user, password) if user != nil
|
110
|
+
'Basic ' + Base64.encode64( "#{user}:#{password}" )
|
111
|
+
end
|
112
|
+
|
113
|
+
# Dropbox style token authentication
|
114
|
+
# @param token [String] Token, as issued by dropbox
|
115
|
+
# @return [String] Concatenation of 'Bearer ' + token
|
116
|
+
def bearer_authorization(token:)
|
117
|
+
"Bearer " + token
|
118
|
+
end
|
119
|
+
|
120
|
+
# Add additional cookies
|
121
|
+
# @param cookies [Hash] cookie_name => cookie_value
|
122
|
+
def add_cookies(cookies)
|
123
|
+
cookies.each { |cookie_name, cookie_value| @cookies[cookie_name] = cookie_value }
|
124
|
+
end
|
125
|
+
|
126
|
+
# Save cookies returned by last html get/post.
|
127
|
+
# Removes previous cookies.
|
128
|
+
# @param response [Net::HTTPResponse] result from HTTP calls
|
129
|
+
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('=')}
|
133
|
+
cookies.each { |c| @cookies[c[0]] = c[1] }
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Convert @cookies to ; separated strings
|
139
|
+
# @return cookies string
|
140
|
+
def cookies_to_s
|
141
|
+
@cookies.to_a.map { |v| v.join('=') }.join('; ')
|
142
|
+
end
|
143
|
+
|
144
|
+
# send a GET query to the web server using an http get, and returns the response.
|
145
|
+
# Cookies in the response get preserved in @cookies, so they will be sent along with subsequent calls
|
146
|
+
# We are currently ignoring redirects from the PDU's we are querying.
|
147
|
+
# @param query [String] The URL after the http://host/ bit and not usually not including parameters, if form_values are passed in
|
148
|
+
# @param form_values [Hash{String=>Object-with-to_s}] The parameter passed to the web server eg. ?key1=value1&key2=value2...
|
149
|
+
# @param authorization [String] If present, add Authorization header, using this string
|
150
|
+
# @param extra_headers [Hash] Add these to standard headers
|
151
|
+
# @param extra_cookies [Hash] Add these to standard cookies
|
152
|
+
# @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(/^\//,'')}")
|
157
|
+
$stderr.puts url if @debug
|
158
|
+
|
159
|
+
req = Net::HTTP::Get.new(url.request_uri)
|
160
|
+
|
161
|
+
header = { 'HOST' => @host }
|
162
|
+
header['Accept'] = '*/*'
|
163
|
+
header['Accept-Encoding'] = 'gzip, deflate, br'
|
164
|
+
header['Accept-Language'] = 'en-US,en;q=0.5'
|
165
|
+
header['Connection'] = 'keep-alive'
|
166
|
+
header['User-Agent'] = 'Mozilla/5.0'
|
167
|
+
header['Content-Type'] = 'application/x-www-form-urlencoded'
|
168
|
+
add_cookies(extra_cookies)
|
169
|
+
header['Cookie'] = cookies_to_s if @cookies.length > 0
|
170
|
+
header['DNT'] = "1"
|
171
|
+
header['Authorization'] = authorization if authorization != nil
|
172
|
+
|
173
|
+
extra_headers.each do |k,v|
|
174
|
+
header[k] = v
|
175
|
+
end
|
176
|
+
|
177
|
+
req.initialize_http_header( header )
|
178
|
+
|
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
|
191
|
+
return
|
192
|
+
elsif response.code.to_i >= 400 && response.code.to_i < 500
|
193
|
+
return response.body
|
194
|
+
end
|
195
|
+
raise Error.new(web_return_code: response.code.to_i, message: "#{response.code} #{response.message} #{query} #{form_values} #{response.body}")
|
196
|
+
end
|
197
|
+
|
198
|
+
return response.body
|
199
|
+
end
|
200
|
+
|
201
|
+
# send a POST query to the server and return the response.
|
202
|
+
# @param query [String] URL, less the 'http://host/' part
|
203
|
+
# @param authorization [String] If present, add Authorization header, using this string
|
204
|
+
# @param content_type [String] Posted content type
|
205
|
+
# @param data [String] Text to add to body of post to the web server
|
206
|
+
# @param extra_headers [Hash] Add these to standard headers
|
207
|
+
# @param extra_cookies [Hash] Add these to standard cookies
|
208
|
+
# @return [String] The Net::HTTPResponse.body text response from the web server
|
209
|
+
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}")
|
211
|
+
req = Net::HTTP::Post.new(url.path)
|
212
|
+
|
213
|
+
header = { 'HOST' => @host }
|
214
|
+
header['Accept'] = '*/*'
|
215
|
+
header['Accept-Encoding'] = 'gzip, deflate, br'
|
216
|
+
header['Accept-Language'] = 'en-US,en;q=0.5'
|
217
|
+
header['Connection'] = 'keep-alive'
|
218
|
+
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'
|
221
|
+
add_cookies(extra_cookies)
|
222
|
+
header['Cookie'] = cookies_to_s if @cookies.length > 0
|
223
|
+
header['DNT'] = "1"
|
224
|
+
header['Authorization'] = authorization if authorization != nil
|
225
|
+
|
226
|
+
extra_headers.each do |k,v|
|
227
|
+
header[k] = v
|
228
|
+
end
|
229
|
+
req.initialize_http_header( header )
|
230
|
+
|
231
|
+
if data != nil
|
232
|
+
if data.class == Hash
|
233
|
+
req.set_form_data(data, '&')
|
234
|
+
else
|
235
|
+
req.body = data #If json as a string or raw string
|
236
|
+
end
|
237
|
+
else
|
238
|
+
req.body = ''
|
239
|
+
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?&
|
250
|
+
return
|
251
|
+
end
|
252
|
+
raise Error.new(web_return_code: response.code, message: "#{response.code} #{response.message} #{query} #{data} #{response.body}")
|
253
|
+
end
|
254
|
+
|
255
|
+
@response = response
|
256
|
+
|
257
|
+
return response.body
|
258
|
+
end
|
259
|
+
|
260
|
+
# send a DELETE query to the server and return the response.
|
261
|
+
# @param query [String] URL, less the 'http://host/' part
|
262
|
+
# @param authorization [String] If present, add Authorization header, using this string
|
263
|
+
# @param content_type [String] Posted content type
|
264
|
+
# @param extra_headers [Hash] Add these to standard headers
|
265
|
+
# @param extra_cookies [Hash] Add these to standard cookies
|
266
|
+
# @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(/^\//,'')}")
|
269
|
+
req = Net::HTTP::Delete.new(query)
|
270
|
+
|
271
|
+
header = { 'HOST' => @host }
|
272
|
+
add_cookies(extra_cookies)
|
273
|
+
header['Cookie'] = cookies_to_s if @cookies.length > 0
|
274
|
+
header['Authorization'] = authorization if authorization != nil
|
275
|
+
|
276
|
+
extra_headers.each do |k,v|
|
277
|
+
header[k] = v
|
278
|
+
end
|
279
|
+
req.initialize_http_header( header )
|
280
|
+
|
281
|
+
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}"
|
287
|
+
end
|
288
|
+
return response.body
|
289
|
+
rescue StandardError => e
|
290
|
+
puts "#{e}"
|
291
|
+
return nil
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
# send a PUT query to the server and return the response.
|
296
|
+
# @param query [String] URL, less the 'http://host/' part
|
297
|
+
# @param authorization [String] If present, add Authorization header, using this string
|
298
|
+
# @param content_type [String] Posted content type
|
299
|
+
# @param data [String] Text to add to body of post to the web server
|
300
|
+
# @param extra_headers [Hash] Add these to standard headers
|
301
|
+
# @param extra_cookies [Hash] Add these to standard cookies
|
302
|
+
# @return [String] The Net::HTTPResponse.body text response from the web server
|
303
|
+
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}")
|
305
|
+
req = Net::HTTP::Put.new(url.path)
|
306
|
+
|
307
|
+
header = { 'HOST' => @host }
|
308
|
+
header['Accept'] = '*/*'
|
309
|
+
header['Accept-Encoding'] = 'gzip, deflate, br'
|
310
|
+
header['Accept-Language'] = 'en-US,en;q=0.5'
|
311
|
+
header['Connection'] = 'keep-alive'
|
312
|
+
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'
|
315
|
+
add_cookies(extra_cookies)
|
316
|
+
header['Cookie'] = cookies_to_s if @cookies.length > 0
|
317
|
+
header['DNT'] = "1"
|
318
|
+
header['Authorization'] = authorization if authorization != nil
|
319
|
+
|
320
|
+
extra_headers.each do |k,v|
|
321
|
+
header[k] = v
|
322
|
+
end
|
323
|
+
req.initialize_http_header( header )
|
324
|
+
|
325
|
+
if data != nil
|
326
|
+
if data.class == Hash
|
327
|
+
req.set_form_data(data, '&')
|
328
|
+
else
|
329
|
+
req.body = data #If json as a string or raw string
|
330
|
+
end
|
331
|
+
else
|
332
|
+
req.body = ''
|
333
|
+
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?&
|
344
|
+
return
|
345
|
+
end
|
346
|
+
raise Error.new(web_return_code: response.code, message: "#{response.code} #{response.message} #{query} #{data} #{response.body}")
|
347
|
+
end
|
348
|
+
|
349
|
+
@response = response
|
350
|
+
|
351
|
+
return response.body
|
352
|
+
end
|
353
|
+
|
354
|
+
#Extract form field values from the html body.
|
355
|
+
# @param body [String] The html response body
|
356
|
+
# @return [Hash] Keys are the field names, values are the field values
|
357
|
+
def extract_input_fields(body)
|
358
|
+
entry = true
|
359
|
+
@inputs = {}
|
360
|
+
doc = Nokogiri::HTML(body)
|
361
|
+
doc.xpath("//form/input").each do |f|
|
362
|
+
@inputs[f.get_attribute('name')] = f.get_attribute('value')
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
#Extract links from the html body.
|
367
|
+
# @param body [String] The html response body
|
368
|
+
# @return [Hash] Keys are the link text, values are the html links
|
369
|
+
def extract_link_fields(body)
|
370
|
+
entry = true
|
371
|
+
@inputs = {}
|
372
|
+
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')
|
375
|
+
end
|
376
|
+
return nil
|
377
|
+
end
|
378
|
+
|
379
|
+
#Take a hash of the params to the post and generate a safe URL string.
|
380
|
+
# @param form_values [Hash] Keys are the field names, values are the field values
|
381
|
+
# @param has_q [Boolean] We have a leading ? for the html get, so don't need to add one.
|
382
|
+
# @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 ? "" : "?")
|
386
|
+
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)}"
|
390
|
+
first = false
|
391
|
+
end
|
392
|
+
return s
|
393
|
+
end
|
394
|
+
end
|
395
|
+
end
|
396
|
+
|
data/version
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wikk_webbrowser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Burrowes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: hoe-yard
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 0.1.3
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.1.3
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: hoe
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.22'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.22'
|
61
|
+
description: |-
|
62
|
+
Wrapper around ruby http and https libraries.
|
63
|
+
|
64
|
+
Converted to a gem from a mixture of versions I've used over the years. Might need some work yet :)
|
65
|
+
email:
|
66
|
+
- r.burrowes@auckland.ac.nz
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files:
|
70
|
+
- History.txt
|
71
|
+
- Manifest.txt
|
72
|
+
- README.md
|
73
|
+
files:
|
74
|
+
- History.txt
|
75
|
+
- Manifest.txt
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- lib/wikk_webbrowser.rb
|
79
|
+
- version
|
80
|
+
homepage: https://wikarekare.github.io/wikk_webbrowser/
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options:
|
86
|
+
- "--markup"
|
87
|
+
- markdown
|
88
|
+
- "--protected"
|
89
|
+
- "--title"
|
90
|
+
- wikk_webbrowser
|
91
|
+
- "--quiet"
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.1.2
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Wrapper around ruby http and https libraries
|
109
|
+
test_files: []
|