Module: Webhookdb::Http

Includes:
Appydays::Configurable
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)


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

Raises:



59
60
61
62
63
64
65
66
# File 'lib/webhookdb/http.rb', line 59

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.



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")
    # 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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webhookdb/http.rb', line 45

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



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
  # 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)


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)
  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



40
41
42
43
# File 'lib/webhookdb/http.rb', line 40

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