Class: HTTP::FormData::Multipart
- Inherits:
-
Object
- Object
- HTTP::FormData::Multipart
- Includes:
- Readable
- Defined in:
- lib/http/form_data/multipart.rb,
lib/http/form_data/multipart/param.rb,
sig/http.rbs
Overview
multipart/form-data form data.
Defined Under Namespace
Classes: Param
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
Default MIME type for multipart form data
"multipart/form-data"
Instance Attribute Summary collapse
-
#boundary ⇒ String
readonly
Returns the multipart boundary string.
Class Method Summary collapse
-
.generate_boundary ⇒ String
Generates a boundary string for multipart form data.
Instance Method Summary collapse
-
#content_type ⇒ String
Returns MIME type for the Content-Type header.
-
#glue ⇒ String
Returns the boundary glue between parts.
-
#initialize(data, boundary: self.class.generate_boundary, content_type: DEFAULT_CONTENT_TYPE) ⇒ Multipart
constructor
Creates a new Multipart form data instance.
-
#parts(data) ⇒ Array[Param]
Coerces data into an array of Param objects.
-
#tail ⇒ String
Returns the closing boundary tail.
Methods included from Readable
Constructor Details
#initialize(data, boundary: self.class.generate_boundary, content_type: DEFAULT_CONTENT_TYPE) ⇒ Multipart
Creates a new Multipart form data instance
39 40 41 42 43 |
# File 'lib/http/form_data/multipart.rb', line 39 def initialize(data, boundary: self.class.generate_boundary, content_type: DEFAULT_CONTENT_TYPE) @boundary = boundary.to_s.freeze @content_type = content_type @io = CompositeIO.new(parts(data).flat_map { |part| [glue, part] } << tail) end |
Instance Attribute Details
#boundary ⇒ String (readonly)
Returns the multipart boundary string
25 26 27 |
# File 'lib/http/form_data/multipart.rb', line 25 def boundary @boundary end |
Class Method Details
.generate_boundary ⇒ String
Generates a boundary string for multipart form data
52 53 54 |
# File 'lib/http/form_data/multipart.rb', line 52 def self.generate_boundary ("-" * 21) << SecureRandom.hex(21) end |
Instance Method Details
#content_type ⇒ String
Returns MIME type for the Content-Type header
64 65 66 |
# File 'lib/http/form_data/multipart.rb', line 64 def content_type "#{@content_type}; boundary=#{@boundary}" end |
#glue ⇒ String
Returns the boundary glue between parts
83 84 85 |
# File 'lib/http/form_data/multipart.rb', line 83 def glue @glue ||= "--#{@boundary}#{CRLF}" end |
#parts(data) ⇒ Array[Param]
Coerces data into an array of Param objects
99 100 101 102 103 |
# File 'lib/http/form_data/multipart.rb', line 99 def parts(data) FormData.ensure_data(data).flat_map do |name, values| Array(values).map { |value| Param.new(name, value) } end end |
#tail ⇒ String
Returns the closing boundary tail
91 92 93 |
# File 'lib/http/form_data/multipart.rb', line 91 def tail @tail ||= "--#{@boundary}--#{CRLF}" end |