Class: HTTP::FormData::Multipart::Param
- Inherits:
-
Object
- Object
- HTTP::FormData::Multipart::Param
- Defined in:
- lib/http/form_data/multipart/param.rb
Overview
Utility class to represent multi-part chunks
Class Method Summary collapse
-
.coerce(data) ⇒ Array<FormData::MultiPart::Param>
Flattens given
dataHash into an array ofParam's.
Instance Method Summary collapse
-
#initialize(name, value) ⇒ Param
constructor
A new instance of Param.
-
#size ⇒ Fixnum
Calculates size of a part (headers + body).
-
#to_s ⇒ String
Returns body part with headers and data.
Constructor Details
#initialize(name, value) ⇒ Param
Returns a new instance of Param.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/http/form_data/multipart/param.rb', line 8 def initialize(name, value) @name, @value = name.to_s, value @header = "Content-Disposition: form-data; name=#{@name.inspect}" return unless file? @header << "; filename=#{value.filename.inspect}" @header << CRLF @header << "Content-Type: #{value.mime_type}" end |
Class Method Details
.coerce(data) ⇒ Array<FormData::MultiPart::Param>
Flattens given data Hash into an array of Param's.
Nested array are unwinded.
Behavior is similar to URL.encode_www_form.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/http/form_data/multipart/param.rb', line 59 def self.coerce(data) params = [] data.each do |name, values| Array(values).each do |value| params << new(name, value) end end params end |
Instance Method Details
#size ⇒ Fixnum
Calculates size of a part (headers + body).
43 44 45 46 47 48 49 50 51 |
# File 'lib/http/form_data/multipart/param.rb', line 43 def size size = @header.bytesize + (CRLF.bytesize * 2) if file? size + @value.size else size + @value.to_s.bytesize end end |
#to_s ⇒ String
Returns body part with headers and data.
36 37 38 |
# File 'lib/http/form_data/multipart/param.rb', line 36 def to_s "#{@header}#{CRLF * 2}#{@value}" end |