nethttputils 0.4.3.0 → 0.4.4.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 +5 -5
- data/lib/nethttputils.rb +14 -9
- data/nethttputils.gemspec +1 -3
- metadata +3 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: bf38b9ab048dec93ae1ed7cdc00746477bdfffe66394e3dca8c97dee19b85d2d
|
|
4
|
+
data.tar.gz: 44f1439d888cf72395de49ae8b2192193cb31f0648d57abb00be1a4d5cdcdb32
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8e445cb04ac14bb07b658d2211e048a1d51c1002ce4e0c50303a73e82474b4e6b74ce1d9d17b30de35a43f0cc0db1e12eeedd4a80cbc924a6974224f9d1dc282
|
|
7
|
+
data.tar.gz: 8c11645091d88847e45e4849d5782c9b562c98a5ef7613b502a5953263db5608e5f8eabadd5850089d85e06ce679912bc58716caec922e5c3cfb0e16a15bc56c
|
data/lib/nethttputils.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
require "net/http"
|
|
2
|
+
require "cgi"
|
|
2
3
|
require "openssl"
|
|
3
4
|
|
|
4
5
|
require "logger"
|
|
5
6
|
|
|
6
|
-
|
|
7
7
|
module NetHTTPUtils
|
|
8
8
|
class << self
|
|
9
9
|
attr_accessor :logger
|
|
@@ -16,8 +16,10 @@ module NetHTTPUtils
|
|
|
16
16
|
|
|
17
17
|
class Error < RuntimeError
|
|
18
18
|
attr_reader :code
|
|
19
|
+
attr_reader :body
|
|
19
20
|
def initialize body, code = nil
|
|
20
21
|
@code = code
|
|
22
|
+
@body = body
|
|
21
23
|
body = body[0...997] + "..." if body.size > 1000
|
|
22
24
|
super "HTTP error ##{code.inspect} #{body}"
|
|
23
25
|
end
|
|
@@ -26,6 +28,7 @@ module NetHTTPUtils
|
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
class << self
|
|
31
|
+
require "addressable"
|
|
29
32
|
|
|
30
33
|
def remove_tags str
|
|
31
34
|
str.gsub(/<script( [a-z-]+="[^"]*")*>.*?<\/script>/m, "").
|
|
@@ -36,12 +39,14 @@ module NetHTTPUtils
|
|
|
36
39
|
def start_http url, max_start_http_retry_delay = 3600, timeout = nil, no_redirect = false, proxy = nil
|
|
37
40
|
timeout ||= 30
|
|
38
41
|
uri = url
|
|
39
|
-
|
|
42
|
+
|
|
43
|
+
uri = begin
|
|
40
44
|
URI url
|
|
41
|
-
url
|
|
42
45
|
rescue URI::InvalidURIError
|
|
43
|
-
URI.escape url
|
|
46
|
+
URI Addressable::URI.escape url
|
|
44
47
|
end unless url.is_a? URI::HTTP
|
|
48
|
+
raise Error, "can't parse host" unless uri.host
|
|
49
|
+
|
|
45
50
|
delay = 5
|
|
46
51
|
begin
|
|
47
52
|
Net::HTTP.start(
|
|
@@ -129,11 +134,12 @@ module NetHTTPUtils
|
|
|
129
134
|
# request.basic_auth *p(auth.map(&URI.method(:escape))) if auth
|
|
130
135
|
request.basic_auth *auth if auth
|
|
131
136
|
if (mtd == :POST || mtd == :PATCH) && !form.empty?
|
|
137
|
+
form.replace form.map{ |k, v| [k.to_s, v.is_a?(Integer) ? v.to_s : v] }.to_h
|
|
132
138
|
case type
|
|
133
139
|
when :json
|
|
134
140
|
request.body = JSON.dump form
|
|
135
141
|
request.content_type = "application/json"
|
|
136
|
-
when :multipart
|
|
142
|
+
when :multipart # in this case form can be of width 3 (when sending files)
|
|
137
143
|
request.set_form form, "multipart/form-data"
|
|
138
144
|
when :form
|
|
139
145
|
if form.any?{ |k, v| v.respond_to? :to_path }
|
|
@@ -171,7 +177,7 @@ module NetHTTPUtils
|
|
|
171
177
|
end.chunk(&:first).map do |file, group|
|
|
172
178
|
"#{file}:#{group.map(&:last).chunk{|_|_}.map(&:first).join(",")}"
|
|
173
179
|
end
|
|
174
|
-
logger.
|
|
180
|
+
logger.info stack.join " -> "
|
|
175
181
|
end
|
|
176
182
|
end
|
|
177
183
|
do_request = lambda do |request|
|
|
@@ -206,10 +212,10 @@ module NetHTTPUtils
|
|
|
206
212
|
response.fetch("x-rate-limit-reset").to_i,
|
|
207
213
|
now.to_i,
|
|
208
214
|
]
|
|
209
|
-
elsif response.key? "x-ratelimit-remaining"
|
|
215
|
+
elsif response.key? "x-ratelimit-remaining" # this is probably reddit-specific because ...
|
|
210
216
|
[
|
|
211
217
|
response.fetch("x-ratelimit-remaining").to_i,
|
|
212
|
-
now + response.fetch("x-ratelimit-reset").to_i,
|
|
218
|
+
now.to_i + response.fetch("x-ratelimit-reset").to_i, # ... reddit sucks and so does not use timestamp here
|
|
213
219
|
now.to_i,
|
|
214
220
|
]
|
|
215
221
|
end
|
|
@@ -242,7 +248,6 @@ module NetHTTPUtils
|
|
|
242
248
|
when /\A30\d\z/
|
|
243
249
|
next response if no_redirect
|
|
244
250
|
logger.info "redirect: #{response["location"]}"
|
|
245
|
-
require "addressable"
|
|
246
251
|
new_uri = URI.join request.uri.to_s, Addressable::URI.escape(response["location"])
|
|
247
252
|
new_host = new_uri.host
|
|
248
253
|
raise Error.new "redirected in place" if new_uri == http.instance_variable_get(:@uri)
|
data/nethttputils.gemspec
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Gem::Specification.new do |spec|
|
|
2
2
|
spec.name = "nethttputils"
|
|
3
|
-
spec.version = "0.4.
|
|
3
|
+
spec.version = "0.4.4.0"
|
|
4
4
|
spec.summary = "this tool is like a pet that I adopted young and now I depend on, sorry"
|
|
5
5
|
spec.description = <<-EOF
|
|
6
6
|
Back in 2015 I was a guy automating things at my job and two scripts had a common need --
|
|
@@ -20,8 +20,6 @@ Gem::Specification.new do |spec|
|
|
|
20
20
|
spec.email = "nakilon@gmail.com"
|
|
21
21
|
spec.license = "MIT"
|
|
22
22
|
|
|
23
|
-
spec.require_path = "lib"
|
|
24
23
|
spec.files = %w{ LICENSE nethttputils.gemspec lib/nethttputils.rb }
|
|
25
|
-
|
|
26
24
|
spec.add_dependency "addressable"
|
|
27
25
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nethttputils
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Victor Maslov aka Nakilon
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2023-05-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: addressable
|
|
@@ -62,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
62
62
|
- !ruby/object:Gem::Version
|
|
63
63
|
version: '0'
|
|
64
64
|
requirements: []
|
|
65
|
-
|
|
66
|
-
rubygems_version: 2.5.2.3
|
|
65
|
+
rubygems_version: 3.3.25
|
|
67
66
|
signing_key:
|
|
68
67
|
specification_version: 4
|
|
69
68
|
summary: this tool is like a pet that I adopted young and now I depend on, sorry
|