Class: Retab::Multipart::FilePart
- Inherits:
-
Object
- Object
- Retab::Multipart::FilePart
- Defined in:
- lib/retab/multipart.rb
Overview
A single binary file part. ‘content` may be a String of bytes, a `Pathname`, or any IO-like object (File/StringIO). Normalised to an IO so net/http streams it as a real file upload.
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
"application/octet-stream"
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Instance Method Summary collapse
- #form_opts ⇒ Object
-
#initialize(content, filename: "file", content_type: DEFAULT_CONTENT_TYPE) ⇒ FilePart
constructor
A new instance of FilePart.
-
#io ⇒ Object
An IO net/http can read the bytes from.
Constructor Details
#initialize(content, filename: "file", content_type: DEFAULT_CONTENT_TYPE) ⇒ FilePart
Returns a new instance of FilePart.
50 51 52 53 54 |
# File 'lib/retab/multipart.rb', line 50 def initialize(content, filename: "file", content_type: DEFAULT_CONTENT_TYPE) @content = content @filename = filename @content_type = content_type end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
48 49 50 |
# File 'lib/retab/multipart.rb', line 48 def content_type @content_type end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
48 49 50 |
# File 'lib/retab/multipart.rb', line 48 def filename @filename end |
Instance Method Details
#form_opts ⇒ Object
68 69 70 |
# File 'lib/retab/multipart.rb', line 68 def form_opts {filename: @filename, content_type: @content_type} end |
#io ⇒ Object
An IO net/http can read the bytes from. Strings are wrapped in a StringIO; Pathnames are opened in binary mode; existing IOs pass through unchanged.
59 60 61 62 63 64 65 66 |
# File 'lib/retab/multipart.rb', line 59 def io return @content if @content.respond_to?(:read) if defined?(Pathname) && @content.is_a?(Pathname) return @content.open("rb") end StringIO.new(@content.to_s) end |