Class: SendgridRuby::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid_ruby/email.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEmail

Returns a new instance of Email.



9
10
11
12
13
14
15
# File 'lib/sendgrid_ruby/email.rb', line 9

def initialize()
  @smtpapi = Smtpapi::Header.new
  @cc_list = []
  @bcc_list = []
  @attachments = []
  @headers = Hash.new
end

Instance Attribute Details

#smtpapiObject

Returns the value of attribute smtpapi.



154
155
156
# File 'lib/sendgrid_ruby/email.rb', line 154

def smtpapi
  @smtpapi
end

Instance Method Details

#add_attachment(file, custom_filename = nil) ⇒ Object



172
173
174
175
# File 'lib/sendgrid_ruby/email.rb', line 172

def add_attachment(file, custom_filename=nil)
  @attachments.push(get_attachment_info(file, custom_filename))
  self
end

#add_category(category) ⇒ Object



211
212
213
214
# File 'lib/sendgrid_ruby/email.rb', line 211

def add_category(category)
  @smtpapi.add_category(category)
  self
end

#add_filter(filter_name, parameter_name, parameter_value) ⇒ Object



256
257
258
# File 'lib/sendgrid_ruby/email.rb', line 256

def add_filter(filter_name, parameter_name, parameter_value)
  @smtpapi.add_filter(filter_name, parameter_name, parameter_value)
end

#add_header(key, value) ⇒ Object



285
286
287
288
# File 'lib/sendgrid_ruby/email.rb', line 285

def add_header(key, value)
  @headers[key] = value
  self
end

#add_section(from_value, to_value) ⇒ Object



236
237
238
239
# File 'lib/sendgrid_ruby/email.rb', line 236

def add_section(from_value, to_value)
  @smtpapi.add_section(from_value, to_value)
  self
end

#add_substitution(from_value, to_values) ⇒ Object



226
227
228
229
# File 'lib/sendgrid_ruby/email.rb', line 226

def add_substitution(from_value, to_values)
  @smtpapi.add_substitution(from_value, to_values)
  self
end

#add_to(email, name = nil) ⇒ Object



28
29
30
31
# File 'lib/sendgrid_ruby/email.rb', line 28

def add_to(email, name=nil)
  @smtpapi.add_to(email, name)
  self
end

#add_unique_arg(key, value) ⇒ Object



246
247
248
249
# File 'lib/sendgrid_ruby/email.rb', line 246

def add_unique_arg(key, value)
  @smtpapi.add_unique_arg(key, value)
  self
end

#get_attachment_info(file, custom_filename = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sendgrid_ruby/email.rb', line 186

def get_attachment_info(file, custom_filename=nil)
  dir, basename = File::split(file)
  extname = File::extname(file)
  filename = File::basename(file, extname)
  info = {
    'dirname' => dir,
    'basename' => basename,
    'extension' => extname,
    'filename' => filename
  }
  info['file'] = file
  info['custom_filename'] = custom_filename if !custom_filename.nil?
  info
end

#get_attachmentsObject



177
178
179
# File 'lib/sendgrid_ruby/email.rb', line 177

def get_attachments()
  @attachments
end

#get_from(as_array = false) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/sendgrid_ruby/email.rb', line 52

def get_from(as_array = false)
  if as_array && name = @from_name
    {@from => name}
  else
    @from
  end
end

#get_from_nameObject



74
75
76
# File 'lib/sendgrid_ruby/email.rb', line 74

def get_from_name
  @from_name
end

#get_headersObject



272
273
274
# File 'lib/sendgrid_ruby/email.rb', line 272

def get_headers()
  @headers
end

#get_headers_jsonObject



276
277
278
# File 'lib/sendgrid_ruby/email.rb', line 276

def get_headers_json()
  JSON.generate(@headers)
end

#get_htmlObject



150
151
152
# File 'lib/sendgrid_ruby/email.rb', line 150

def get_html
  @html
end

#get_reply_toObject



65
66
67
# File 'lib/sendgrid_ruby/email.rb', line 65

def get_reply_to
  @reply_to
end

#get_subjectObject



131
132
133
# File 'lib/sendgrid_ruby/email.rb', line 131

def get_subject
  @subject
end

#get_textObject



140
141
142
# File 'lib/sendgrid_ruby/email.rb', line 140

def get_text
  @text
end

#get_tosObject



38
39
40
# File 'lib/sendgrid_ruby/email.rb', line 38

def get_tos
  @smtpapi.to
end

#remove_attachment(file) ⇒ Object



181
182
183
184
# File 'lib/sendgrid_ruby/email.rb', line 181

def remove_attachment(file)
  @attachments = remove_from_list(@attachments, file, "file")
  self
end

#remove_category(category) ⇒ Object



216
217
218
219
# File 'lib/sendgrid_ruby/email.rb', line 216

def remove_category(category)
  @smtpapi.remove_category(category)
  self
end

#remove_from_list(list, item, key_field = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/sendgrid_ruby/email.rb', line 17

def remove_from_list(list, item, key_field = nil)
  list.each do |value|
    if key_field == nil
      list.delete(value) if (value == item)
    else
      list.delete(value) if (value[key_field] == item)
    end
  end
  list
end

#remove_header(key) ⇒ Object



290
291
292
293
# File 'lib/sendgrid_ruby/email.rb', line 290

def remove_header(key)
  @headers = remove_from_list(@headers, key)
  self
end

