Class: Telegem::API::MultipartForm

Inherits:
Object
  • Object
show all
Defined in:
lib/api/client.rb

Constant Summary collapse

CRLF =
"\r\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMultipartForm

Returns a new instance of MultipartForm.



15
16
17
18
19
# File 'lib/api/client.rb', line 15

def initialize
  @boundary = "----telegem#{SecureRandom.hex(16)}"
  @content_type = "multipart/form-data; boundary=#{@boundary}"
  @parts = []
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



13
14
15
# File 'lib/api/client.rb', line 13

def content_type
  @content_type
end

Instance Method Details

#add(name, value) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/api/client.rb', line 21

def add(name, value)
  if file?(value)
    append_file(name, value)
  else
    append_field(name, value.to_s)
  end
end

#bodyObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api/client.rb', line 29

def body
  String.new.tap do |buffer|
    @parts.each do |part|
      buffer << "--#{@boundary}#{CRLF}"
      buffer << part
      buffer << CRLF
    end

    buffer << "--#{@boundary}--#{CRLF}"
  end
end