Sha256: d877e427ad353f050a4dfe43f2e44cd3569071036210c866d01ab6c1f5e494e6

Contents?: true

Size: 710 Bytes

Versions: 4

Compression:

Stored size: 710 Bytes

Contents

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

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

    def open
      file = super(@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

4 entries across 4 versions & 1 rubygems

Version Path
anyfetch-0.1.6 lib/anyfetch/open_uri.rb
anyfetch-0.1.5 lib/anyfetch/open_uri.rb
anyfetch-0.1.4 lib/anyfetch/open_uri.rb
anyfetch-0.1.3 lib/anyfetch/open_uri.rb