Class: Multipart::Post

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

Overview

Formats a given hash as a multipart form post If a hash value responds to :string or :read messages, then it is interpreted as a file and processed accordingly; otherwise, it is assumed to be a string

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 =
"0123456789ABLEWASIEREISAWELBA9876543210"
CONTENT_TYPE =
"multipart/form-data; boundary=#{ BOUNDARY }"
HEADER =
{ "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT }

Class Method Summary collapse

Class Method Details

.prepare_query(params) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/httpclient/multipart.rb', line 26

def self.prepare_query(params)
  fp = []
  
  params.each do |k, v|
    
    k = k.to_s
    
    # Are we trying to make a file parameter?
    if v.respond_to?(:path) and v.respond_to?(:read) then
      fp.push(FileParam.new(k, v.path, v.read))
    # We must be trying to make a regular parameter
    else
      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, HEADER
end