class HTTP::FormData::File

Represents file form param.

@example Usage with StringIO

io = StringIO.new "foo bar baz"
FormData::File.new io, :filename => "foobar.txt"

@example Usage with IO

File.open "/home/ixti/avatar.png" do |io|
  FormData::File.new io
end

@example Usage with pathname

FormData::File.new "/home/ixti/avatar.png"

Constants

DEFAULT_MIME

Default MIME type

Public Class Methods

new(path_or_io, opts = {}) click to toggle source

@see DEFAULT_MIME @param [String, Pathname, IO] path_or_io Filename or IO instance. @param [#to_h] opts @option opts [#to_s] :content_type (DEFAULT_MIME)

Value of Content-Type header

@option opts [#to_s] :filename

When `path_or_io` is a String, Pathname or File, defaults to basename.
When `path_or_io` is a IO, defaults to `"stream-{object_id}"`.
# File lib/http/form_data/file.rb, line 35
def initialize(path_or_io, opts = {})
  opts = FormData.ensure_hash(opts)

  if opts.key? :mime_type
    warn "[DEPRECATED] :mime_type option deprecated, use :content_type"
    opts[:content_type] = opts[:mime_type]
  end

  @io           = make_io(path_or_io)
  @content_type = opts.fetch(:content_type, DEFAULT_MIME).to_s
  @filename     = opts.fetch(:filename, filename_for(@io))
end

Private Instance Methods

filename_for(io) click to toggle source
# File lib/http/form_data/file.rb, line 60
def filename_for(io)
  if io.respond_to?(:path)
    ::File.basename io.path
  else
    "stream-#{io.object_id}"
  end
end
make_io(path_or_io) click to toggle source
# File lib/http/form_data/file.rb, line 50
def make_io(path_or_io)
  if path_or_io.is_a?(String)
    ::File.open(path_or_io, :binmode => true)
  elsif defined?(Pathname) && path_or_io.is_a?(Pathname)
    path_or_io.open(:binmode => true)
  else
    path_or_io
  end
end