Class: Appwrite::MultipartBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boundary: nil) ⇒ MultipartBuilder

Returns a new instance of MultipartBuilder.



7
8
9
10
# File 'lib/appwrite/multipart.rb', line 7

def initialize(boundary: nil)
    @boundary = boundary ||= "----RubyMultipartPost#{rand(1000000)}"
    @parts = []
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



5
6
7
# File 'lib/appwrite/multipart.rb', line 5

def boundary
  @boundary
end

Instance Method Details

#add(name, contents, filename: nil, content_type: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/appwrite/multipart.rb', line 12

def add(name, contents, filename: nil, content_type: nil)
    if contents.is_a?(Array)
        contents.each_with_index do |element, index|
            add("#{name}[#{index}]", element)
        end
        return
    end

    part = "--#{@boundary}\r\n"
    part << "Content-Disposition: form-data; name=\"#{name}\""
    part << "; filename=\"#{filename}\"" if filename
    part << "\r\n"
    if content_type
    part << "Content-Type: #{content_type}\r\n"
    elsif filename
    content_type = MIME::Types.type_for(filename).first&.content_type || 'application/octet-stream'
    part << "Content-Type: #{content_type}\r\n"
    end
    part << "\r\n"
    part << contents.to_s
    part << "\r\n"

    @parts << part
end

#bodyObject



37
38
39
# File 'lib/appwrite/multipart.rb', line 37

def body
    @parts.join + "--#{@boundary}--\r\n"
end

#content_typeObject



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

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