Class: Hermes::SendgridProvider

Inherits:
Provider
  • Object
show all
Defined in:
lib/providers/sendgrid/sendgrid_provider.rb

Instance Attribute Summary

Attributes inherited from Provider

#credentials, #defaults, #deliverer, #weight

Instance Method Summary collapse

Methods inherited from Provider

#initialize, #message_failure, #message_success, #provider_name, required_credentials

Methods included from Extractors

#complex_extract, #extract_from, #extract_html, #extract_text, #extract_to

Constructor Details

This class inherits a constructor from Hermes::Provider

Instance Method Details

#clientObject



39
40
41
42
43
44
# File 'lib/providers/sendgrid/sendgrid_provider.rb', line 39

def client
  SendGrid::Client.new({
    api_user: self.credentials[:api_user], 
    api_key: self.credentials[:api_key]
  })
end

#payload(rails_message) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/providers/sendgrid/sendgrid_provider.rb', line 15

def payload(rails_message)
  # requireds
  message = SendGrid::Mail.new({
    from: extract_from(rails_message, :address),
    from_name: extract_from(rails_message, :name),
    to: extract_to(rails_message, :address),
    to_name: extract_to(rails_message, :name),
    subject: rails_message.subject,
    html: extract_html(rails_message),
    text: extract_text(rails_message),
  })

  # optionals
  message.reply_to = rails_message[:reply_to].formatted.first if rails_message[:reply_to]
  message.message_id = rails_message[:message_id].value if rails_message.message_id

  # and any attachments
  rails_message.attachments.try(:each) do |attachment|
    message.add_attachment_file(Hermes::EmailAttachment.new(attachment))
  end

  return message
end

#send_message(rails_message) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/providers/sendgrid/sendgrid_provider.rb', line 5

def send_message(rails_message)
  payload = payload(rails_message)

  if self.deliverer.should_deliver?
    client.send(payload)
  end

  self.message_success(message_success)
end