-
Notifications
You must be signed in to change notification settings - Fork 19
/
mandrill.js
66 lines (55 loc) · 1.73 KB
/
mandrill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var request = require('request'),
_ = require('underscore');
var MANDRILL_API_ROOT = 'https://mandrillapp.com/api/1.0/';
function makeMandrill(key)
{
function mandrill(path, opts, callback)
{
var format = path.split('.');
path = format[0];
if (format.length == 1) format = 'json';
else format = format[1].toLowerCase();
if (typeof opts == 'function')
{
var callback = opts;
opts = { key: key };
}
var requestOptions = {
method: 'POST',
url: MANDRILL_API_ROOT + path + '.' + format,
};
requestOptions['body'] = JSON.stringify( _.extend({ key: key }, opts) );
request(requestOptions, function(error, response, body)
{
if (typeof callback == 'function')
{
if (!error)
{
//everything is good!
if (format == 'json')
{
try {
body = JSON.parse(body);
} catch (e) {
callback(e);
}
}
if (response['statusCode'] >= 200 && response['statusCode'] < 300)
{
callback(null, body);
}
else
{
callback(body);
}
}
else
{
callback(error);
}
}
});
}
return mandrill;
}
module.exports = makeMandrill;