This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtor.js
113 lines (98 loc) · 2.7 KB
/
tor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var crypto = require("crypto"),
_ = require("underscore"),
needle = require("needle");
var tor = (function () {
var module = {};
module.md5 = function(value){
return crypto.createHash("md5").update(value).digest("hex");
};
serialize = function(obj, prefix) {
var str = [];
for(var p in obj) {
var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
str.push(typeof v == "object" ?
serialize(v, k) :
encodeURIComponent(k) + "=" + encodeURIComponent(v));
}
return str.join("&");
};
module.TorApi = function(apiKey, projectDomain){
this.apiKey = apiKey;
this.projectDomain = projectDomain;
this.apiToken = module.md5(this.projectDomain + module.md5(this.apiKey));
this.apiVersion = "1";
this.serverUrl = "http://api.ticketonrails.com";
this.request = function(url, method, params, attachment, callback){
var requestCallBack = callback;
var self = this;
requestUrl = this.serverUrl + "/v" + this.apiVersion + url;
params["token"] = this.apiToken;
switch(method){
case "POST":
if (attachment) {
params["attachment"] = {
file: attachment,
content_type: "application/octet-stream"
};
needle.post(
requestUrl,
params,
{ multipart: true },
function(err, resp, body){
self.parseResponse(err, resp, body, requestCallBack);
}
);
} else {
needle.post(requestUrl, params, function(err, resp, body){
self.parseResponse(err, resp, body, requestCallBack);
});
}
break;
case "GET":
needle.get(requestUrl + '?' + serialize(params), {}, function(err, resp, body){
self.parseResponse(err, resp, body, requestCallBack);
});
break;
// TODO: implement all the other methods
}
};
this.parseResponse = function(err, resp, body, requestCallBack){
response = JSON.parse(body);
requestCallBack(response);
};
this.newTicket = function(ticketObj, callback){
params = {};
ticket = {};
attachment = null;
if(ticketObj){
ticketParams = [
"email",
"from_name",
"subject",
"body",
"html",
"date",
"labels"
];
ticket = _.pick(ticketObj, ticketParams);
if (_.has(ticketObj, "attachment")){
attachment = ticketObj["attachment"];
}
params["ticket"] = JSON.stringify(ticket);
}
this.request("/tickets", "POST", params, attachment, callback);
};
this.getTicket = function(id, callback){
this.request("/tickets/" + id, "GET", {}, null, callback);
};
this.getTickets = function(page, limit, callback){
params = {
page: page,
limit: limit
};
this.request("/tickets", "GET", params, null, callback);
};
};
return module;
}());
module.exports = tor;