Class: HTTP::FormData::File

Inherits:
Part
  • Object
show all
Defined in:
lib/http/form_data/file.rb

Overview

Represents file form param.

Examples:

Usage with StringIO


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

Usage with IO


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

Usage with pathname


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

Constant Summary collapse

DEFAULT_MIME =

Default MIME type

"application/octet-stream"

Instance Attribute Summary

Attributes inherited from Part

#content_type, #filename

Instance Method Summary collapse

Methods included from Readable

#read, #rewind, #size, #to_s

Constructor Details

#initialize(path_or_io, opts = {}) ⇒ File

Returns a new instance of File.

Parameters:

  • path_or_io (String, Pathname, IO)

    Filename or IO instance.

  • opts (#to_h) (defaults to: {})

Options Hash (opts):

  • :content_type (#to_s) — default: DEFAULT_MIME

    Value of Content-Type header

  • :filename (#to_s)

    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}".

See Also:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/http/form_data/file.rb', line 36

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