diff options
author | Jacob Lowe <jacoblowe2.0@gmail.com> | 2013-08-31 14:46:48 -0700 |
---|---|---|
committer | Jacob Lowe <jacoblowe2.0@gmail.com> | 2013-08-31 14:46:48 -0700 |
commit | a8bf847768377cfb4456c32ce572e298d587cffd (patch) | |
tree | 828b1f64c7166fb5bc88b726af608db7f43ac6e1 /lib/sendgrid.js | |
parent | 542b318379d76201dc1ee28d18559ed27ab3c210 (diff) | |
download | sendgrid-nodejs-a8bf847768377cfb4456c32ce572e298d587cffd.zip sendgrid-nodejs-a8bf847768377cfb4456c32ce572e298d587cffd.tar.gz sendgrid-nodejs-a8bf847768377cfb4456c32ce572e298d587cffd.tar.bz2 |
fixing context and magically invocating functions, also adding also allowing send to map to either smtp or http
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r-- | lib/sendgrid.js | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index ab32956..1850424 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -6,12 +6,27 @@ var _ = require('underscore'); var request = require('request'); var Email = require('./email'); var SmtpapiHeaders = require('./smtpapi_headers'); +var Sendgrid = function (api_user, api_key, options) { -module.exports = function(api_user, api_key) { - var self; + console.log( this ); - var send = function(email, callback) { - web(email, callback); + if( !(this instanceof Sendgrid) ) { + return new Sendgrid(api_user, api_key, options); + } + + var _this = this; + + _this.options = options || {}; + + var send = function( ) { + + if( _this.options.api === 'smtp'){ + smtp.apply( this, arguments ); + return true; + } + + web.apply( this, arguments ); + } /* @@ -24,9 +39,7 @@ module.exports = function(api_user, api_key) { * This parameter is optional. */ var web = function(email, callback) { - self = this; var callback = callback || function() { }; - if (email.constructor !== Email) { email = new Email(email); } @@ -46,7 +59,6 @@ module.exports = function(api_user, api_key) { * This parameter is optional. */ var smtp = function(email, nodeMailerOptions, callback) { - self = this; // Support a callback without nodeMailerOptions if (! callback && typeof nodeMailerOptions === "function") { @@ -113,7 +125,7 @@ module.exports = function(api_user, api_key) { // SMTP settings var smtp_settings = { host: "smtp.sendgrid.net", - port: parseInt(self.port), + port: parseInt(_this.port), requiresAuth: true, auth: { user: api_user, @@ -124,7 +136,7 @@ module.exports = function(api_user, api_key) { smtp_settings['secureConnection'] = true; } - var smtpTransport = nodemailer.createTransport(self.SMTP, smtp_settings); + var smtpTransport = nodemailer.createTransport(_this.SMTP, smtp_settings); var smtpParams = email.toSmtpFormat(); @@ -154,6 +166,10 @@ module.exports = function(api_user, api_key) { api_key : api_key, web : web, smtp : smtp, - send : send + send : send, + options : this.options }; -} + +}; + +module.exports = Sendgrid; |