Class: MandrillQueue::Message::Internal

Inherits:
Object
  • Object
show all
Includes:
Attachments::DSL, MandrillQueue::Message::Images::DSL, MergeVars::DSL, RecipientMetadata::DSL, Recipients::DSL
Defined in:
lib/mandrill_queue/message.rb

Constant Summary collapse

ACCESSORS =
[
	:html, :text, :from_email, :from_name, :subject,
	:headers, :important, :track_opens, :track_clicks, :auto_text, :auto_html,
	:inline_css, :url_strip_qs, :preserve_recipients, :view_content_link,
	:bcc_address, :tracking_domain, :signing_domain, :return_path_domain,
	:merge, :tags, :subaccount, :google_analytics_domain,
	:google_analytics_campaign
]
EXTERNAL_ACCESSORS =
[
	:global_merge_vars, :merge_vars, :recipient_metadata,
	:metadata, :attachments, :images
]

Instance Method Summary collapse

Methods included from MandrillQueue::Message::Images::DSL

#images

Methods included from Attachments::DSL

#attachments

Methods included from RecipientMetadata::DSL

#recipient_metadata

Methods included from MergeVars::DSL

#merge_vars

Methods included from Recipients::DSL

include_as, included, #recipients

Constructor Details

#initialize(values = nil) ⇒ Internal

Returns a new instance of Internal.



36
37
38
# File 'lib/mandrill_queue/message.rb', line 36

def initialize(values = nil)
     set!(values) unless values.nil?
end

Instance Method Details

#content_message?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/mandrill_queue/message.rb', line 79

def content_message?
	!html.blank? || !text.blank?
end

#load_attachments!Object



95
96
97
98
99
# File 'lib/mandrill_queue/message.rb', line 95

def load_attachments!
	@_attachments.load_all unless @_attachments.nil?
	@_images.load_all unless @_images.nil?
	self
end

#nillify!Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/mandrill_queue/message.rb', line 54

def nillify!
	transform_accessors! { |k| nil }

	EXTERNAL_ACCESSORS.each do |key|
		instance_variable_set("@_#{key}", nil)
	end

	@_recipients = nil
	self
end

#set!(values) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mandrill_queue/message.rb', line 65

def set!(values)
	nillify!
     values.to_hash.symbolize_keys!
	transform_accessors! { |k| values[k] }

	EXTERNAL_ACCESSORS.each do |key|
		send(key).set!(values[key]) unless values[key].nil?
	end

	[:to, :cc, :bcc].each do |key|
		recipients.set!(values[key], key) if values[key]
	end
end

#to_hash(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/mandrill_queue/message.rb', line 105

def to_hash(options = {})
	hash = {}
	hash[:to] = recipients.to_a(options) if @_recipients

	ACCESSORS.each do |key|
		value = instance_variable_get("@#{key}")
		next if value.nil? && !options[:include_nils]
		hash[key] = value.respond_to?(:to_hash) ? value.to_hash : value
	end

	EXTERNAL_ACCESSORS.each do |key|
		sym = "@_#{key}".to_sym
		var = instance_variable_get(sym)
		if options[:include_nils] || !var.nil?
			if var.is_a?(Variables::Internal)
				hash[key] = var.to_key_value_array(options)
			else
				hash[key] = (var.to_hash(options) rescue var.to_a(options) rescue var)
			end
		end
	end

	hash
end

#to_json(options = {}) ⇒ Object



101
102
103
# File 'lib/mandrill_queue/message.rb', line 101

def to_json(options = {})
	to_hash(options).to_json
end

#validate(errors) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mandrill_queue/message.rb', line 83

def validate(errors)
	errors.push([:message, "Please specify at least one recipient."]) if to.empty?

	EXTERNAL_ACCESSORS.each do |key|
		sym = "@_#{key}"
		val = instance_variable_get(sym)
		val.validate(errors) unless val.nil? || !val.respond_to?(:validate)
	end

	recipients.validate(errors)
end