Sha256: f769e515e2e734cf98ceef704b221067bece92df4df56503001cbd0d08270987

Contents?: true

Size: 1.48 KB

Versions: 31

Compression:

Stored size: 1.48 KB

Contents

module Postmark
  module MessageHelper

    extend self

    def to_postmark(message = {})
      message = message.dup

      %w(to reply_to cc bcc).each do |field|
        message[field.to_sym] = Array[*message[field.to_sym]].join(", ")
      end

      if message[:headers]
        message[:headers] = headers_to_postmark(message[:headers])
      end

      if message[:attachments]
        message[:attachments] = attachments_to_postmark(message[:attachments])
      end

      if message[:track_links]
        message[:track_links] = ::Postmark::Inflector.to_postmark(message[:track_links])
      end

      HashHelper.to_postmark(message)
    end

    def headers_to_postmark(headers)
      wrap_in_array(headers).map do |item|
        HashHelper.to_postmark(item)
      end
    end

    def attachments_to_postmark(attachments)
      wrap_in_array(attachments).map do |item|
        if item.is_a?(Hash)
          HashHelper.to_postmark(item)
        elsif item.is_a?(File)
          {
            "Name"        => item.path.split("/")[-1],
            "Content"     => encode_in_base64(IO.read(item.path)),
            "ContentType" => "application/octet-stream"
          }
        end
      end
    end

    def encode_in_base64(data)
      [data].pack('m')
    end

    protected

    # From ActiveSupport (Array#wrap)
    def wrap_in_array(object)
      if object.nil?
        []
      elsif object.respond_to?(:to_ary)
        object.to_ary || [object]
      else
        [object]
      end
    end

  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
postmark-1.25.1 lib/postmark/helpers/message_helper.rb
postmark-1.25.0 lib/postmark/helpers/message_helper.rb
postmark-1.24.1 lib/postmark/helpers/message_helper.rb
postmark-1.24.0 lib/postmark/helpers/message_helper.rb
postmark-1.23.0 lib/postmark/helpers/message_helper.rb
postmark-1.22.3 lib/postmark/helpers/message_helper.rb
postmark-1.22.2 lib/postmark/helpers/message_helper.rb
postmark-1.22.1 lib/postmark/helpers/message_helper.rb
postmark-1.22.0 lib/postmark/helpers/message_helper.rb
postmark-1.21.8 lib/postmark/helpers/message_helper.rb
postmark-1.21.7 lib/postmark/helpers/message_helper.rb
postmark-1.21.6 lib/postmark/helpers/message_helper.rb
postmark-1.21.5 lib/postmark/helpers/message_helper.rb
postmark-1.21.4 lib/postmark/helpers/message_helper.rb
postmark-1.21.3 lib/postmark/helpers/message_helper.rb
postmark-1.21.2 lib/postmark/helpers/message_helper.rb
postmark-1.21.1 lib/postmark/helpers/message_helper.rb
postmark-1.21.0 lib/postmark/helpers/message_helper.rb
postmark-1.20.0 lib/postmark/helpers/message_helper.rb
postmark-1.19.2 lib/postmark/helpers/message_helper.rb