class Email::Delivery::SendgridClient

Public Instance Methods

content(body) click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 51
def content(body)
  [
      {
          "type" => "text/plain",
          "value" => body
      }
  ]
end
dispatch(from, to, cc, bcc, subject, body) click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 16
def dispatch(from, to, cc, bcc, subject, body)
  response = RestClient.post("#{ENV['SENDGRID_API_URL']}", JSON.dump(payload(from, to, cc, bcc, subject, body)), headers=headers_hash)

  {
      status: response.code,
      message: response.description
  }
end
from(from) click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 45
def from(from)
  {
      "email" => from
  }
end
headers_hash() click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 69
def headers_hash
  {
      "Authorization" => "Bearer #{ENV['SENDGRID_API_KEY']}",
      "content-type" => "application/json"
  }
end
payload(from, to, cc, bcc, subject, body) click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 60
def payload(from, to, cc, bcc, subject, body)
  {
      "personalizations" => personalizations(to, cc, bcc) ,
      "from" => from(from) ,
      "subject" => subject,
      "content" => content(body)
  }
end
personalizations(to, cc, bcc) click to toggle source

This method helps to include all recipients defined within the to, cc, and bcc parameters, across each object that you include in the personalizations array.

# File lib/email/delivery/sendgrid_client.rb, line 27
def personalizations(to, cc, bcc)
  personalizations_hash = {
      "to" => recepient_list(to)
  }

  unless cc.nil?
    personalizations_hash.merge!({ "cc" => recepient_list(cc) })
  end

  unless bcc.nil?
    personalizations_hash.merge!({ "bcc" => recepient_list(cc) })
  end

  [
      personalizations_hash
  ]
end
recepient_list(lst) click to toggle source
# File lib/email/delivery/sendgrid_client.rb, line 76
def recepient_list(lst)
  if lst.is_a?(Array)
    lst.map {|elem| {"email" => elem } }
  else
    [
        {
            "email" => lst
        }
    ]
  end
end