diff options
author | scottmotte <scott@scottmotte.com> | 2013-07-23 16:17:17 -0700 |
---|---|---|
committer | scottmotte <scott@scottmotte.com> | 2013-07-23 16:17:17 -0700 |
commit | 392b40836065918c74237775dc59bfe76ef265fc (patch) | |
tree | 9396faf7431a1374f74054811512539a89bb05d5 /lib/sendgrid.js | |
parent | dd8a35098e4edf5fd60f3410c2e476ff5e0c799d (diff) | |
download | sendgrid-nodejs-392b40836065918c74237775dc59bfe76ef265fc.zip sendgrid-nodejs-392b40836065918c74237775dc59bfe76ef265fc.tar.gz sendgrid-nodejs-392b40836065918c74237775dc59bfe76ef265fc.tar.bz2 |
Refactors sendgrid.js to be a bit more standard node module like and updates README
Diffstat (limited to 'lib/sendgrid.js')
-rw-r--r-- | lib/sendgrid.js | 167 |
1 files changed, 82 insertions, 85 deletions
diff --git a/lib/sendgrid.js b/lib/sendgrid.js index 15fee86..be04e96 100644 --- a/lib/sendgrid.js +++ b/lib/sendgrid.js @@ -1,70 +1,76 @@ "use strict"; -var package_json = require('./../package.json'); -var nodemailer = require('nodemailer'); -var request = require('request'); -var Email = require('./email'); - - -/* - * Class for handling communications with SendGrid. - * - * @param {String} api_user The SendGrid username. - * @param {String} api_key The key credentials for SendGrid. - */ -function SendGrid(api_user, api_key) { - this.api_user = api_user; - this.api_key = api_key; - this.version = package_json.version; - this.SMTP = "SMTP"; - if (process.env.NODE_ENV == "test") { - this.SMTP = "STUB"; - } -} +var package_json = require('./../package.json'); +var nodemailer = require('nodemailer'); +var request = require('request'); +var Email = require('./email'); +var SmtpapiHeaders = require('./smtpapi_headers'); -/* - * Sends an email via web. See .web method for more details. - * - */ -SendGrid.prototype.send = function(email, callback) { - this.web(email, callback); -} +module.exports = function(api_user, api_key) { + var self; -/* - * Sends an email via web and returns true if the - * message was sent successfully. - * - * @param {Email|Object} email An email object or a hash that has - * the values for the email to be sent. - * @param {Function} callback A function to call when the processing is done. - * This parameter is optional. - */ -SendGrid.prototype.web = function(email, callback) { - var api_user = this.api_user; - var api_key = this.api_key; - - var self = this - , cb = callback || function() { }; - - if (email.constructor !== Email) { - email = new Email(email); + var send = function(email, callback) { + web(email, callback); } - function send_web() { + /* + * Sends an email via web and returns true if the + * message was sent successfully. + * + * @param {Email|Object} email An email object or a hash that has + * the values for the email to be sent. + * @param {Function} callback A function to call when the processing is done. + * This parameter is optional. + */ + var web = function(email, callback) { + self = this; + var callback = callback || function() { }; + + if (email.constructor !== Email) { + email = new Email(email); + } + + _sendWeb(email, callback); + }; + + /* + * Sends an email via SMTP and returns true if the + * message was sent successfully. + * + * @param {Email|Object} email An email object or a hash that has + * the values for the email to be sent. + * @param {Function} callback A function to call when the processing is done. + * This parameter is optional. + */ + var smtp = function(email, callback) { + self = this; + var callback = callback || function() { }; + + if (email.constructor !== Email) { + email = new Email(email); + } + + _sendSmtp(email, callback); + }; + + /* + * Psuedo-private methods + */ + var _sendWeb = function(email, callback) { var req = request({ method : 'POST', uri : "https://sendgrid.com/api/mail.send.json" }, function(err, resp, body) { - if(err) return cb(err, null); + if(err) return callback(err, null); var json = JSON.parse(body); if (json.message !== 'success') { var error = 'sendgrid error'; if (json.errors) { error = json.errors.shift(); } - return cb(error, null); + return callback(error, null); } - return cb(null, json); + return callback(null, json); }); var form = email.toWebFormat(); @@ -87,46 +93,37 @@ SendGrid.prototype.web = function(email, callback) { } } - send_web(); -}; - -/* - * Sends an email via SMTP and returns true if the - * message was sent successfully. - * - * @param {Email|Object} email An email object or a hash that has - * the values for the email to be sent. - * @param {Function} callback A function to call when the processing is done. - * This parameter is optional. - */ -SendGrid.prototype.smtp = function(email, callback) { - var self = this - , smtpTransport - , cb = callback || function() { }; - - // SMTP settings - smtpTransport = nodemailer.createTransport(this.SMTP, { - service: 'SendGrid', - auth: { - user: this.api_user, - pass: this.api_key - } - }); + var _sendSmtp = function(email, callback) { + // SMTP settings + var smtpTransport = nodemailer.createTransport(self.SMTP, { + service: 'SendGrid', + auth: { + user: api_user, + pass: api_key + } + }); - function send_smtp() { smtpTransport.sendMail(email.toSmtpFormat(), function(err, response) { smtpTransport.close(); - if(err) { return cb(err.data, null);} + if(err) { return callback(err.data, null);} - return cb(null, {'message': 'success'}); + return callback(null, {'message': 'success'}); }); } - if (email.constructor !== Email) { - email = new Email(email); - } - - send_smtp(); -}; -module.exports = SendGrid; + /* + * Expose public API calls + */ + return { + version : package_json.version, + SMTP : "SMTP", + Email : Email, + SmtpapiHeaders : SmtpapiHeaders, + api_user : api_user, + api_key : api_key, + web : web, + smtp : smtp, + send : send + }; +} |