Class: Kooaba::MultipartMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/kooaba/multipart_message.rb

Instance Method Summary collapse

Constructor Details

#initializeMultipartMessage

Returns a new instance of MultipartMessage.



14
15
16
17
# File 'lib/kooaba/multipart_message.rb', line 14

def initialize
  @file_parts = []
  @text_parts = []
end

Instance Method Details

#add_file_part(name, file, type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kooaba/multipart_message.rb', line 19

def add_file_part(name, file, type)
  case file
  when String
    io = File.open(file, "rb")
  else
    io = file
  end

  unless io.respond_to?(:content_type)
    io.extend(TypedFile)
    content_type = (type == "image/jpeg") ? "image/jpg" : type
    io.content_type = content_type
  end

  @file_parts << [name, io]
end

#add_text_part(name, text) ⇒ Object



36
37
38
# File 'lib/kooaba/multipart_message.rb', line 36

def add_text_part(name, text)
  @text_parts << [name, text]
end

#bodyObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/kooaba/multipart_message.rb', line 44

def body
  boundary_marker = "--#{boundary_token}\r\n".force_encoding("UTF-8")
  body = @text_parts.map { |param|
    (name, value) = param
    boundary_marker + text_to_multipart(name, value)
  }.map{|p| p.force_encoding("UTF-8")}.join('') + @file_parts.map { |param|
    (name, value) = param
    boundary_marker + data_to_multipart(name, value)
  }.map{|p| p.force_encoding("UTF-8")}.join('') + "--#{boundary_token}--\r\n".force_encoding("UTF-8")
end

#content_typeObject



40
41
42
# File 'lib/kooaba/multipart_message.rb', line 40

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