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 ⇒ Integer
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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/http/form_data/multipart/param.rb', line 10 def initialize(name, value) @name = name.to_s @part = if value.is_a?(FormData::Part) value else FormData::Part.new(value) end parameters = { :name => @name } parameters[:filename] = @part.filename if @part.filename parameters = parameters.map { |k, v| "#{k}=#{v.inspect}" }.join("; ") @header = "Content-Disposition: form-data; #{parameters}" return unless @part.content_type @header += "#{CRLF}Content-Type: #{@part.content_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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/http/form_data/multipart/param.rb', line 64 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 ⇒ Integer
Calculates size of a part (headers + body).
54 55 56 |
# File 'lib/http/form_data/multipart/param.rb', line 54 def size @header.bytesize + (CRLF.bytesize * 2) + @part.size end |
#to_s ⇒ String
Returns body part with headers and data.
47 48 49 |
# File 'lib/http/form_data/multipart/param.rb', line 47 def to_s "#{@header}#{CRLF * 2}#{@part}" end |