class HTTP::FormData::File

FormData::File.new “/home/ixti/avatar.png”
@example Usage with pathname
end
FormData::File.new io
File.open “/home/ixti/avatar.png” do |io|
@example Usage with IO
FormData::File.new io, filename: “foobar.txt”
io = StringIO.new “foo bar baz”
@example Usage with StringIO
Represents file form param.

def close

Returns:
  • (void) -

Other tags:
    Api: - public
def close
  @io.close if @autoclose
end

def filename_for(io)

Returns:
  • (String) -

Parameters:
  • io (IO) --

Other tags:
    Api: - private
def filename_for(io)
  if io.respond_to?(:path)
    ::File.basename(io.path)
  else
    "stream-#{io.object_id}"
  end
end

def initialize(path_or_io, opts = nil) # rubocop:disable Lint/MissingSuper

Options Hash: (**opts)
  • :filename (#to_s) --
  • :content_type (#to_s) --

Parameters:
  • opts (#to_h) --
  • path_or_io (String, Pathname, IO) -- Filename or IO instance

Other tags:
    See: DEFAULT_MIME -

Other tags:
    Api: - public
def initialize(path_or_io, opts = nil) # rubocop:disable Lint/MissingSuper
  opts = FormData.ensure_hash(opts)
  @io           = make_io(path_or_io)
  @autoclose    = path_or_io.is_a?(String) || path_or_io.is_a?(Pathname)
  @content_type = opts.fetch(:content_type, DEFAULT_MIME).to_s
  @filename     = opts.fetch(:filename, filename_for(@io))
end

def make_io(path_or_io)

Returns:
  • (IO) -

Parameters:
  • path_or_io (String, Pathname, IO) --

Other tags:
    Api: - private
def make_io(path_or_io)
  case path_or_io
  when String   then ::File.new(path_or_io, binmode: true)
  when Pathname then path_or_io.open(binmode: true)
  else path_or_io
  end
end