-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.js
79 lines (62 loc) · 3.3 KB
/
main.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
// Copyright 2011-2014 Numerotron, Inc. / Patrick Crosby
//
// StatHat API node.js module
//
(function() {
var http = require('http');
var https = require('https');
var StatHat = {
useHTTPS: false,
trackValue: function(user_key, stat_key, value, callback) {
this._postRequest('/v', {key: stat_key, ukey: user_key, value: value}, callback);
},
trackValueWithTime: function(user_key, stat_key, value, timestamp, callback) {
this._postRequest('/v', {key: stat_key, ukey: user_key, value: value, t: timestamp}, callback);
},
trackCount: function(user_key, stat_key, count, callback) {
this._postRequest('/c', {key: stat_key, ukey: user_key, count: count}, callback);
},
trackCountWithTime: function(user_key, stat_key, count, timestamp, callback) {
this._postRequest('/c', {key: stat_key, ukey: user_key, count: count, t: timestamp}, callback);
},
trackEZValue: function(ezkey, stat_name, value, callback) {
this._postRequest('/ez', {ezkey: ezkey, stat: stat_name, value: value}, callback);
},
trackEZValueWithTime: function(ezkey, stat_name, value, timestamp, callback) {
this._postRequest('/ez', {ezkey: ezkey, stat: stat_name, value: value, t: timestamp}, callback);
},
trackEZCount: function(ezkey, stat_name, count, callback) {
this._postRequest('/ez', {ezkey: ezkey, stat: stat_name, count: count}, callback);
},
trackEZCountWithTime: function(ezkey, stat_name, count, timestamp, callback) {
this._postRequest('/ez', {ezkey: ezkey, stat: stat_name, count: count, t: timestamp}, callback);
},
_postRequest: function(path, params, callback) {
var qs = Object.keys(params)
.map( function(k) { return k + '=' + params[k] } )
.join('&');
var options = {
hostname: 'api.stathat.com',
path: path,
method: 'POST',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : qs.length
}
};
var hmod = this.useHTTPS ? https : http;
var request = hmod.request(options, function(res) {
res.on('data', function(chunk) {
callback(res.statusCode, chunk);
});
});
request.on('error', function(e) {
if (!e) e = {};
callback(600,e.message);
});
request.write(qs);
request.end();
},
};
module.exports = StatHat;
}())