Class: SendGridMailer::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/send_grid_mailer/definition.rb

Constant Summary collapse

METHODS =
[
  :substitute,
  :dynamic_template_data,
  :set_template_id,
  :set_sender,
  :set_recipients,
  :set_subject,
  :set_content,
  :add_attachment,
  :add_header,
  :add_category
]

Instance Method Summary collapse

Instance Method Details

#add_attachment(file, name, type, disposition = "inline", content_id = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/send_grid_mailer/definition.rb', line 66

def add_attachment(file, name, type, disposition = "inline", content_id = nil)
  attachment = SendGrid::Attachment.new
  attachment.content = Base64.strict_encode64(file)
  attachment.type = type
  attachment.filename = name
  attachment.disposition = disposition
  attachment.content_id = content_id
  mail.add_attachment(attachment)
end

#add_category(value) ⇒ Object



82
83
84
85
86
# File 'lib/send_grid_mailer/definition.rb', line 82

def add_category(value)
  return unless value

  mail.add_category(SendGrid::Category.new(name: value))
end

#add_header(key, value) ⇒ Object



76
77
78
79
80
# File 'lib/send_grid_mailer/definition.rb', line 76

def add_header(key, value)
  return if !key || !value

  personalization.add_header(SendGrid::Header.new(key: key, value: value))
end

#clean_recipients(mode) ⇒ Object



97
98
99
# File 'lib/send_grid_mailer/definition.rb', line 97

def clean_recipients(mode)
  personalization.instance_variable_set("@#{mode}s", [])
end

#content?Boolean

Returns:

  • (Boolean)


107
# File 'lib/send_grid_mailer/definition.rb', line 107

def content?; mail.contents.present? end

#dynamic_template_data(object) ⇒ Object



22
23
24
# File 'lib/send_grid_mailer/definition.rb', line 22

def dynamic_template_data(object)
  personalization.add_dynamic_template_data(object)
end

#mailObject



93
94
95
# File 'lib/send_grid_mailer/definition.rb', line 93

def mail
  @mail ||= SendGrid::Mail.new
end

#personalizationObject



101
102
103
# File 'lib/send_grid_mailer/definition.rb', line 101

def personalization
  @personalization ||= SendGrid::Personalization.new
end

#personalization?Boolean

Returns:

  • (Boolean)


105
# File 'lib/send_grid_mailer/definition.rb', line 105

def personalization?; !personalization.to_json.empty? end

#sender?Boolean

Returns:

  • (Boolean)


109
# File 'lib/send_grid_mailer/definition.rb', line 109

def sender?; mail.from.present? end

#set_content(value, type = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/send_grid_mailer/definition.rb', line 59

def set_content(value, type = nil)
  return unless value

  type ||= "text/plain"
  mail.add_content(SendGrid::Content.new(type: type, value: value))
end

#set_recipients(mode, *emails) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/send_grid_mailer/definition.rb', line 45

def set_recipients(mode, *emails)
  emails.flatten.each do |email|
    next unless email

    personalization.send("add_#{mode}", SendGrid::Email.new(email: email))
  end
end

#set_sender(email) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/send_grid_mailer/definition.rb', line 32

def set_sender(email)
  return unless email

  matched_format = email.match(/<(.+)>/)
  if matched_format
    address = matched_format[1]
    name = email.match(/\"?([^<^\"]*)\"?\s?/)[1].strip
    mail.from = SendGrid::Email.new(email: address, name: name)
  else
    mail.from = SendGrid::Email.new(email: email)
  end
end

#set_subject(value) ⇒ Object



53
54
55
56
57
# File 'lib/send_grid_mailer/definition.rb', line 53

def set_subject(value)
  return unless value

  personalization.subject = value
end

#set_template_id(value) ⇒ Object



26
27
28
29
30
# File 'lib/send_grid_mailer/definition.rb', line 26

def set_template_id(value)
  return unless value

  mail.template_id = value
end

#subject?Boolean

Returns:

  • (Boolean)


111
# File 'lib/send_grid_mailer/definition.rb', line 111

def subject?; personalization.subject.present? end

#substitute(key, value, default = "") ⇒ Object



16
17
18
19
20
# File 'lib/send_grid_mailer/definition.rb', line 16

def substitute(key, value, default = "")
  personalization.add_substitution(
    SendGrid::Substitution.new(key: key, value: value.to_s || default)
  )
end

#template_id?Boolean

Returns:

  • (Boolean)


113
# File 'lib/send_grid_mailer/definition.rb', line 113

def template_id?; mail.template_id.present? end

#to_jsonObject



88
89
90
91
# File 'lib/send_grid_mailer/definition.rb', line 88

def to_json
  mail.add_personalization(personalization) if personalization?
  mail.to_json
end