Created
July 20, 2012 20:00
-
-
Save jimrubenstein/3152882 to your computer and use it in GitHub Desktop.
mandrill library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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