Class: DoneDone::Multipart::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/donedone/multipart.rb

Overview

Formats a given hash as a multipart form post If determine if the params are Files or Strings and process appropriately

Constant Summary collapse

USERAGENT =

We have to pretend like we’re a web browser…

"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
BOUNDARY =
'--JTMultiPart' + rand(1000000).to_s + 'ZZZZZ'
CONTENT_TYPE =
"multipart/form-data; boundary=#{ BOUNDARY }"

Class Method Summary collapse

Class Method Details

.prepare_query(params) ⇒ Object

HEADER = { “Content-Type” => CONTENT_TYPE, “User-Agent” => USERAGENT } unless const_defined?(:HEADER)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/donedone/multipart.rb', line 22

def self.prepare_query(params)
  fp = []

  params.each do |k, v|
    if File.exists?(v) # must be a file
      path = File.path(v)
      content = File.binread(v)
      fp.push(FileParam.new(k, path, content))
    else # must be a string
      fp.push(StringParam.new(k, v))
    end
  end

  # Assemble the request body using the special multipart format
  query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("")  + "--" + BOUNDARY + "--"
  return query, CONTENT_TYPE, USERAGENT
end