#remove_to(email) ⇒ Object



42
43
44
45
# File 'lib/sendgrid_ruby/email.rb', line 42

def remove_to(email)
  remove_from_list(@smtpapi.to, email)
  self
end

#set_asm_group(group_id) ⇒ Object



268
269
270
# File 'lib/sendgrid_ruby/email.rb', line 268

def set_asm_group(group_id)
  @smtpapi.set_asm_group(group_id)
end

#set_attachment(file, custom_filename = nil) ⇒ Object



167
168
169
170
# File 'lib/sendgrid_ruby/email.rb', line 167

def set_attachment(file, custom_filename=nil)
  @attachments = [get_attachment_info(file, custom_filename)]
  self
end

#set_attachments(files) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/sendgrid_ruby/email.rb', line 156

def set_attachments(files)
  files.each do |filename, file|
    if file.is_a?(String)
      add_attachment(file, filename)
    else
      add_attachment(filename)
    end
  end
  self
end

#set_categories(categories) ⇒ Object



201
202
203
204
# File 'lib/sendgrid_ruby/email.rb', line 201

def set_categories(categories)
  @smtpapi.set_categories(categories)
  self
end

#set_category(category) ⇒ Object



206
207
208
209
# File 'lib/sendgrid_ruby/email.rb', line 206

def set_category(category)
  @smtpapi.set_category(category)
  self
end

#set_filters(filter_settings) ⇒ Object



251
252
253
254
# File 'lib/sendgrid_ruby/email.rb', line 251

def set_filters(filter_settings)
  @smtpapi.set_filters(filter_settings)
  self
end

#set_from(email) ⇒ Object



47
48
49
50
# File 'lib/sendgrid_ruby/email.rb', line 47

def set_from(email)
  @from = email
  self
end

#set_from_name(from_name) ⇒ Object



69
70
71
72
# File 'lib/sendgrid_ruby/email.rb', line 69

def set_from_name(from_name)
  @from_name = from_name
  self
end

#set_headers(key_value_pairs) ⇒ Object



280
281
282
283
# File 'lib/sendgrid_ruby/email.rb', line 280

def set_headers(key_value_pairs)
  @headers = key_value_pairs
  self
end

#set_html(html) ⇒ Object



145
146
147
148
# File 'lib/sendgrid_ruby/email.rb', line 145

def set_html(html)
  @html = html
  self
end

#set_reply_to(reply_to) ⇒ Object



60
61
62
63
# File 'lib/sendgrid_ruby/email.rb', line 60

def set_reply_to(reply_to)
  @reply_to = reply_to
  self
end

#set_sections(key_value_pairs) ⇒ Object



231
232
233
234
# File 'lib/sendgrid_ruby/email.rb', line 231

def set_sections(key_value_pairs)
  @smtpapi.set_sections(key_value_pairs)
  self
end

#set_send_at(send_at) ⇒ Object



260
261
262
# File 'lib/sendgrid_ruby/email.rb', line 260

def set_send_at(send_at)
  @smtpapi.set_send_at(send_at)
end

#set_send_each_at(send_each_at) ⇒ Object



264
265
266
# File 'lib/sendgrid_ruby/email.rb', line 264

def set_send_each_at(send_each_at)
  @smtpapi.set_send_each_at(send_each_at)
end

#set_subject(subject) ⇒ Object

def get_bccs

@bcc_list

end



126
127
128
129
# File 'lib/sendgrid_ruby/email.rb', line 126

def set_subject(subject)
  @subject = subject
  self
end

#set_substitutions(key_value_pairs) ⇒ Object



221
222
223
224
# File 'lib/sendgrid_ruby/email.rb', line 221

def set_substitutions(key_value_pairs)
  @smtpapi.set_substitutions(key_value_pairs)
  self
end

#set_text(text) ⇒ Object



135
136
137
138
# File 'lib/sendgrid_ruby/email.rb', line 135

def set_text(text)
  @text = text
  self
end

#set_tos(emails) ⇒ Object



33
34
35
36
# File 'lib/sendgrid_ruby/email.rb', line 33

def set_tos(emails)
  @smtpapi.set_tos(emails)
  self
end

#set_unique_args(key_value_pairs) ⇒ Object



241
242
243
244
# File 'lib/sendgrid_ruby/email.rb', line 241

def set_unique_args(key_value_pairs)
  @smtpapi.set_unique_args(key_value_pairs)
  self
end

#to_web_formatObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/sendgrid_ruby/email.rb', line 295

def to_web_format()
  web = {
    'to'          => @to,
    'from'        => get_from,
    'x-smtpapi'   => @smtpapi.to_json,
    'subject'     => @subject,
    'text'        => @text,
    'html'        => @html,
    'headers'     => get_headers_json,
  }

  # web['cc']         = get_ccs     if get_ccs
  # web['bcc']        = get_bccs    if get_bccs
  web['fromname']   = @from_name  if @from_name
  web['replyto']    = @reply_to   if @reply_to
  web['to']         = @from       if (@smtpapi.to && @smtpapi.to.length > 0)

  if get_attachments
    get_attachments.each do |f|

      file          = f['file']
      extension     = f['extension']
      filename      = f['filename']
      full_filename = filename
      full_filename = filename + extension if extension
      full_filename = f['custom_filename'] if f['custom_filename']

      contents   = File.new(file, 'rb')

      web["files[#{full_filename}]"] = contents
    end
  end
  web
end