Class: HTTP::FormData::File
- Defined in:
- lib/http/form_data/file.rb,
sig/http.rbs
Overview
Represents file form param.
Constant Summary collapse
- DEFAULT_MIME =
Default MIME type
"application/octet-stream"
Instance Attribute Summary
Attributes inherited from Part
Instance Method Summary collapse
-
#close ⇒ void
Closes the underlying IO if it was opened by this instance.
-
#filename_for(io) ⇒ String
Determines filename for the given IO.
-
#initialize(path_or_io, opts = nil) ⇒ File
constructor
Creates a new File from a path or IO object.
-
#make_io(path_or_io) ⇒ Object
Wraps path_or_io into an IO object.
Methods included from Readable
Constructor Details
#initialize(path_or_io, opts = nil) ⇒ File
Creates a new File from a path or IO object
39 40 41 42 43 44 45 46 |
# File 'lib/http/form_data/file.rb', line 39 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 |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Closes the underlying IO if it was opened by this instance
62 63 64 |
# File 'lib/http/form_data/file.rb', line 62 def close @io.close if @autoclose end |
#filename_for(io) ⇒ String
Determines filename for the given IO
86 87 88 89 90 91 92 |
# File 'lib/http/form_data/file.rb', line 86 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) ⇒ Object
Wraps path_or_io into an IO object
73 74 75 76 77 78 79 |
# File 'lib/http/form_data/file.rb', line 73 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 |