Sha256: eee0a1190892fd7d8b81ea846cbea5b29bec8fa7b62818322b480de9761ab00c

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

load 'lib/mailcannon/adapters/sendgrid.rb'

class MailCannon::Envelope
  include Mongoid::Document
  include Mongoid::Timestamps
  include MailCannon::Adapter::Sendgrid
  
  has_one :mail
  has_many :stamps
  
  field :from, type: String
  field :from_name, type: String
  field :to, type: String
  field :to_name, type: String
  field :subject, type: String
  field :bcc, type: String
  field :reply_to, type: String
  field :date, type: Date
  field :xsmtpapi, type: Hash
  
  validates :from, :to, :subject, :mail, presence: true
  validates_associated :mail
  
  after_create do |envelope|
    envelope.stamp! MailCannon::Event::New.stamp
    MailCannon::SingleBarrel.perform_async(envelope.id)
  end
  
  def stamp!(code)
    self.class.valid_code_kind?(code)
    self.stamps << MailCannon::Stamp.from_code(code)
  end
  
  def after_sent(response)
    if response
      stamp!(MailCannon::Event::Processed.stamp)
      self.mail.destroy
      self.mail=nil # to avoid reload
    end
  end
  
  private
  def self.valid_code_kind?(code)
    unless [Fixnum, MailCannon::Stamp].include?(code.class) || MailCannon::Event.constants.include?(code.to_s.camelize.to_sym)
      raise 'code must be an Integer, MailCannon::Event::*, or MailCannon::Stamp !'
    end
  end
    
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mailcannon-0.0.2 lib/mailcannon/envelope.rb