Module: Webhookdb::Http
- Includes:
- Appydays::Configurable
- Defined in:
- lib/webhookdb/http.rb
Defined Under Namespace
Classes: BaseError, Error
Class Method Summary
collapse
-
._setup_required_args(options) ⇒ Object
-
.check!(response, **options) ⇒ Object
-
.chunked_download(request_url, rewindable: false, **down_kw) ⇒ Object
Convenience wrapper around Down that handles gzip.
-
.extract_url_auth(url) ⇒ Object
-
.get(url, query = {}, **options) ⇒ Object
-
.gzipped?(string) ⇒ Boolean
-
.post(url, body = {}, headers: {}, method: nil, check: true, **options) ⇒ Object
-
.user_agent ⇒ Object
Class Method Details
._setup_required_args(options) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/webhookdb/http.rb', line 91
def self._setup_required_args(options)
raise ArgumentError, "must pass :timeout keyword" unless options.key?(:timeout)
raise ArgumentError, "must pass :logger keyword" unless options.key?(:logger)
options[:log_format] = :appydays
options[:log_level] = self.log_level
end
|
.check!(response, **options) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/webhookdb/http.rb', line 59
def self.check!(response, **options)
return if response.code < 300
return if response.code < 400 && !options[:follow_redirects]
raise Error, response
end
|
.chunked_download(request_url, rewindable: false, **down_kw) ⇒ Object
Convenience wrapper around Down that handles gzip.
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/webhookdb/http.rb', line 101
def self.chunked_download(request_url, rewindable: false, **down_kw)
io = Down::NetHttp.open(request_url, rewindable:, **down_kw)
if io.data[:headers].fetch("Content-Encoding", "").include?("gzip")
io.instance_variable_set(:@encoding, "binary")
io = Zlib::GzipReader.wrap(io)
end
return io
end
|
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/webhookdb/http.rb', line 45
def self.(url)
parsed_uri = URI(url)
if parsed_uri.userinfo.present?
auth_params = {
username: URI.decode_www_form_component(parsed_uri.user || ""),
password: URI.decode_www_form_component(parsed_uri.password || ""),
}
parsed_uri.user = parsed_uri.password = nil
cleaned_url = parsed_uri.to_s
return cleaned_url, auth_params
end
return url, nil
end
|
.get(url, query = {}, **options) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/webhookdb/http.rb', line 68
def self.get(url, query={}, **options, &)
self._setup_required_args(options)
opts = {query:, headers: {}}.merge(**options)
opts[:headers]["User-Agent"] = self.user_agent
opts[:headers]["Connection"] ||= "keep-alive"
r = HTTParty.get(url, **opts, &)
self.check!(r, **opts)
return r
end
|
.gzipped?(string) ⇒ Boolean
115
116
117
118
119
|
# File 'lib/webhookdb/http.rb', line 115
def self.gzipped?(string)
return false if string.length < 3
b = string[..2].bytes
return b[0] == 0x1f && b[1] == 0x8b
end
|
.post(url, body = {}, headers: {}, method: nil, check: true, **options) ⇒ Object
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/webhookdb/http.rb', line 80
def self.post(url, body={}, headers: {}, method: nil, check: true, **options, &)
self._setup_required_args(options)
["Content-Type"] ||= "application/json"
["User-Agent"] = self.user_agent
body = body.to_json if !body.is_a?(String) && ["Content-Type"].include?("json")
opts = {body:, headers:}.merge(**options)
r = HTTParty.send(method || :post, url, **opts, &)
self.check!(r, **options) if check
return r
end
|