class Paubox::Message

The Message class is for building a Paubox email message using a hash.

Attributes

allow_non_tls[RW]
bcc[RW]
cc[RW]
force_secure_notification[RW]
from[RW]
html_content[RW]
reply_to[RW]
subject[RW]
text_content[RW]
to[RW]

Public Class Methods

new(args) click to toggle source
# File lib/paubox/message.rb, line 10
def initialize(args)
  @from = args[:from]
  @to = args[:to]
  @cc = args[:cc]
  @bcc = args[:bcc]
  @subject = args[:subject]
  @reply_to = args[:reply_to]
  @text_content = args[:text_content]
  @html_content = args[:html_content]
  @packaged_attachments = []
  @attachments = build_attachments(args[:attachments])
  @allow_non_tls = args.fetch(:allow_non_tls, false)
  @force_secure_notification = args.fetch(:force_secure_notification, nil)
end

Public Instance Methods

add_attachment(file_path) click to toggle source
# File lib/paubox/message.rb, line 29
def add_attachment(file_path)
  @packaged_attachments << { filename: file_path.split('/').last,
                             content_type: `file --b --mime-type #{file_path}`.chomp,
                             content: Base64.encode64(File.read(file_path)) }
end
attachments() click to toggle source
# File lib/paubox/message.rb, line 35
def attachments
  @packaged_attachments
end
attachments=(args) click to toggle source
# File lib/paubox/message.rb, line 39
def attachments=(args)
  @attachments = build_attachments(args)
end
send_message_payload() click to toggle source
# File lib/paubox/message.rb, line 25
def send_message_payload
  { data: { message: convert_keys_to_json_version(build_parts) } }.to_json
end

Private Instance Methods

build_attachments(args) click to toggle source
# File lib/paubox/message.rb, line 45
def build_attachments(args)
  return (@packaged_attachments = []) if args.to_a.empty?

  args.each do |a|
    a[:content] = base64_encode_if_needed(a[:content])
    @packaged_attachments << a
  end
  @packaged_attachments
end
build_content() click to toggle source
# File lib/paubox/message.rb, line 55
def build_content
  content = {}
  content[:text_content] = text_content if text_content
  content[:html_content] = base64_encode_if_needed(html_content) if html_content
  content
end
build_force_secure_notification() click to toggle source
# File lib/paubox/message.rb, line 66
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/message.rb, line 62
def build_headers
  %i[from reply_to subject].map { |k| [k, send(k)] }.to_h
end
build_parts() click to toggle source
# File lib/paubox/message.rb, line 82
def build_parts
  msg = {}
  msg[:recipients] = string_or_array_to_array(to)
  msg[:cc] = string_or_array_to_array(cc)
  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[:bcc] = string_or_array_to_array(bcc)
  msg[:headers] = build_headers
  msg[:content] = build_content
  msg[:attachments] = attachments
  msg
end