Class: Itrp::Multipart::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/itrp/client/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



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

def self.prepare_query(params)
  fp = []

  params.each do |k, v|
    if v.respond_to?(:path) && v.respond_to?(:read)
      fp.push(FileParam.new(k, v.path, v.read))
    else
      fp.push(StringParam.new(k, v))
    end
  end

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