Class: Philiprehberger::Multipart::Part
- Inherits:
-
Object
- Object
- Philiprehberger::Multipart::Part
- Defined in:
- lib/philiprehberger/multipart/part.rb
Overview
Represents a single part in a multipart/form-data body
Instance Attribute Summary collapse
-
#content_type ⇒ String?
The content type.
-
#filename ⇒ String?
The original filename.
-
#name ⇒ Symbol
readonly
The field name.
-
#value ⇒ String
The part value / body content.
Instance Method Summary collapse
-
#body ⇒ String
Alias for value — the body content of this part.
-
#file? ⇒ Boolean
Whether this part represents a file upload.
-
#initialize(name, value, filename: nil, content_type: nil) ⇒ Part
constructor
A new instance of Part.
-
#to_s(boundary) ⇒ String
Render this part as a multipart body segment.
Constructor Details
#initialize(name, value, filename: nil, content_type: nil) ⇒ Part
Returns a new instance of Part.
23 24 25 26 27 28 |
# File 'lib/philiprehberger/multipart/part.rb', line 23 def initialize(name, value, filename: nil, content_type: nil) @name = name @value = value @filename = filename @content_type = content_type end |
Instance Attribute Details
#content_type ⇒ String?
Returns the content type.
17 18 19 |
# File 'lib/philiprehberger/multipart/part.rb', line 17 def content_type @content_type end |
#filename ⇒ String?
Returns the original filename.
14 15 16 |
# File 'lib/philiprehberger/multipart/part.rb', line 14 def filename @filename end |
#name ⇒ Symbol (readonly)
Returns the field name.
8 9 10 |
# File 'lib/philiprehberger/multipart/part.rb', line 8 def name @name end |
#value ⇒ String
Returns the part value / body content.
11 12 13 |
# File 'lib/philiprehberger/multipart/part.rb', line 11 def value @value end |
Instance Method Details
#body ⇒ String
Alias for value — the body content of this part
33 34 35 |
# File 'lib/philiprehberger/multipart/part.rb', line 33 def body @value end |
#file? ⇒ Boolean
Whether this part represents a file upload
40 41 42 |
# File 'lib/philiprehberger/multipart/part.rb', line 40 def file? !@filename.nil? end |
#to_s(boundary) ⇒ String
Render this part as a multipart body segment
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/philiprehberger/multipart/part.rb', line 48 def to_s(boundary) lines = [] lines << "--#{boundary}\r\n" if file? lines << "Content-Disposition: form-data; name=\"#{@name}\"; filename=\"#{@filename}\"\r\n" lines << "Content-Type: #{@content_type || 'application/octet-stream'}\r\n" else lines << "Content-Disposition: form-data; name=\"#{@name}\"\r\n" end lines << "\r\n" lines << @value lines << "\r\n" lines.join end |