-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcooladata.js
83 lines (61 loc) · 2.08 KB
/
cooladata.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Created by backand on 3/27/16.
*/
var unirest = require('unirest');
var url = ''; //enter here v3 version url //'https://api.cooladata.com/v3/ti9p1pqxkanfdfsfs8wdz94jv8jcaatag/track';
var dateConvert = require('./timeUtil');
var coolaAppender = function () {
};
//
// This function get an array of messages and send them to cooladata
//
coolaAppender.prototype.processMessages = function (msgBulk, cb) {
var self = this;
// have to clone it because we have to add new fields.
var newMsgBlk = [];
for (var i = 0; i < msgBulk.length; i++) {
var msg = msgBulk[i];
var newMsg = JSON.parse(msg.origin);
// clean message
Object.keys(newMsg).forEach(function (key) {
if (newMsg[key] === null) {
newMsg[key] = "";
}
newMsg[key] = newMsg[key].toString().replace(/["]/g, "'").replace(/[:]/g, "").trim();
});
newMsg.event_name = ''; // set here event name
newMsg.user_id = ''; // set here user_id
newMsg.event_timestamp_epoch = ''; // set here time, use this function: dateConvert(newMsg.Time);
newMsgBlk.push(newMsg);
}
// create bulk:
var packtBefore = {"events": newMsgBlk};
packt = encodeURIComponent(JSON.stringify(packtBefore)).replace(/'/g,"%27").replace(/"/g,"%22");
// send to cooladata
unirest.post(url)
.header('Content-Type', 'application/x-www-form-urlencoded')
.send(packt)
.end(function (res) {
var parsed;
try {
parsed = JSON.parse(res.raw_body);
// you can check here result equal to msgBulk.length
}
catch (err) {
cb(err);
return;
}
}
)
;
};
//
// Allow use to send a single event
//
coolaAppender.prototype.processMessage = function(msg, cb){
var arrayed = [msg];
return this.processMessages(arrayed, cb);
};
module.exports = coolaAppender;
var a = new coolaAppender();
a.processMessage(a, console.log);