lib/postmark/message_extensions/mail.rb in postmark-1.14.0 vs lib/postmark/message_extensions/mail.rb in postmark-1.15.0

- old
+ new

@@ -59,10 +59,54 @@ "attachments API provided by Mail library.") ::Postmark::MessageHelper.attachments_to_postmark(@_attachments) end + def template_alias(val = nil) + return self[:postmark_template_alias] && self[:postmark_template_alias].to_s if val.nil? + self[:postmark_template_alias] = val + end + + attr_writer :template_model + def template_model(model = nil) + return @template_model if model.nil? + @template_model = model + end + + def templated? + !!template_alias + end + + def prerender + raise ::Postmark::Error, 'Cannot prerender a message without an associated template alias' unless templated? + + unless delivery_method.is_a?(::Mail::Postmark) + raise ::Postmark::MailAdapterError, "Cannot render templates via #{delivery_method.class} adapter." + end + + client = delivery_method.api_client + template = client.get_template(template_alias) + response = client.validate_template(template.merge(:test_render_model => template_model || {})) + + raise ::Postmark::InvalidTemplateError, response unless response[:all_content_is_valid] + + self.body = nil + + subject response[:subject][:rendered_content] + + text_part do + body response[:text_body][:rendered_content] + end + + html_part do + content_type 'text/html; charset=UTF-8' + body response[:html_body][:rendered_content] + end + + self + end + def text? if defined?(super) super else has_content_type? ? !!(main_type =~ /^text$/i) : false @@ -137,9 +181,10 @@ date content-type cc bcc subject tag attachment to track-opens track-links + postmark-template-alias ] end end end