class Paubox::MailToMessage

The MailToMessage class takes a Ruby Mail object and attempts to parse it into a Hash formatted for the JSON payload of HTTP api request.

Attributes

mail[R]

Public Class Methods

new(mail, args = {}) click to toggle source
# File lib/paubox/mail_to_message.rb, line 11
def initialize(mail, args = {})
  @mail = mail
  @allow_non_tls = args.fetch(:allow_non_tls, false)
  @force_secure_notification = args.fetch(:force_secure_notification, nil)
end

Public Instance Methods

send_message_payload() click to toggle source
# File lib/paubox/mail_to_message.rb, line 17
def send_message_payload
  { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
end

Private Instance Methods

build_attachments() click to toggle source
# File lib/paubox/mail_to_message.rb, line 23
def build_attachments
  attachments = mail.attachments
  return [] if attachments.empty?

  packaged_attachments = []
  attachments.each do |attch|
    packaged_attachments << { content: convert_binary_to_base64(attch.body.decoded),
                              file_name: attch.filename,
                              content_type: attch.mime_type }
  end
  packaged_attachments
end
build_content() click to toggle source
# File lib/paubox/mail_to_message.rb, line 36
def build_content
  content = {}
  if mail.multipart?
    html_content = mail.html_part.body.to_s if mail.html_part
    text_content = mail.text_part.body.to_s if mail.text_part
    content[:html_content] = base64_encode_if_needed(html_content) unless html_content.nil?
    content[:text_content] = text_content unless text_content.nil?
  elsif mail.content_type.to_s.include? 'text/html'
    content[:html_content] = base64_encode_if_needed(mail.body.to_s)
  else
    content[:text_content] = mail.body.to_s
  end
  content
end
build_force_secure_notification() click to toggle source
# File lib/paubox/mail_to_message.rb, line 55
def build_force_secure_notification
  if @force_secure_notification.instance_of?(String)
    unless @force_secure_notification.to_s.empty? # if force_secure_notification is not nil or empty
      force_secure_notification_val = @force_secure_notification.strip.downcase
      if force_secure_notification_val == 'true'
        return true
      elsif force_secure_notification_val == 'false'
        return false
      else
        return nil
      end
    end
  end
  nil
end
build_headers() click to toggle source
# File lib/paubox/mail_to_message.rb, line 51
def build_headers
  %i[from reply_to subject].map { |k| [k, mail[k].to_s] }.to_h
end
build_parts() click to toggle source
# File lib/paubox/mail_to_message.rb, line 71
def build_parts
  msg = {}
  msg[:recipients] = string_or_array_to_array(mail.to)
  msg[:cc] = string_or_array_to_array(mail.cc)
  msg[:bcc] = string_or_array_to_array(mail.bcc)
  msg[:allow_non_tls] = @allow_non_tls
  @force_secure_notification = build_force_secure_notification
  unless @force_secure_notification.nil?
    msg[:force_secure_notification] = @force_secure_notification
  end
  msg[:headers] = build_headers
  msg[:content] = build_content
  msg[:attachments] = build_attachments
  msg
end
convert_binary_to_base64(f) click to toggle source
# File lib/paubox/mail_to_message.rb, line 87
def convert_binary_to_base64(f)
  file = Tempfile.new(encoding: 'ascii-8bit')
  file.write(f)
  file.rewind
  Base64.encode64(file.read)
end