Class: CloudApp::Multipart

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

Overview

TODO - Document the Multipart Class

Constant Summary

EOL =

:nodoc:

"\r\n"

Instance Method Summary (collapse)

Constructor Details

- (Multipart) initialize(params)

:nodoc:



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloudapp/multipart.rb', line 10

def initialize(params)
  @params = params
  file = @params.delete(:file)
  bound = "--#{boundary}"
  @body = bound + EOL
  @params.each do |key, val|
    @body += create_regular_field(key, val)
    @body += EOL + bound + EOL
  end
  @body += create_file_field(file)
  @body += EOL + bound + "--#{EOL}"
end

Instance Method Details

- (Object) boundary

:nodoc:



30
31
32
# File 'lib/cloudapp/multipart.rb', line 30

def boundary #:nodoc:
  @boundary ||= "#{HEADERS["User-Agent"]}.#{Array.new(16/2) { rand(256) }.pack("C*").unpack("H*").first}"
end

- (Object) create_file_field(file)

:nodoc:



38
39
40
41
42
# File 'lib/cloudapp/multipart.rb', line 38

def create_file_field(file) #:nodoc:
  %Q{Content-Disposition: form-data; name="file"; filename="#{File.basename(file.path)}"} + EOL +
    "Content-Type: #{mime_for(file.path)}" + EOL + EOL +
    file.read 
end

- (Object) create_regular_field(key, val)

:nodoc:



34
35
36
# File 'lib/cloudapp/multipart.rb', line 34

def create_regular_field(key, val) #:nodoc:
  %Q{Content-Disposition: form-data; name="#{key}"} + EOL + EOL + val
end

- (Object) mime_for(path)

:nodoc:



44
45
46
# File 'lib/cloudapp/multipart.rb', line 44

def mime_for(path) #:nodoc:
  (MIME::Types.type_for(path)[0] || MIME::Types["application/octet-stream"][0]).simplified
end

- (Object) payload

:nodoc:



23
24
25
26
27
28
# File 'lib/cloudapp/multipart.rb', line 23

def payload #:nodoc:
  {
    :headers => {"Content-Type" => "multipart/form-data; boundary=#{boundary}"},
    :body => @body
  }
end