Sha256: a22e5a6775978acfe8ba675145c24a7402a962175cf9d9a475029167745fb55b

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

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.3 lib/mailcannon/envelope.rb