-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (38 loc) · 1 KB
/
index.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
var request = require('request');
function report (endpoint, client_id, key, value, number, callback, optionalData) {
optionalData = optionalData || { };
var date = optionalData.date || new Date();
var precision = optionalData.precision || 'day';
var formData = {
group_id: optionalData.group_id,
client_id: client_id,
key: key,
value: value,
number: number,
date: dateToFormat(date, precision)
};
request.post(endpoint, function (err, body, response) {
if (err) {
callback(err);
} else {
callback(null);
}
}).form(formData);
}
function pad(str, length) {
str = String(str);
while (str.length < length) {
str = '0' + str;
}
return str;
}
function dateToFormat (date, precision) {
var month = pad(date.getMonth() + 1, 2);
var day = pad(date.getDate(), 2);
var output = date.getFullYear() + "-" + month + "-" + day;
if (precision === 'hour') {
output += " " + pad(date.getHours(), 2);
}
return output;
}
exports.report = report;