class Salesfly::Multipart
Public Instance Methods
encode(params, files)
click to toggle source
# File lib/salesfly/multipart.rb, line 8 def encode(params, files) boundary = self.get_random_string() lines = [] params.each do |key, value| if value.kind_of?(Array) value.each do |v| lines.push(StringParam.new(key, v)) end else lines.push(StringParam.new(key, value)) end end files.each do |fn| lines.push(FileParam.new(fn)) end content = lines.collect {|p| "--" + boundary + "\r\n" + p.to_multipart }.join("") + "--" + boundary + "--" headers = { "Content-Type" => "multipart/form-data; boundary=#{ boundary }", "Content-Length" => content.length.to_s } return content, headers end
get_random_string(length=32)
click to toggle source
# File lib/salesfly/multipart.rb, line 34 def get_random_string(length=32) source=("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a key="" length.times{ key += source[rand(source.size)].to_s } return key end