class SendGridMailer::DevDeliverer

Public Instance Methods

deliver!(sg_definition) click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 8
def deliver!(sg_definition)
  @sg_definition = sg_definition
  execute_interceptors(@sg_definition)
  log_definition(@sg_definition)
  letter_opener_delivery_method.deliver!(mail)
end

Private Instance Methods

api_key() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 21
def api_key
  Rails.application.config.action_mailer.sendgrid_dev_settings[:api_key]
rescue
  nil
end
dev_emails_location() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 31
def dev_emails_location
  Rails.application.config.action_mailer.sendgrid_dev_settings[:emails_location] || "/tmp/mails"
rescue
  "/tmp/mails"
end
emails(origin) click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 50
def emails(origin)
  @emails ||= {}
  return @emails[origin] if @emails.has_key?(origin)

  @emails[origin] = @sg_definition.personalization.send(origin)&.map {|em| em["email"]}
end
letter_opener_delivery_method() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 27
def letter_opener_delivery_method
  @letter_opener_delivery_method ||= LetterOpener::DeliveryMethod.new(location: dev_emails_location)
end
mail() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 57
def mail
  template = (parsed_template || @sg_definition.mail.contents[0]['value']).html_safe
  m = Mail.new
  m.html_part = template
  m.subject = @sg_definition.personalization.subject
  m.from = @sg_definition.mail.from["email"] if @sg_definition.mail.from.present?
  m.to = emails(:tos) if emails(:tos).present?
  m.cc = emails(:ccs) if emails(:ccs).present?
  m.bcc = emails(:bccs) if emails(:bccs).present?

  m
end
parsed_template() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 37
def parsed_template
  template_response = sg_api.get_template(@sg_definition)
  template_versions = JSON.parse(template_response.body)["versions"]
  return if template_versions.blank?

  template_active_version = template_versions.find { |version| version["active"] == 1 }
  template_content = template_active_version["html_content"]
  @sg_definition.personalization.substitutions.each { |k, v| template_content.gsub!(k, v) }
  template = Handlebars::Context.new.compile(template_content)
  template_content = template.call(@sg_definition.personalization.dynamic_template_data)
  template_content
end
sg_api() click to toggle source
# File lib/send_grid_mailer/dev_deliverer.rb, line 17
def sg_api
  @sg_api ||= Api.new(api_key)
end