Class: CloudApp::Multipart

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

Overview

TODO - Document the Multipart Class

Constant Summary collapse

EOL =

:nodoc:

"\r\n"

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Multipart

Returns a new instance of Multipart


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

#boundaryObject

:nodoc:


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

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

#create_file_field(file) ⇒ Object

:nodoc:


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

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

#create_regular_field(key, val) ⇒ Object

:nodoc:


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

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

#mime_for(path) ⇒ Object

:nodoc:


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

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

#payloadObject

:nodoc:


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

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