Module: Webhookdb::Http

Defined in:
lib/webhookdb/http.rb

Defined Under Namespace

Classes: BaseError, Error

Class Method Summary collapse

Class Method Details

._setup_required_args(options) ⇒ Object

Raises:

  • (ArgumentError)


86
87
88
89
90
91
# File 'lib/webhookdb/http.rb', line 86

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
end

.check!(response, **options) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
# File 'lib/webhookdb/http.rb', line 54

def self.check!(response, **options)
  # All oks are ok
  return if response.code < 300
  # We expect 300s if we aren't following redirects
  return if response.code < 400 && !options[:follow_redirects]
  # Raise for 400s, or 300s if we were meant to follow redirects
  raise Error, response
end

.chunked_download(request_url, rewindable: false, **down_kw) ⇒ Object

Convenience wrapper around Down that handles gzip.



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/webhookdb/http.rb', line 95

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")
    # If the response is gzipped, Down doesn't handle it properly.
    # Wrap it with gzip reader, and force the encoding to binary
    # the server may send back a header like Content-Type: text/plain; UTF-8,
    # so each line Down yields via #gets will have force_encoding('utf-8').
    # https://github.com/janko/down/issues/87
    io.instance_variable_set(:@encoding, "binary")
    io = Zlib::GzipReader.wrap(io)
  end
  return io
end

.extract_url_auth(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/webhookdb/http.rb', line 40

def self.extract_url_auth(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



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/webhookdb/http.rb', line 63

def self.get(url, query={}, **options, &)
  self._setup_required_args(options)
  opts = {query:, headers: {}}.merge(**options)
  opts[:headers]["User-Agent"] = self.user_agent
  # See https://github.com/jnunemaker/httparty/issues/784#issuecomment-1585714745
  # I *think* this should be safe to always use.
  opts[:headers]["Connection"] ||= "keep-alive"
  r = HTTParty.get(url, **opts, &)
  self.check!(r, **opts)
  return r
end

.gzipped?(string) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/webhookdb/http.rb', line 109

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/webhookdb/http.rb', line 75

def self.post(url, body={}, headers: {}, method: nil, check: true, **options, &)
  self._setup_required_args(options)
  headers["Content-Type"] ||= "application/json"
  headers["User-Agent"] = self.user_agent
  body = body.to_json if !body.is_a?(String) && headers["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

.user_agentObject



35
36
37
38
# File 'lib/webhookdb/http.rb', line 35

def self.user_agent
  return Webhookdb.http_user_agent unless Webhookdb.http_user_agent.blank?
  return "WebhookDB/#{Webhookdb::RELEASE} https://webhookdb.com #{Webhookdb::RELEASE_CREATED_AT}"
end