UNPKG

1.45 kBJavaScriptView Raw
1var request = require('request');
2var ziptoo = require('funkit').functional.ziptoo;
3
4
5function init(config) {
6 // https://www.pingdom.com/services/api-documentation-rest/
7 var baseUrl = 'https://api.pingdom.com/api/2.0/';
8 var resources = ['actions', 'analysis', 'checks', 'contacts', 'credits', 'probes',
9 'reference', 'reports.email', 'reports.public', 'reports.shared',
10 'results', 'servertime', 'settings', 'summary.average', 'summary.hoursofday',
11 'summary.outage', 'summary.performance', 'summary.probes', 'single', 'traceroute'
12 ];
13
14 return ziptoo(resources.map(function(resource) {
15 return [resource, template.bind(undefined, config, baseUrl, resource)];
16 }));
17}
18module.exports = init;
19
20function template(config, baseUrl, property, cb, o) {
21 o = o || {};
22 var target = o.target || '';
23 var qs = o.qs || {};
24
25 // accept Date objects and convert them to Unix format here
26 if(qs.to) qs.to = dateToUnix(qs.to);
27 if(qs.from) qs.from = dateToUnix(qs.from);
28
29 request.get(baseUrl + property + '/' + target, {
30 auth: config,
31 headers: {
32 'App-Key': config.appkey
33 },
34 qs: qs
35 }, function(err, res) {
36 try {
37 cb(err, JSON.parse(res.body)[property.split('.')[0]], res);
38 }
39 catch(e) {
40 cb(e, null, res);
41 }
42 });
43}
44
45function dateToUnix(date) {
46 return parseInt(date.getTime() / 1000, 10);
47}