Permalink
Browse files

refactoring and cleanup

  • Loading branch information...
1 parent a55597a commit 65632d321499152209b634b5909e72c6e04ad0c9 @garrensmith committed Feb 10, 2011
Showing with 100 additions and 176 deletions.
  1. 0 { → examples}/demo.js
  2. +0 −115 index.js
  3. +100 −0 lib/index.js
  4. +0 −61 test.js
File renamed without changes.
View
115 index.js
@@ -1,115 +0,0 @@
-var https = require('https'),
- querystring = require('querystring');
-
-
-
-var Madmimi = module.exports = function (username, api_key) {
-
- if (!username || !api_key) {
- throw("need to specify username and api key")
- }
-
- var self = this;
-
- self.username = username,
- self.api_key = api_key;
-
-
- self.sendMail = function (options, cb) {
- /*params = {
- username: "garren.smith@gmail.com",
- api_key:"d152284b309e8bb771524e88d7772b9e",
- promotion_name:"Test Promotion",
- recipient:"Garren Smith <garren.smith@gmail.com>",
- subject:"Test Promotion",
- //bcc:"admin@example.com",
- from:"no-reply@classroom7.com",
- raw_html:"<html><head><title>My great promotion!</title></head><body>Body stuff[[tracking_beacon]]</body></html>"
- }*/
-
- options.username = self.username;
- options.api_key = self.api_key;
-
-
- optionsParameterized = querystring.stringify(options, "&", "=");
-
-
-
- var request_options = {
- host: 'api.madmimi.com',
- port: '443',
- path: '/mailer',
- method: 'POST',
- headers: {'content-type': 'application/x-www-form-urlencoded'}
- };
-
- var req = https.request(request_options, function(res) {
- res.body = "";
-
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
-
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- res.body += chunk;
- });
-
- res.on('end', function () {
- console.log('end');
- cb(res.body);
- });
- });
-
- req.write(optionsParameterized);
- req.end();
-
- };
-
- self.mailStatus = function(id, cb) {
- options = {
- username: self.username,
- api_key: self.api_key
- };
-
- optionsParameterized = querystring.stringify(options, "&", "=");
- transactionPath = '/mailers/status/'+ id +'?' + optionsParameterized;
-
- var request_options = {
- host: 'madmimi.com',
- port: '443',
- path: transactionPath,
- method: 'GET',
- //headers: {'content-type': 'application/x-www-form-urlencoded'}
- };
-
-
-
- var req = https.request(request_options, function(res) {
- res.body = "";
-
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
-
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- res.body += chunk;
- });
-
- res.on('end', function () {
- console.log('end');
- cb(res.body);
- });
- });
-
- req.end();
-
-
- };
-
- return self;
-
-};
-
-
View
@@ -0,0 +1,100 @@
+var https = require('https'),
+ querystring = require('querystring');
+
+var Madmimi = module.exports = function (username, api_key, debug) {
+
+ if (debug) {
+ logger = console.log;
+ } else {
+ logger = function () {};
+ }
+
+ if (!username || !api_key) {
+ throw("need to specify username and api key")
+ }
+
+ var self = this;
+
+ self.username = username,
+ self.api_key = api_key;
+
+ self.request = function(requestOptions, body, cb) {
+
+ if(!cb) { cb = function () {} };
+
+ var httpBody = undefined
+
+ if (arguments.length > 2) {
+ httpBody = body;
+ }
+
+ var req = https.request(requestOptions, function(res) {
+ res.body = "";
+
+ logger('STATUS: ' + res.statusCode);
+ logger('HEADERS: ' + JSON.stringify(res.headers));
+
+ res.setEncoding('utf8');
+ res.on('data', function (chunk) {
+ logger('BODY: ' + chunk);
+ res.body += chunk;
+ });
+
+ res.on('end', function () {
+ logger('end');
+ cb(res.body);
+ });
+ });
+
+ if(httpBody) {
+ req.write(optionsParameterized);
+ };
+
+ req.end();
+ };
+
+
+
+ self.sendMail = function (options, cb) {
+ options.username = self.username;
+ options.api_key = self.api_key;
+
+
+ optionsParameterized = querystring.stringify(options, "&", "=");
+
+ var requestOptions = {
+ host: 'api.madmimi.com',
+ port: '443',
+ path: '/mailer',
+ method: 'POST',
+ headers: {'content-type': 'application/x-www-form-urlencoded'}
+ };
+
+ self.request(requestOptions, optionsParameterized, cb);
+
+ };
+
+ self.mailStatus = function(id, cb) {
+ options = {
+ username: self.username,
+ api_key: self.api_key
+ };
+
+ optionsParameterized = querystring.stringify(options, "&", "=");
+ transactionPath = '/mailers/status/'+ id +'?' + optionsParameterized;
+
+ var requestOptions = {
+ host: 'madmimi.com',
+ port: '443',
+ path: transactionPath,
+ method: 'GET',
+ };
+
+ self.request(requestOptions, optionsParameterized, cb);
+ };
+
+
+ return self;
+};
+
+
View
61 test.js
@@ -1,61 +0,0 @@
-var https = require('https'),
- sys = require('sys'),
- querystring = require('querystring');
- params = {
- username:"garren.smith@gmail.com",
- api_key:"d152284b309e8bb771524e88d7772b9e",
- promotion_name:"Test Promotion",
- recipient:"Garren Smith <garren.smith@gmail.com>",
- subject:"Test Promotion",
- //bcc:"admin@example.com",
- from:"no-reply@classroom7.com",
- raw_html:"<html><head><title>My great promotion!</title></head><body>Body stuff[[tracking_beacon]]</body></html>"
- },
-
- body = querystring.stringify(params, "&", "=");
-
-/*
-username=YourMadMimiEmailAddress
- api_key=YourMadMimiApiKey
- promotion_name=Welcome to Acme Widgets
- recipient=Dave Hoover <dave@example.com>
- subject=Welcome to Acme Widgets
- bcc=admin@example.com
- from=no-reply@example.com
- raw_html=yourhtml
-
- *
- */
-
-
-console.log("my query: " + body);
-
-var options = {
- host: 'api.madmimi.com',
- port: '443',
- path: '/mailer',
- method: 'POST',
- headers: {'content-type': 'application/x-www-form-urlencoded'}
-};
-
-var req = https.request(options, function(res) {
- res.body = "";
-
- console.log('STATUS: ' + res.statusCode);
- console.log('HEADERS: ' + JSON.stringify(res.headers));
-
- res.setEncoding('utf8');
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- res.body += chunk;
- });
-
- res.on('end', function () {
- console.log('end');
- });
-});
-
- req.write(body);
- req.end();
-
-

0 comments on commit 65632d3

Please sign in to comment.