Class: Resourceful::MultipartFormData

Inherits:
AbstractFormData show all
Defined in:
lib/resourceful/multipart_form_data.rb

Defined Under Namespace

Classes: FileParamValue

Instance Method Summary collapse

Methods inherited from AbstractFormData

#add, #initialize

Constructor Details

This class inherits a constructor from Resourceful::AbstractFormData

Instance Method Details

#add_file(name, file_name, content_type = "application/octet-stream") ⇒ Object



7
8
9
# File 'lib/resourceful/multipart_form_data.rb', line 7

def add_file(name, file_name, content_type="application/octet-stream")
  add(name, FileParamValue.new(File.new(file_name, 'r'), File.basename(file_name), content_type))
end

#content_typeObject



11
12
13
# File 'lib/resourceful/multipart_form_data.rb', line 11

def content_type
  "multipart/form-data; boundary=#{boundary}"
end

#readObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/resourceful/multipart_form_data.rb', line 15

def read
  StringIO.new.tap do |out|
    first = true
    form_data.each do |key, val|
      out << "\r\n" unless first
      out << "--" << boundary
      out << "\r\nContent-Disposition: form-data; name=\"#{key}\""
      if val.kind_of?(FileParamValue)
        out << "; filename=\"#{val.file_name}\""
        out << "\r\nContent-Type: #{val.content_type}"
      end
      out << "\r\n\r\n"
      if val.kind_of?(FileParamValue)
        out << val.content.read
      else
        out << val.to_s
      end
      first = false
    end
    out << "\r\n--#{boundary}--"
  end.string
end