1 | var api_url = 'https://api.hipchat.com/',
|
2 | qstring = require('querystring');
|
3 |
|
4 | module.exports.create = function(options) {
|
5 | var request = options.request || require ('request');
|
6 |
|
7 | if (!options.api_key){
|
8 | throw 'api_key required';
|
9 | }
|
10 |
|
11 | var errors = {
|
12 | 400: 'Bad request. Please check your data',
|
13 | 401: 'Unauthorized: API KEY not valid',
|
14 | 403: 'You have exceeded the rate limit',
|
15 | 404: 'Not found',
|
16 | 406: 'You requested an invalid content type',
|
17 | 500: 'Server Error',
|
18 | 503: 'The method you requested is currently unavailable (due to maintenance or high load'
|
19 | };
|
20 |
|
21 | var onResponseBuilder = function (callback) {
|
22 | return function (err, response, body) {
|
23 | if (callback) {
|
24 | var err_msg = null;
|
25 | if (err || !response || response.statusCode != 200) {
|
26 | err_msg = err;
|
27 | if (response && errors[response.statusCode]) {
|
28 | err_msg += ' ' + errors[response.statusCode] + '; ' + body;
|
29 | }
|
30 | }
|
31 | callback(err_msg, response ? response.statusCode : null, body);
|
32 | }
|
33 | }
|
34 | };
|
35 |
|
36 | return {
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | 'message' : function (roomId, from, message, notify, callback){
|
46 | var url = api_url + 'v1/rooms/message?format=json&auth_token=' + options.api_key;
|
47 |
|
48 | var data = {
|
49 | room_id : roomId,
|
50 | from : from,
|
51 | message : message,
|
52 | notify : notify
|
53 | };
|
54 |
|
55 | request.post({
|
56 | url: url,
|
57 | headers:{'content-type': 'application/x-www-form-urlencoded'},
|
58 | body: qstring.stringify(data)
|
59 | }, onResponseBuilder(callback));
|
60 | },
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | 'roomInfo' : function (roomId, callback){
|
67 | var url = api_url + 'v2/room/' + roomId + '?format=json&auth_token=' + options.api_key;
|
68 | request.get({url: url, json:true}, onResponseBuilder(callback));
|
69 | }
|
70 | };
|
71 | }; |
\ | No newline at end of file |