`multipart/form-data` form data.
@param [#to_h, Hash] data form data key-value Hash
# File lib/http/form_data/multipart.rb, line 12 def initialize(data) @parts = Param.coerce FormData.ensure_hash data @boundary = (Array.new(21, "-") << SecureRandom.hex(21)).join("") @content_length = nil end
Returns form data content size to be used for HTTP request `Content-Length` header.
@return [Integer]
# File lib/http/form_data/multipart.rb, line 36 def content_length unless @content_length @content_length = head.bytesize + tail.bytesize @content_length += @parts.map(&:size).reduce(:+) @content_length += (glue.bytesize * (@parts.count - 1)) end @content_length end
Returns MIME type to be used for HTTP request `Content-Type` header.
@return [String]
# File lib/http/form_data/multipart.rb, line 28 def content_type "multipart/form-data; boundary=#{@boundary}" end
Returns content to be used for HTTP request body.
@return [String]
# File lib/http/form_data/multipart.rb, line 21 def to_s head + @parts.map(&:to_s).join(glue) + tail end
@return [String]
# File lib/http/form_data/multipart.rb, line 54 def glue @glue ||= "#{CRLF}--#{@boundary}#{CRLF}" end
@return [String]
# File lib/http/form_data/multipart.rb, line 49 def head @head ||= "--#{@boundary}#{CRLF}" end
@return [String]
# File lib/http/form_data/multipart.rb, line 59 def tail @tail ||= "#{CRLF}--#{@boundary}--" end