Sha256: e8a7fbe803fad2d36144fbfeff4c592f2e12ffcbce8078d2575f30d36c1b388c

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'uuid'

module Emailer
  
  class MockNetSmtp
    def start(*args); end
    def sendmail(*args); end
    def finish; end
    def started?; end
  end
  
  class MockSmtpFacade < SmtpFacade
  
    attr_reader :sent
    
    def initialize(settings = {})
      @sent = {}
      super
    end
    
    # Don't open connections...
    def get_net_smtp_instance
      MockNetSmtp.new
    end
    
    # And save, don't send, mail...
    def send_mail(options)
      raise ConnectionNotOpenError unless @open
      @sent[UUID.new.generate] = options
      true
    rescue ConnectionNotOpenError => e
      raise e
    rescue StandardError => e
      @error = e
      @offending_mail = options
      false
    end
    
    def last_email_sent
      @sent[@sent.keys.last.to_s]
    end
    
    def last_email_sent_key
      @sent.keys.last
    end
    
    def last_email_sent_url
      get_url_for last_email_sent_key
    end
    
    def get_url_for uuidString
      return "/getemail/"+uuidString
    end
  end
  
  class TestingMiddleware
    
    def call(env)
      if  env["PATH_INFO"].index("/getemail/")

        uuid = env["PATH_INFO"].sub("/getemail/".length)
        
        return [200, {"Content-Type" => "text/plain"},['Emailer::SmtpFacade.default is not a MockSmtpFacade']] unless
          Emailer::SmtpFacade.default.instance_of? Emailer::MockSmtpFacade 
        
        return [200, {"Content-Type" => "text/plain"},['No email sent']] if Emailer::SmtpFacade.default.sent.count == 0
        
        return [200, {"Content-Type" => "text/html"}, [Emailer::SmtpFacade.default.sent[uuid][:body].to_s]]
      else
        @app.call env
      end
    end
    
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bjornblomqvist-emailer-0.1.12 lib/emailer/mock_smtp_facade.rb