Class: UploadIO

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_io.rb

Overview

Convenience methods for dealing with files and IO that are to be uploaded.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io, content_type, filename = nil) ⇒ UploadIO

Returns a new instance of UploadIO.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/composite_io.rb', line 65

def initialize(filename_or_io, content_type, filename = nil)
  io = filename_or_io
  local_path = ""
  if io.respond_to? :read
    # in Ruby 1.9.2, StringIOs no longer respond to path
    # (since they respond to :length, so we don't need their local path, see parts.rb:41)
    local_path = filename_or_io.respond_to?(:path) ? filename_or_io.path : "local.path"
  else
    io = File.open(filename_or_io)
    local_path = filename_or_io
  end
  filename ||= local_path

  @content_type = content_type
  @original_filename = File.basename(filename)
  @local_path = local_path
  @io = io
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



88
89
90
# File 'lib/composite_io.rb', line 88

def method_missing(*args)
  @io.send(*args)
end

Instance Attribute Details

#content_typeObject (readonly)

Create an upload IO suitable for including in the params hash of a Net::HTTP::Post::Multipart.

Can take two forms. The first accepts a filename and content type, and opens the file for reading (to be closed by finalizer). The second accepts an already-open IO, but also requires a third argument, the filename from which it was opened.

UploadIO.new("file.txt", "text/plain")
UploadIO.new(file_io, "text/plain", "file.txt")


63
64
65
# File 'lib/composite_io.rb', line 63

def content_type
  @content_type
end

#ioObject (readonly)

Create an upload IO suitable for including in the params hash of a Net::HTTP::Post::Multipart.

Can take two forms. The first accepts a filename and content type, and opens the file for reading (to be closed by finalizer). The second accepts an already-open IO, but also requires a third argument, the filename from which it was opened.

UploadIO.new("file.txt", "text/plain")
UploadIO.new(file_io, "text/plain", "file.txt")


63
64
65
# File 'lib/composite_io.rb', line 63

def io
  @io
end

#local_pathObject (readonly)

Create an upload IO suitable for including in the params hash of a Net::HTTP::Post::Multipart.

Can take two forms. The first accepts a filename and content type, and opens the file for reading (to be closed by finalizer). The second accepts an already-open IO, but also requires a third argument, the filename from which it was opened.

UploadIO.new("file.txt", "text/plain")
UploadIO.new(file_io, "text/plain", "file.txt")


63
64
65
# File 'lib/composite_io.rb', line 63

def local_path
  @local_path
end

#original_filenameObject (readonly)

Create an upload IO suitable for including in the params hash of a Net::HTTP::Post::Multipart.

Can take two forms. The first accepts a filename and content type, and opens the file for reading (to be closed by finalizer). The second accepts an already-open IO, but also requires a third argument, the filename from which it was opened.

UploadIO.new("file.txt", "text/plain")
UploadIO.new(file_io, "text/plain", "file.txt")


63
64
65
# File 'lib/composite_io.rb', line 63

def original_filename
  @original_filename
end

Class Method Details

.convert!(io, content_type, original_filename, local_path) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
# File 'lib/composite_io.rb', line 84

def self.convert!(io, content_type, original_filename, local_path)
  raise ArgumentError, "convert! has been removed. You must now wrap IOs using:\nUploadIO.new(filename_or_io, content_type, filename=nil)\nPlease update your code."
end

Instance Method Details

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/composite_io.rb', line 92

def respond_to?(meth)
  @io.respond_to?(meth) || super(meth)
end