Class: Mack::Notifier::Adapters::Tmail

Inherits:
Base show all
Defined in:
lib/mack-notifier/adapters/tmail.rb

Overview

Converts a Mack::Notifier object into a TMail object.

Instance Attribute Summary

Attributes inherited from Base

#mack_notifier

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Mack::Notifier::Adapters::Base

Instance Method Details

#convertObject

Converts the Mack::Notifier object to a TMail object.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mack-notifier/adapters/tmail.rb', line 23

def convert
  @tmail = TMail::Mail.new 
  @tmail.to =           mack_notifier.to
  @tmail.cc =           mack_notifier.cc
  @tmail.bcc =          mack_notifier.bcc
  @tmail.reply_to =     mack_notifier.reply_to
  @tmail.from =         mack_notifier.from
  @tmail.subject =      mack_notifier.subject
  @tmail.date =         mack_notifier.date_sent
  @tmail.mime_version = mack_notifier.mime_version

  # set text and html bodies
  main_body = TMail::Mail.new
  unless mack_notifier.body(:plain).blank?
    text = TMail::Mail.new
    text.content_type = "text/plain"
    text.body = mack_notifier.body(:plain)
    main_body.parts << text
  end
  unless mack_notifier.body(:html).blank?
    html = TMail::Mail.new
    html.content_type = "text/html"
    html.body = mack_notifier.body(:html)
    main_body.parts << html
  end

  unless main_body.parts.empty?
    main_body.content_type = "multipart/alternative"
    if mack_notifier.attachments.any? # there's an attachment
      @tmail.parts << main_body
    else
      if main_body.parts.size == 1
        @tmail.body = main_body.parts.first.body
      else
        @tmail.parts << main_body
      end
    end
  end

  # set attachments, if any.
  mack_notifier.attachments.each do |at|
    attachment = TMail::Mail.new
    attachment.body = Base64.encode64(at.body)
    attachment.transfer_encoding = "Base64"
    attachment.content_type = "application/octet-stream"
    attachment['Content-Disposition'] = "attachment; filename=#{at.file_name}"
    @tmail.parts << attachment
  end
  
  @tmail.content_type = mack_notifier.content_type
end

#deliverableObject

Returns the ready to be delivered encoded String



18
19
20
# File 'lib/mack-notifier/adapters/tmail.rb', line 18

def deliverable
  transformed.encoded
end

#transformedObject

Returns the underlying TMail object. Raises Mack::Errors::UnconvertedNotifier if the convert method hasn’t been called first.



12
13
14
15
# File 'lib/mack-notifier/adapters/tmail.rb', line 12

def transformed
  raise Mack::Errors::UnconvertedNotifier.new if @tmail.nil?
  @tmail
end