Permalink
Browse files

rough promotion list with failing tests

  • Loading branch information...
1 parent 806840c commit e49250f3b5897396b5297238ee5667fe9f536178 @garrensmith committed Feb 12, 2011
Showing with 108 additions and 30 deletions.
  1. +1 −1 LICENSE
  2. +17 −12 examples/demo.js
  3. +56 −17 lib/index.js
  4. +34 −0 spec/promotions_spec.js
View
2 LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2010 Garren Smith
+Copyright (c) 2011 Garren Smith
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
29 examples/demo.js
@@ -1,19 +1,24 @@
var Madmimi = require('../lib/index');
+
+var madmimi = new Madmimi(process.env['email'],process.env["madmimi_api_key"], true);
-var madmimi = new Madmimi("email","api_key");
+/*var options = {
+ promotion_name:"Test Promotion",
+ recipient:"Jimi Hendrix <jimi@electricladyland.com>",
+ subject:"Test Promotion",
+ from:"no-reply@guitargear.com",
+ raw_html:"<html><head><title>Great promotion!</title></head><body>Cool guitar stuff[[tracking_beacon]]</body></html>"
+ };
-var options = {
- promotion_name:"Test Promotion",
- recipient:"Jimi Hendrix <jimi@electricladyland.com>",
- subject:"Test Promotion",
- from:"no-reply@guitargear.com",
- raw_html:"<html><head><title>Great promotion!</title></head><body>Cool guitar stuff[[tracking_beacon]]</body></html>"
-};
-
-madmimi.sendMail(options, function (msg) {
+ madmimi.sendMail(options, function (msg) {
console.log("email send, the id is: " + msg);
-});
+ });
-madmimi.mailStatus(2861240550, function (msg) {
+ madmimi.mailStatus(2861240550, function (msg) {
console.log("status: " + msg);
+ });*/
+
+
+madmimi.promotions(function (promotions) {
+ console.dir(promotions);
});
View
73 lib/index.js
@@ -1,5 +1,7 @@
var https = require('https'),
- querystring = require('querystring');
+ querystring = require('querystring'),
+ xml2js = require('xml2js');
+
var Madmimi = module.exports = function (username, api_key, debug) {
@@ -14,20 +16,20 @@ var Madmimi = module.exports = function (username, api_key, debug) {
}
var self = this;
-
+
self.username = username,
- self.api_key = api_key;
+ self.api_key = api_key;
+
+ self.request = function(requestOptions, body, cb) {
- self.request = function(requestOptions, body, cb) {
-
if(!cb) { cb = function () {} };
var httpBody = undefined
-
- if (arguments.length > 2) {
- httpBody = body;
- }
-
+
+ if (arguments.length > 2) {
+ httpBody = body;
+ }
+
var req = https.request(requestOptions, function(res) {
res.body = "";
@@ -71,29 +73,66 @@ var Madmimi = module.exports = function (username, api_key, debug) {
};
self.request(requestOptions, optionsParameterized, cb);
-
+
};
-
+
self.mailStatus = function(id, cb) {
options = {
- username: self.username,
+ username: self.username,
api_key: self.api_key
- };
+ };
optionsParameterized = querystring.stringify(options, "&", "=");
transactionPath = '/mailers/status/'+ id +'?' + optionsParameterized;
- var requestOptions = {
+ var requestOptions = {
host: 'madmimi.com',
port: '443',
path: transactionPath,
method: 'GET',
};
-
+
self.request(requestOptions, optionsParameterized, cb);
};
-
+ self.promotions = function (cb) {
+ options = {
+ username: self.username,
+ api_key: self.api_key
+ };
+
+ optionsParameterized = querystring.stringify(options, "&", "=");
+ transactionPath = '/promotions.xml' +'?' + optionsParameterized;
+
+ var requestOptions = {
+ host: 'api.madmimi.com',
+ port: '443',
+ path: transactionPath,
+ method: 'GET',
+ };
+
+ xml_parser_cb = function (data) {
+ logger("promotions_xml: " + data);
+
+ var parser = new xml2js.Parser();
+
+ parser.addListener('end', function(result) {
+ logger("result_js:");
+ logger(result);
+ var promotions = result.promotion;
+
+ cb(promotions);
+ });
+
+ parser.parseString(data);
+
+ };
+
+ self.request(requestOptions, optionsParameterized, xml_parser_cb);
+
+ };
+
+
return self;
};
View
34 spec/promotions_spec.js
@@ -0,0 +1,34 @@
+var describe = require('Jody').describe,
+ Madmimi = require('../lib/index.js');
+
+var madmimi = "";
+var email_options = {
+ promotion_name:"Test Promotion",
+ recipient:"Jimi Hendrix <jimi@electricladyland.com>",
+ subject:"Test Promotion",
+ from:"no-reply@guitargear.com",
+ raw_html:"<html><head><title>Great promotion!</title></head><body>Cool guitar stuff[[tracking_beacon]]</body></html>"
+};
+
+
+describe("Promotions").
+ beforeEach( function () {
+ madmimi = new Madmimi("fake@email.com", "fake-api-key");
+ }).
+ it("Should create http request for all promotions", function (atEnd) {
+ var requestOptions;
+
+ madmimi.request = function (options, body) {
+ requestOptions = options;
+ };
+
+ madmimi.promotions(function () {});
+
+ atEnd(function () {
+ requestOptions.host.should().beEqual('madmimi.com');
+ requestOptions.port.should().beEqual('443');
+ requestOptions.path.should().beEqual('/mailers/status/1234?username=fake%40email.com&api_key=fake-api-key');
+ requestOptions.method.should().beEqual('GET');
+ })
+ });
+

0 comments on commit e49250f

Please sign in to comment.