Sha256: 52e7a4278cb8c23fa8e19c406e861b9c38ac55136f75e485051e5da0803098e1
Contents?: true
Size: 1.13 KB
Versions: 2
Compression:
Stored size: 1.13 KB
Contents
require 'mime/types' class MultipartParams #:nodoc: attr_accessor :content_type, :body def initialize(param_hash={}) boundary_token = [Array.new(8) {rand(256)}].join self.content_type = "multipart/form-data; boundary=#{boundary_token}" boundary_marker = "--#{boundary_token}\r\n" self.body = param_hash.map { |param_name, param_value| boundary_marker + case param_value when String text_to_multipart(param_name, param_value) when File file_to_multipart(param_name, param_value) end }.join('') + "--#{boundary_token}--\r\n" end protected def file_to_multipart(key,file) filename = File.basename(file.path) mime_types = MIME::Types.of(filename) mime_type = mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type part = %Q[Content-Disposition: form-data; name="#{key}"; filename="#{filename}"\r\n] part += "Content-Transfer-Encoding: binary\r\n" part += "Content-Type: #{mime_type}\r\n\r\n#{file.read}\r\n" end def text_to_multipart(key,value) "Content-Disposition: form-data; name=\"#{key}\"\r\n\r\n#{value.to_s}\r\n" end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
viddler-0.2.0 | lib/viddler/multipart_params.rb |
viddler-0.1.0 | lib/viddler/multipart_params.rb |