Class: DMCourier::Services::Sendgrid

Inherits:
Object
  • Object
show all
Includes:
MessageHelper
Defined in:
lib/dm_courier/services/sendgrid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageHelper

#attachments, #attachments?, #extract_params, #fallback_options, #from, #from_address, #from_email, #from_name, #html_part, #metadata, #nil_true_false?, #return_string_value, #subject, #text_part

Constructor Details

#initialize(mail, options = {}) ⇒ Sendgrid

Returns a new instance of Sendgrid.



13
14
15
16
17
# File 'lib/dm_courier/services/sendgrid.rb', line 13

def initialize(mail, options = {})
  @mail = mail
  @api_key = options.fetch(:api_key)
  @options = options
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/dm_courier/services/sendgrid.rb', line 11

def api_key
  @api_key
end

#mailObject (readonly)

Returns the value of attribute mail.



11
12
13
# File 'lib/dm_courier/services/sendgrid.rb', line 11

def mail
  @mail
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/dm_courier/services/sendgrid.rb', line 11

def options
  @options
end

Instance Method Details

#deliver!Object



23
24
25
26
# File 'lib/dm_courier/services/sendgrid.rb', line 23

def deliver!
  sendgrid_api = ::SendGrid::Client.new(api_key)
  sendgrid_api.send(sendgrid_message)
end

#nameObject



19
20
21
# File 'lib/dm_courier/services/sendgrid.rb', line 19

def name
  :sendgrid
end

#sendgrid_messageObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dm_courier/services/sendgrid.rb', line 28

def sendgrid_message
  message = { to: (mail[:to].formatted if mail[:to]),
              cc: (mail[:cc].formatted if mail[:cc]),
              bcc: return_string_value(:bcc_address),
              from: from_email,
              from_name: from_name,
              subject: subject,
              html: html_part,
              text: text_part,
              reply_to: reply_to }

  message[:attachments] = regular_attachments

  ::SendGrid::Mail.new(message) do |mail|
    inline_attachments.each do |attachment|
      mail.contents << attachment
    end
  end
end