forked from Qard/node-hipchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (153 loc) · 4.93 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var http = require('http');
var qstring = require('querystring');
// Create API section handlers.
var Rooms = function(key) { this.key = key; };
var Users = function(key) { this.key = key; };
/***************
* *
* Rooms *
* *
***************/
// Get the history of a room. Selects recent if date not supplied.
Rooms.prototype.history = function(room, date, cb) {
var last = arguments[arguments.length-1];
var cb = (typeof last === 'function') ? last : function(){};
if (arguments.length < 3) { date = 'recent'; }
var data = typeof room !== 'object' ? { room_id: room, date: date } : room;
this.request('GET', '/v1/rooms/history', data, cb);
};
// Get list of rooms.
Rooms.prototype.list = function(cb) {
this.request('GET', '/v1/rooms/list', cb);
};
// Send a message.
Rooms.prototype.message = function(room, name, msg) {
var last = arguments[arguments.length-1];
var cb = (typeof last === 'function') ? last : function(){};
var data = typeof room !== 'object'
? { room_id: room, from: name, message: msg }
: room;
this.request('POST', '/v1/rooms/message', data, cb)
};
// Get detailed room info.
Rooms.prototype.show = function(room, cb) {
var data = typeof room !== 'object' ? { room_id: room } : room;
this.request('GET', '/v1/rooms/show', data, cb);
};
// Add permissions.
Rooms.prototype.addPermissions = function(room, newParticipants) {
var last = arguments[arguments.length-1];
var cb = (typeof last === 'function') ? last : function(){};
var data = typeof room !== 'object'
? { room_id: room, member_user_ids: newParticipants.join(',') }
: room;
this.request('POST', '/v1/rooms/add_permissions', data, cb)
};
// Remove permissions.
Rooms.prototype.removePermissions = function(room, oldParticipants) {
var last = arguments[arguments.length-1];
var cb = (typeof last === 'function') ? last : function(){};
var data = typeof room !== 'object'
? { room_id: room, member_user_ids: oldParticipants.join(',') }
: room;
this.request('POST', '/v1/rooms/remove_permissions', data, cb)
};
/***************
* *
* Users *
* *
***************/
// Create a new user.
Users.prototype.create = function(data, cb) {
this.request('POST', '/v1/users/create', data, cb);
};
// Delete a user.
Users.prototype.delete = function(id, cb) {
var data = typeof id !== 'object' ? { user_id: id } : id;
this.request('POST', '/v1/users/delete', data, cb);
};
// List all user.
Users.prototype.list = function(cb) {
this.request('GET', '/v1/users/list', cb);
};
// Show detailed user info.
Users.prototype.show = function(id, cb) {
var data = typeof id !== 'object' ? { user_id: id } : id;
this.request('GET', '/v1/users/show', data, cb);
};
// Update a user.
Users.prototype.update = function(id, obj, cb) {
var data = obj;
data.user_id = id;
this.request('POST', '/v1/users/update', data, cb);
};
/***************
* *
* Other *
* *
***************/
// Generic request handler for all API sections.
var request = function(method, path, data) {
var query = { format: 'json', auth_token: this.key };
// Merge data into query string variables.
if (method === 'GET' && typeof data === 'object') {
for (var i in data) { query[i] = data[i]; }
}
// Build options object.
var options = {
host: 'api.hipchat.com',
port: 80,
path: path+'?'+qstring.stringify(query),
method: method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
// Make sure we set the content length where needed.
if (method !== 'GET') {
if (typeof data === 'object' || typeof data === 'string') {
if (typeof data !== 'string') {
data = qstring.stringify(data);
}
options.headers['Content-Length'] = data.length;
}
}
var last = arguments[arguments.length-1];
var cb = (typeof last === 'function') ? last : function(){};
// Build request handler.
var req = http.request(options, function(res) {
var body = [];
res.setEncoding('utf8');
res.on('data', body.push.bind(body));
res.on('end', function(){
body = body.join('');
try { body = JSON.parse(body) } catch(e) {}
if (res.statusCode === 200) cb(null, body);
else cb({ code: res.statusCode, message: body });
})
});
// Handle errors
req.on('error', cb);
// Send data, if supplied, and we aren't using GET method.
if (method !== 'GET') {
if (typeof data === 'object' || typeof data === 'string') {
if (typeof data !== 'string') {
data = qstring.stringify(data);
}
req.write(data);
}
}
// Run request.
req.end();
};
// Attach generic request handler to all interfaces.
Rooms.prototype.request = request;
Users.prototype.request = request;
// Export the API intializer.
module.exports = function(key) {
if ( ! key) throw new Error('Auth Token required!');
return {
Rooms: new Rooms(key),
Users: new Users(key)
};
};