Sha256: 7df71700bf23ed6518758c15a5d9671b8b8df2c24b51ae63e9785ce62ca755d0
Contents?: true
Size: 1.38 KB
Versions: 2
Compression:
Stored size: 1.38 KB
Contents
# frozen_string_literal: true require "securerandom" require "http/form_data/multipart/param" require "http/form_data/readable" require "http/form_data/composite_io" module HTTP module FormData # `multipart/form-data` form data. class Multipart include Readable attr_reader :boundary # @param [#to_h, Hash] data form data key-value Hash def initialize(data, boundary: self.class.generate_boundary) parts = Param.coerce FormData.ensure_hash data @boundary = boundary.to_s.freeze @io = CompositeIO.new [*parts.flat_map { |part| [glue, part] }, tail] end # Generates a string suitable for using as a boundary in multipart form # data. # # @return [String] def self.generate_boundary ("-" * 21) << SecureRandom.hex(21) end # Returns MIME type to be used for HTTP request `Content-Type` header. # # @return [String] def content_type "multipart/form-data; boundary=#{@boundary}" end # Returns form data content size to be used for HTTP request # `Content-Length` header. # # @return [Integer] alias content_length size private # @return [String] def glue @glue ||= "--#{@boundary}#{CRLF}" end # @return [String] def tail @tail ||= "--#{@boundary}--" end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
http-form_data-2.0.0.pre2 | lib/http/form_data/multipart.rb |
http-form_data-2.0.0.pre1 | lib/http/form_data/multipart.rb |