Class: Kooaba::MultipartMessage
- Inherits:
-
Object
- Object
- Kooaba::MultipartMessage
- Defined in:
- lib/kooaba/multipart_message.rb
Instance Method Summary collapse
- #add_file_part(name, file, type) ⇒ Object
- #add_text_part(name, text) ⇒ Object
- #body ⇒ Object
- #content_type ⇒ Object
-
#initialize ⇒ MultipartMessage
constructor
A new instance of MultipartMessage.
Constructor Details
#initialize ⇒ MultipartMessage
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 = open(file) 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 |
#body ⇒ Object
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" body = @text_parts.map { |param| (name, value) = param boundary_marker + text_to_multipart(name, value) }.join('') + @file_parts.map { |param| (name, value) = param boundary_marker + data_to_multipart(name, value) }.join('') + "--#{boundary_token}--\r\n" end |
#content_type ⇒ Object
40 41 42 |
# File 'lib/kooaba/multipart_message.rb', line 40 def content_type "multipart/form-data; boundary=#{boundary_token}" end |