Sha256: d540420314c622357063e8a54cec0d73b67e10e44c79b1e0defcd74a6e3e97aa

Contents?: true

Size: 723 Bytes

Versions: 1

Compression:

Stored size: 723 Bytes

Contents

require "tempfile"
require "anyfetch/original_filename/content_type"

module Anyfetch
  class OpenURI
    def initialize(*args)
      raise NotImplementedError
    end

    def open
      file = ::OpenURI.open_uri(@uri, @options)

      if file.is_a?(StringIO)
        file = to_tempfile(file)
      end

      file.extend(OriginalFilename::ContentType)
      file
    end

    private

    # OpenURI returns either Tempfile or StringIO depending of the size of
    # the response. We want to unify this and always return Tempfile.
    def to_tempfile(io)
      tempfile = Tempfile.new('open-uri')
      tempfile.binmode
      ::OpenURI::Meta.init(tempfile, io)
      tempfile << io.string
      tempfile
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anyfetch-0.1.7 lib/anyfetch/open_uri.rb