class Net::HTTP::MultipartPost

Constants

BOUNDARY

Attributes

multipart_post_files[R]
params[R]
url[R]

Public Instance Methods

post() click to toggle source
# File lib/net/http_multipart_post.rb, line 30
def post
  req = Post.new(url.path)
  req.body = body
  req.content_type = content_type
  req.basic_auth url.user, url.password if url.user
  Net::HTTP.new(url.host, url.port).start {|http|
    http.request(req)
  }    
end

Protected Instance Methods

body() click to toggle source
# File lib/net/http_multipart_post.rb, line 63
def body
  encode_parameters + encode_multipart_post_files + final_boundary
end
content_type() click to toggle source
# File lib/net/http_multipart_post.rb, line 59
def content_type
  "multipart/form-data; boundary=#{BOUNDARY}"
end
disposition(attribute) click to toggle source
# File lib/net/http_multipart_post.rb, line 110
def disposition(attribute)
  "Content-Disposition: form-data; #{attribute}\r\n"
end
disposition_with_filename(name, filename) click to toggle source
# File lib/net/http_multipart_post.rb, line 98
def disposition_with_filename(name, filename)
  if name.nil?
    disposition("filename=\"#{filename}\"")
  else
    disposition("name=\"#{name}\"; filename=\"#{filename}\"")
  end
end
disposition_with_name(name) click to toggle source
# File lib/net/http_multipart_post.rb, line 106
def disposition_with_name(name)
  disposition("name=\"#{name}\"\r\n")
end
encode_multipart_post_file(name, multipart_post_file) click to toggle source
# File lib/net/http_multipart_post.rb, line 78
def encode_multipart_post_file(name, multipart_post_file)
  parameter_boundary + 
  disposition_with_filename(name, multipart_post_file.filename) + 
  file_content_type(multipart_post_file.content_type) + 
  multipart_post_file.data + 
  "\r\n"
end
encode_multipart_post_files() click to toggle source
# File lib/net/http_multipart_post.rb, line 67
def encode_multipart_post_files
  return "" if multipart_post_files.empty?
  if multipart_post_files.size == 1
    name = multipart_post_files.keys.first
    file = multipart_post_files.values.first
    encode_multipart_post_file(name, file)
  else
    raise "Currently more than 1 file upload is not supported."
  end
end
encode_parameter(key, value) click to toggle source
# File lib/net/http_multipart_post.rb, line 90
def encode_parameter(key, value)
  parameter_boundary + disposition_with_name(key) + value.to_s + "\r\n"
end
encode_parameters() click to toggle source
# File lib/net/http_multipart_post.rb, line 86
def encode_parameters
 params.sort_by{|key, value| key.to_s}.map{|key, value| encode_parameter(key, value)}.join
end
extract_file_parameters_from(hash) click to toggle source
# File lib/net/http_multipart_post.rb, line 45
def extract_file_parameters_from(hash)
  hash.reject{|key, value| !multipart_post_file?(value)}
end
extract_non_file_parameters_from(hash) click to toggle source
# File lib/net/http_multipart_post.rb, line 49
def extract_non_file_parameters_from(hash)
  hash.reject{|key, value| multipart_post_file?(value)}
end
file_content_type(string) click to toggle source
# File lib/net/http_multipart_post.rb, line 94
def file_content_type(string)
  "Content-Type: #{string}\r\n\r\n"
end
final_boundary() click to toggle source
# File lib/net/http_multipart_post.rb, line 118
def final_boundary
  "--#{BOUNDARY}--\r\n"
end
multipart_post_file?(object) click to toggle source
# File lib/net/http_multipart_post.rb, line 53
def multipart_post_file?(object)
  object.respond_to?(:content_type) &&
  object.respond_to?(:data) &&
  object.respond_to?(:filename)
end
parameter_boundary() click to toggle source
# File lib/net/http_multipart_post.rb, line 114
def parameter_boundary
  "--#{BOUNDARY}\r\n"
end

Public Class Methods

new(url, params) click to toggle source
# File lib/net/http_multipart_post.rb, line 24
def initialize(url, params)
  @url = url
  @multipart_post_files = extract_file_parameters_from(params)
  @params = extract_non_file_parameters_from(params)
end