Class: Infrawrench::Upload
- Inherits:
-
Object
- Object
- Infrawrench::Upload
- Defined in:
- lib/infrawrench/transport.rb,
sig/infrawrench/sdk.rbs
Overview
A file to send in a multipart/form-data request.
Call sites rarely need this: a plain String of bytes or any IO-like object is accepted wherever a file is expected, and Upload.coerce wraps it. Construct one explicitly only to control the filename or content type the server sees.
Instance Attribute Summary collapse
-
#bytes ⇒ String
readonly
The file's bytes.
-
#content_type ⇒ String
readonly
Content-Type sent for the part.
-
#filename ⇒ String
readonly
Filename sent in the part's Content-Disposition.
Class Method Summary collapse
-
.coerce(value) ⇒ Upload
Wrap whatever a caller passed for a file field.
Instance Method Summary collapse
-
#initialize(content, filename: "file", content_type: "application/octet-stream") ⇒ Upload
constructor
A new instance of Upload.
Constructor Details
#initialize(content, filename: "file", content_type: "application/octet-stream") ⇒ Upload
Returns a new instance of Upload.
75 76 77 78 79 |
# File 'lib/infrawrench/transport.rb', line 75 def initialize(content, filename: "file", content_type: "application/octet-stream") @bytes = (content.respond_to?(:read) ? content.read : content.to_s).b @filename = filename @content_type = content_type end |
Instance Attribute Details
#bytes ⇒ String (readonly)
Returns The file's bytes.
69 70 71 |
# File 'lib/infrawrench/transport.rb', line 69 def bytes @bytes end |
#content_type ⇒ String (readonly)
Returns Content-Type sent for the part.
73 74 75 |
# File 'lib/infrawrench/transport.rb', line 73 def content_type @content_type end |
#filename ⇒ String (readonly)
Returns Filename sent in the part's Content-Disposition.
71 72 73 |
# File 'lib/infrawrench/transport.rb', line 71 def filename @filename end |
Class Method Details
.coerce(value) ⇒ Upload
Wrap whatever a caller passed for a file field.
A File keeps its basename, because a server that stores the upload under
the name it was given should get the name the user actually chose.
88 89 90 91 92 93 94 |
# File 'lib/infrawrench/transport.rb', line 88 def self.coerce(value) return value if value.is_a?(Upload) return new(value) unless value.respond_to?(:read) path = value.respond_to?(:path) ? value.path : nil path ? new(value, filename: File.basename(path)) : new(value) end |