class HTTP::FormData::Multipart::Param
Utility class to represent multi-part chunks
Public Class Methods
coerce(data)
click to toggle source
Flattens given `data` Hash into an array of `Param`'s. Nested array are unwinded. Behavior is similar to `URL.encode_www_form`.
@param [Hash] data @return [Array<FormData::MultiPart::Param>]
# 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
new(name, value)
click to toggle source
@param [#to_s] name @param [FormData::File, to_s] value
# 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
Public Instance Methods
size()
click to toggle source
Calculates size of a part (headers + body).
@return [Fixnum]
# 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()
click to toggle source
Returns body part with headers and data.
@example With {FormData::File} value
Content-Disposition: form-data; name="avatar"; filename="avatar.png" Content-Type: application/octet-stream ...data of avatar.png...
@example With non-{FormData::File} value
Content-Disposition: form-data; name="username" ixti
@return [String]
# File lib/http/form_data/multipart/param.rb, line 36 def to_s "#{@header}#{CRLF * 2}#{@value}" end
Private Instance Methods
file?()
click to toggle source
Tells whenever value is a {FormData::File} or not.
@return [Boolean]
# File lib/http/form_data/multipart/param.rb, line 76 def file? @value.is_a? FormData::File end