Class: SendGrid::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/helpers/mail/mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_email = nil, subj = nil, to_email = nil, cont = nil) ⇒ Mail

Returns a new instance of Mail.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sendgrid/helpers/mail/mail.rb', line 11

def initialize(from_email = nil, subj = nil, to_email = nil, cont = nil)
  @from = nil
  @subject = nil
  @personalizations = []
  @contents = []
  @attachments = []
  @template_id = nil
  @sections = {}
  @headers = {}
  @categories = []
  @custom_args = {}
  @send_at = nil
  @batch_id = nil
  @asm = nil
  @ip_pool_name = nil
  @mail_settings = nil
  @tracking_settings = nil
  @reply_to = nil

  return if from_email.nil? && subj.nil? && to_email.nil? && cont.nil?

  self.from = from_email
  self.subject = subj
  personalization = Personalization.new
  personalization.add_to(to_email)
  add_personalization(personalization)
  add_content(cont)
end

Instance Attribute Details

#asmObject


82
83
84
# File 'lib/sendgrid/helpers/mail/mail.rb', line 82

def asm
  @asm.nil? ? nil : @asm.to_json
end

#attachmentsObject (readonly)

Returns the value of attribute attachments


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def attachments
  @attachments
end

#batch_idObject

Returns the value of attribute batch_id


7
8
9
# File 'lib/sendgrid/helpers/mail/mail.rb', line 7

def batch_id
  @batch_id
end

#categoriesObject (readonly)

Returns the value of attribute categories


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def categories
  @categories
end

#contentsObject (readonly)

Returns the value of attribute contents


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def contents
  @contents
end

#custom_argsObject (readonly)

Returns the value of attribute custom_args


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def custom_args
  @custom_args
end

#fromObject


40
41
42
# File 'lib/sendgrid/helpers/mail/mail.rb', line 40

def from
  @from.nil? ? nil : @from.to_json
end

#headersObject (readonly)

Returns the value of attribute headers


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def headers
  @headers
end

#ip_pool_nameObject

Returns the value of attribute ip_pool_name


7
8
9
# File 'lib/sendgrid/helpers/mail/mail.rb', line 7

def ip_pool_name
  @ip_pool_name
end

#mail_settingsObject


86
87
88
# File 'lib/sendgrid/helpers/mail/mail.rb', line 86

def mail_settings
  @mail_settings.nil? ? nil : @mail_settings.to_json
end

#personalizationsObject (readonly)

Returns the value of attribute personalizations


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def personalizations
  @personalizations
end

#reply_toObject


94
95
96
# File 'lib/sendgrid/helpers/mail/mail.rb', line 94

def reply_to
  @reply_to.nil? ? nil : @reply_to.to_json
end

#sectionsObject (readonly)

Returns the value of attribute sections


8
9
10
# File 'lib/sendgrid/helpers/mail/mail.rb', line 8

def sections
  @sections
end

#send_atObject

Returns the value of attribute send_at


7
8
9
# File 'lib/sendgrid/helpers/mail/mail.rb', line 7

def send_at
  @send_at
end

#subjectObject

Returns the value of attribute subject


7
8
9
# File 'lib/sendgrid/helpers/mail/mail.rb', line 7

def subject
  @subject
end

#template_idObject

Returns the value of attribute template_id


7
8
9
# File 'lib/sendgrid/helpers/mail/mail.rb', line 7

def template_id
  @template_id
end

#tracking_settingsObject


90
91
92
# File 'lib/sendgrid/helpers/mail/mail.rb', line 90

def tracking_settings
  @tracking_settings.nil? ? nil : @tracking_settings.to_json
end

Instance Method Details

#add_attachment(attachment) ⇒ Object


59
60
61
# File 'lib/sendgrid/helpers/mail/mail.rb', line 59

def add_attachment(attachment)
  @attachments << attachment.to_json
end

#add_category(category) ⇒ Object


63
64
65
# File 'lib/sendgrid/helpers/mail/mail.rb', line 63

def add_category(category)
  @categories << category.name
end

#add_content(content) ⇒ Object


48
49
50
# File 'lib/sendgrid/helpers/mail/mail.rb', line 48

def add_content(content)
  @contents << content.to_json
end

#add_custom_arg(custom_arg) ⇒ Object


77
78
79
80
# File 'lib/sendgrid/helpers/mail/mail.rb', line 77

def add_custom_arg(custom_arg)
  custom_arg = custom_arg.to_json
  @custom_args = @custom_args.merge(custom_arg['custom_arg'])
end

#add_header(header) ⇒ Object


72
73
74
75
# File 'lib/sendgrid/helpers/mail/mail.rb', line 72

def add_header(header)
  header = header.to_json
  @headers = @headers.merge(header['header'])
end

#add_personalization(personalization) ⇒ Object


44
45
46
# File 'lib/sendgrid/helpers/mail/mail.rb', line 44

def add_personalization(personalization)
  @personalizations << personalization.to_json
end

#add_section(section) ⇒ Object


67
68
69
70
# File 'lib/sendgrid/helpers/mail/mail.rb', line 67

def add_section(section)
  section = section.to_json
  @sections = @sections.merge(section['section'])
end

#check_for_secrets(patterns) ⇒ Object


52
53
54
55
56
57
# File 'lib/sendgrid/helpers/mail/mail.rb', line 52

def check_for_secrets(patterns)
  contents = @contents.map { |content| content['value'] }.join(' ')
  patterns.each do |pattern|
    raise SecurityError, 'Content contains sensitive information.' if contents.match(pattern)
  end
end

#to_jsonObject


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sendgrid/helpers/mail/mail.rb', line 98

def to_json(*)
  {
    'from' => from,
    'subject' => subject,
    'personalizations' => personalizations,
    'content' => contents,
    'attachments' => attachments,
    'template_id' => template_id,
    'sections' => sections,
    'headers' => headers,
    'categories' => categories,
    'custom_args' => custom_args,
    'send_at' => send_at,
    'batch_id' => batch_id,
    'asm' => asm,
    'ip_pool_name' => ip_pool_name,
    'mail_settings' => mail_settings,
    'tracking_settings' => tracking_settings,
    'reply_to' => reply_to
  }.delete_if { |_, value| value.to_s.strip == '' || value == [] || value == {} }
end