Skip to content

Instantly share code, notes, and snippets.

@jimrubenstein
Created July 20, 2012 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimrubenstein/3152882 to your computer and use it in GitHub Desktop.
Save jimrubenstein/3152882 to your computer and use it in GitHub Desktop.
mandrill library
var request = require('request'),
_ = require('underscore');
var MANDRILL_API_ROOT = 'https://mandrillapp.com/api/1.0/';
function makeMandrill(key)
{
function mandrill(path, opts, callback)
{
console.log(this.key, 'key');
var requestOptions = {
method: 'POST',
uri: MANDRILL_API_ROOT + path,
};
if (typeof opts == 'object')
{
requestOptions['multipart'] = [{
'content-type': 'application/json',
body: JSON.stringify(_.extend(opts, { key: this.key }))
}];
}
request(requestOptions, function(error, response, body)
{
if (typeof callback == 'function')
{
if (!error)
{
//everything is good!
if (path.substr(-4).toLowerCase() == 'json')
{
body = JSON.parse(body);
}
if (Math.floor(response['statusCode'] / 200) == 1) //200 response code
{
callback(null, body);
}
else
{
callback(body);
}
}
else
{
callback(error);
}
}
});
}
mandrill.key = key;
return mandrill;
}
md = makeMandrill('my mandrill key');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment