class MailerApi

Attributes

mail[R]
options[R]

Public Class Methods

new(mail, options = {}) click to toggle source
# File lib/mailpy/mailer_api.rb, line 4
def initialize(mail, options = {})
  @mail = mail
  @options = options
end

Public Instance Methods

send() click to toggle source
# File lib/mailpy/mailer_api.rb, line 9
def send
  result = HTTParty.post(
    options[:endpoint],
    body: form_data,
    headers: headers
  )
end

Private Instance Methods

attachment_data() click to toggle source
# File lib/mailpy/mailer_api.rb, line 37
def attachment_data
  attachments = {}
  mail.attachments.each { |attachment| attachments[attachment.filename] = attachment.body }
  attachments
end
body_data() click to toggle source
# File lib/mailpy/mailer_api.rb, line 33
def body_data
  mail.body.parts.present? ? mail.body.parts[0].body.to_s : mail.body.to_s
end
form_data() click to toggle source
# File lib/mailpy/mailer_api.rb, line 20
def form_data
  {
    to: mail.to.try(:join, ', '),
    cc: mail.cc.try(:join, ', '),
    bcc: mail.bcc.try(:join, ', '),
    from: mail.from.try(:join, ', '),
    options: eval(mail[:options].to_s),
    subject: mail.subject,
    body: body_data,
    attachment: attachment_data
  }
end
headers() click to toggle source
# File lib/mailpy/mailer_api.rb, line 43
def headers
  options[:headers]
end