Method: Webhookdb::Http.chunked_download

Defined in:
lib/webhookdb/http.rb

.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