class MailCannon::Envelope

Where the magic happens, the Envelope is responsible for keeping the information necessary to send the email(s) and holding the Stamps related to mailing Events.

Private Class Methods

valid_code_kind?(code) click to toggle source
# File lib/mailcannon/envelope.rb, line 91
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

Public Instance Methods

after_sent(response) click to toggle source

Callback to be run after the Envelope has been processed.

# File lib/mailcannon/envelope.rb, line 52
def after_sent(response)
  if response
    stamp!(MailCannon::Event::Processed.stamp)
    if MailCannon.config['auto_destroy']
      self.mail.destroy
      self.mail=nil # to avoid reload
    end
  end
end
post_envelope!() click to toggle source

Post this Envelope!

# File lib/mailcannon/envelope.rb, line 29
def post_envelope!
  self.save if self.changed?
  raise "Envelope(#{self.id}) has no mail! Didn't you already send it?" unless self.mail
  if validate_xsmtpapi(self.xsmtpapi)
    jid = schedule_send_job
    self.save if self.changed?
  else
    raise 'Invalid xsmtpapi hash!'
  end
end
posted?() click to toggle source
# File lib/mailcannon/envelope.rb, line 62
def posted?
  if self.stamps.where(code: 0).count > 0
    true
  else
    false
  end
end
processed?() click to toggle source
# File lib/mailcannon/envelope.rb, line 70
def processed?
  if self.stamps.where(code: 1).count > 0
    true
  else
    false
  end
end
stamp!(code,recipient=nil) click to toggle source

Stamp this Envelope with code.

# File lib/mailcannon/envelope.rb, line 42
def stamp!(code,recipient=nil)
  self.class.valid_code_kind?(code)
  unless self.persisted?
    logger.warn "You're trying to save the Stamp with an unsaved Envelope! Auto-saving Envelope."
    self.save
  end
  self.stamps.create(code: MailCannon::Stamp.from_code(code).code, recipient: recipient)
end

Private Instance Methods

schedule_send_job() click to toggle source
# File lib/mailcannon/envelope.rb, line 79
def schedule_send_job
  if MailCannon.config['waiting_time'].to_i>0
    self.jid = MailCannon::Barrel.perform_in(MailCannon.config['waiting_time'].seconds,self.id)
  else
    self.jid = MailCannon::Barrel.perform_async(self.id)
  end
  if self.jid
    self.stamp! MailCannon::Event::Posted.stamp
    return self.jid
  end
end
validate_xsmtpapi(xsmtpapi) click to toggle source
# File lib/mailcannon/envelope.rb, line 97
def validate_xsmtpapi(xsmtpapi)
  return true # TODO write tests for this method
  if xsmtpapi['to'].is_a? Array
    true
  else
    false
  end
end