Class: MandrillQueue::Message::Attachments::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_queue/message/attachments.rb

Direct Known Subclasses

Images::Image

Constant Summary collapse

ACCESSORS =
[:file, :name, :content, :type]

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, options = {}, &block) ⇒ Attachment

Returns a new instance of Attachment.



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#content(*args) ⇒ Object



59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
# File 'lib/mandrill_queue/message/attachments.rb', line 66

def content64(*args)
	@content = args.first if args.count > 0
	@content
end

#file(*args) ⇒ Object



24
25
26
27
28
29
30
# 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?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/mandrill_queue/message/attachments.rb', line 39

def file_loaded?
	!@content.nil?
end

#load_fileObject



71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



43
44
45
46
47
# 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!Object



32
33
34
35
36
37
# File 'lib/mandrill_queue/message/attachments.rb', line 32

def reset!
	@file = nil
	@type = nil
	@content = nil
	@name = nil
end

#set!(hash) ⇒ Object



89
90
91
92
93
94
95
# 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 = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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 = {}) ⇒ Object



82
83
84
85
86
87
# 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