class MandrillQueue::Message::Attachments::Attachment

Constants

ACCESSORS

Public Class Methods

new(file = nil, options = {}, &block) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 10
def initialize(file = nil, options = {}, &block)
        @options = options
        @file = file

        if file.is_a?(Hash)
                @options = file
                @file = nil
        end

        load_file if @options[:load_file] && !@file.nil?

        instance_eval(&block) if block_given?
end

Public Instance Methods

content(*args) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 59
def content(*args)
        if args.count > 0
                @content = Base64.encode64(args.first)
        end
        @content
end
content64(*args) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 66
def content64(*args)
        @content = args.first if args.count > 0
        @content
end
file(*args) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 24
def file(*args)
        if args.count > 0
                reset!
                @file = args.first
        end
        @file
end
file_loaded?() click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 39
def file_loaded?
        !@content.nil?
end
load_file() click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 71
def load_file
        return false if file_loaded?

        content(IO.read(@file))
        # set @name and @type if not already set before getting rid of file
        name()
        type()
        @file = nil
        self
end
name(*args) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 43
def name(*args)
        @name = args.first if args.count > 0

        @name ||= (File.basename(@file) unless @file.nil?)
end
reset!() click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 32
def reset!
        @file = nil
        @type = nil
        @content = nil
        @name = nil
end
set!(hash) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 89
def set!(hash)
        ACCESSORS.each do |key|
                instance_variable_set("@#{key}", hash[key])
        end

        self
end
to_hash(options = {}) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 97
def to_hash(options = {})
        hash = {}

        ACCESSORS.each do |key|
                value = send(key)
                hash[key] = value if options[:include_nils] || !value.nil?
        end

        hash
end
type(*args) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 49
def type(*args)
        @type = args.first if args.count > 0

        @type ||= begin
                unless @file.nil?
                        MIME::Types.type_for(@file).first.to_s
                end || "application/octet-stream"
        end
end
validate(errors, options = {}) click to toggle source
# File lib/mandrill_queue/message/attachments.rb, line 82
def validate(errors, options = {})
        options[:as] ||= :attachments
        errors.push([options[:as], "No file or content for attachment '#{name}'."]) if content64.nil? && file.nil?
        errors.push([options[:as], "No attachment name given."]) if name.nil?
        errors.push([options[:as], "File to load (#{file}) does not exist."]) if @file && !file_loaded? && !File.exist?(file)
end