Class: Multipart::Post

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

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 }

Instance Method Summary collapse

Constructor Details

#initializePost

Returns a new instance of Post.



21
22
23
# File 'lib/it_tools/multipart.rb', line 21

def initialize
  @fp = []
end

Instance Method Details

#add_file(filename, contents) ⇒ Object



24
25
26
# File 'lib/it_tools/multipart.rb', line 24

def add_file(filename, contents)
  @fp.push(FileParam.new(filename, filename, contents))
end

#add_params(params = {}) ⇒ Object



27
28
29
30
31
# File 'lib/it_tools/multipart.rb', line 27

def add_params(params = {})
  params.each do |k, v|
    @fp.push(StringParam.new(k, v))
  end
end

#get_queryObject



41
42
43
44
# File 'lib/it_tools/multipart.rb', line 41

def get_query
  query = @fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("")  + "--" + BOUNDARY + "--"
  return query, HEADER
end

#post(url = "http://something.com/uploads") ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/it_tools/multipart.rb', line 32

def post(url = "http://something.com/uploads")
  uri = URI.parse(url)
  post_body = get_query
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = post_body.join
  request["Content-Type"] = "multipart/form-data, boundary=#{BOUNDARY}"
  http.request(request)
end