-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrti.js
147 lines (134 loc) · 5.3 KB
/
rti.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* global module, define */
(function () {
function moduleDefiniton() {
/**
* Manage RT with its REST API
* @class RT
* @param {Object} [cfg]
* @param {String} [cfg.url]
* @singleton
*/
function RT(cfg) {
function init() {
this.url = cfg.url || this.url; // always put a property in instance, I wanna see it in console
this.debug = typeof cfg.debug !== 'undefined' ? cfg.debug : this.debug;
}
/**
*
* @param {Number|String} id
* @param {Function} [callback]
* callback param:
* {
* success: true,
* ticketData: {
* id: "1",
* owner: "you"
* },
* errorText: ""
* }
* @return {undefined}
*/
this.getTicket = function (id, callback) {
var fullUrl;
fullUrl = this.url + this.getTicketPattern.replace('%id%', id);
this.ajax({
method: 'POST',
url: fullUrl,
success: this.onGetTicket.bind(this, callback),
error: this.onGetTicketError.bind(this, callback)
});
};
return init.call(this);
}
var protoStuff = {
/**
* @param {String}
*/
url: 'http://rt.easter-eggs.org/demos/4.2/',
getTicketPattern: 'ticket/%id%/show',
debug: true,
/**
* Custom AJAX function, implement it yourself if wanna be jquery-independent
*/
nonJqueryAjax: null,
onGetTicket: function (callback, res) {
var parsedRes = this.parseResponse(res);
if (!parsedRes.success){
this.debug && console.error('Error getting ticket data: ', parsedRes.errorText || '<No error text>');
}else if ( callback && typeof callback === 'function' ){
callback(parsedRes); // assuming scope and everything is bound already
}
},
onGetTicketError: function (callback, res) {
var errorText = res.statusText || '<No error text>';
this.debug && console.error('Error getting ticket data: ', errorText, ' Status: ' + res.status);
if ( callback && typeof callback === 'function' ){
callback({
success: false,
data: {},
errorText: errorText
});
}
},
parseResponse: function(ticketResponse){
var r, statusLine, emptyLine, possibleErrorLine, data, line, success, errorText;
data = {};
success = true;
errorText ='';
r = ticketResponse;
r = r.split('\n');
statusLine = r.shift();
emptyLine = r.shift();
possibleErrorLine = r[0];
if (!statusLine || statusLine.toLowerCase().indexOf('200 ok') < 0 || emptyLine !== ''){
errorText = 'Something went wrong, incorrect server answer structure.';
success = false;
}else if ( possibleErrorLine && possibleErrorLine.indexOf('#') === 0 ){
possibleErrorLine = possibleErrorLine.replace('#', '').replace(/^\s+/gim, '');
errorText = possibleErrorLine;
success = false;
}else{
for (var i = 0; i < r.length; i++){
line = r[i];
if (line !== '' && !!line && line.indexOf(': ')){
line = line.split(': ');
if ( line.length === 2 ){
if (line[0] === 'id'){
line[1] = line[1].replace('ticket/', '');
}
data[line[0]] = line[1];
}
}
}
}
return {
success: success,
errorText: errorText,
ticketData: data
};
},
ajax: function(ajaxParams){
if (this.nonJqueryAjax){
return this.nonJqueryAjax(ajaxParams);
}else{
return $.ajax(ajaxParams);
}
}
};
for (var stuffName in protoStuff) {
if (typeof protoStuff[stuffName] === 'function') {
RT.prototype[stuffName] = protoStuff[stuffName].bind(RT.prototype);
} else {
RT.prototype[stuffName] = protoStuff[stuffName];
}
}
return RT;
}
if (typeof define === "function" && define.amd) {
define([], moduleDefiniton);
} else if (typeof module === "object" && module.exports) {
module.exports = moduleDefiniton;
} else {
window['RTIConstructor'] = moduleDefiniton();
}
})();