Remove unnecessary switch, expose available transports via hash on Transport constructor #97

Merged
merged 1 commit into from Aug 14, 2012
View
@@ -8,6 +8,16 @@ var SendmailTransport = require("./engines/sendmail"),
module.exports.Transport = Transport;
/**
+ * Hash of available transports (uppercase) and their constructor methods
+ */
+Transport.transports = {
+ "SMTP": SMTPTransport,
+ "SES": SESTransport,
+ "SENDMAIL": SendmailTransport,
+ "STUB": StubTransport
+}
+
+/**
* <p>Generates a Transport object that can be used to deliver e-mail.</p>
*
* <p>All transport objects need to have <code>sendMail</code> property defined
@@ -23,21 +33,11 @@ function Transport(type, options){
this.transportType = (type || "").toString().trim().toUpperCase();
this.dkimOptions = false;
- switch(this.transportType){
- case "SMTP":
- this.transport = new SMTPTransport(this.options);
- break;
- case "SES":
- this.transport = new SESTransport(this.options);
- break;
- case "SENDMAIL":
- this.transport = new SendmailTransport(this.options);
- break;
- case "STUB":
- this.transport = new StubTransport(this.options);
- break;
- default:
- this.transport = false;
+ var constructor = Transport.transports[this.transportType];
+ if(constructor){
+ this.transport = new constructor(this.options);
+ }else{
+ this.transport = false;
}
}
@@ -107,4 +107,4 @@ Transport.prototype.close = function(callback){
callback(null);
}
}
-};
+};