1 | var _ = require('lodash');
|
2 | var check = require('check-types');
|
3 | var verify = check.verify;
|
4 | var rsvp = require('rsvp');
|
5 |
|
6 | function Builder(descriptor, callbacks) {
|
7 | verify.object(descriptor);
|
8 |
|
9 | descriptor = JSON.parse(JSON.stringify(descriptor));
|
10 | var capabilities = descriptor.capabilities = descriptor.capabilities || {};
|
11 | var consumer = capabilities.hipchatApiConsumer = capabilities.hipchatApiConsumer || {};
|
12 | var scopes = consumer.scopes = consumer.scopes || [];
|
13 | var installable = capabilities.installable = capabilities.installable || {};
|
14 | var webhooks = capabilities.webhook = capabilities.webhook || [];
|
15 |
|
16 | var frozen = false;
|
17 | function checkMutable() {
|
18 | if (frozen) throw new Error('Builder API is frozen');
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | process.nextTick(function () {
|
25 | frozen = true;
|
26 | callbacks.ready(descriptor);
|
27 | });
|
28 |
|
29 | return {
|
30 |
|
31 | key: function (key) {
|
32 | checkMutable();
|
33 | verify.string(key);
|
34 | descriptor.key = key;
|
35 | return this;
|
36 | },
|
37 |
|
38 | name: function (name) {
|
39 | checkMutable();
|
40 | verify.string(name);
|
41 | descriptor.name = name;
|
42 | return this;
|
43 | },
|
44 |
|
45 | description: function (description) {
|
46 | checkMutable();
|
47 | verify.string(description);
|
48 | descriptor.description = description;
|
49 | return this;
|
50 | },
|
51 |
|
52 | version: function (version) {
|
53 | checkMutable();
|
54 | verify.string(version);
|
55 | descriptor.version = version;
|
56 | return this;
|
57 | },
|
58 |
|
59 | vendor: function (vendor) {
|
60 | checkMutable();
|
61 | verify.object(vendor);
|
62 | if (vendor.name) {
|
63 | this.vendorName(vendor.name);
|
64 | }
|
65 | if (vendor.url) {
|
66 | this.vendorUrl(vendor.url);
|
67 | }
|
68 | return this;
|
69 | },
|
70 |
|
71 | vendorName: function (vendorName) {
|
72 | checkMutable();
|
73 | verify.string(vendorName);
|
74 | descriptor.vendor = descriptor.vendor || {};
|
75 | descriptor.vendor.name = vendorName;
|
76 | return this;
|
77 | },
|
78 |
|
79 | vendorUrl: function (vendorUrl) {
|
80 | checkMutable();
|
81 | verify.string(vendorUrl);
|
82 | descriptor.vendor = descriptor.vendor || {};
|
83 | descriptor.vendor.url = vendorUrl;
|
84 | return this;
|
85 | },
|
86 |
|
87 | fromName: function (fromName) {
|
88 | checkMutable();
|
89 | verify.string(fromName);
|
90 | consumer.fromName = fromName;
|
91 | return this;
|
92 | },
|
93 |
|
94 | scopes: function (scopeOrScopes) {
|
95 | checkMutable();
|
96 | if (_.isArray(scopeOrScopes)) {
|
97 | consumer.scopes = scopes.concat(scopeOrScopes);
|
98 | } else if (_.isString(scopeOrScopes)) {
|
99 | consumer.scopes = scopes.concat([].slice.call(arguments));
|
100 | } else {
|
101 | throw Error('Unexpected scope type: ' + typeof scopeOrScopes);
|
102 | }
|
103 | return this;
|
104 | },
|
105 |
|
106 | allowRoom: function (value) {
|
107 | checkMutable();
|
108 | installable.allowRoom = !!value;
|
109 | return this;
|
110 | },
|
111 |
|
112 | allowGlobal: function (value) {
|
113 | checkMutable();
|
114 | installable.allowGlobal = !!value;
|
115 | return this;
|
116 | },
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | webhook: function () {
|
129 | checkMutable();
|
130 | var args = [].slice.call(arguments);
|
131 | if (args[0] == null) {
|
132 | return this;
|
133 | }
|
134 |
|
135 | var webhook = {}, callback, patternRe;
|
136 | var objectMode;
|
137 |
|
138 | function fail() {
|
139 | throw new Error('Unexpected argument list: ' + args.join(', '));
|
140 | }
|
141 |
|
142 | function isUrl(str) {
|
143 | return str && (check.webUrl(str) || (_.isString(str) && str.charAt(0) === '/'));
|
144 | }
|
145 |
|
146 | if (_.isString(args[0])) {
|
147 | webhook.event = args[0];
|
148 | } else if (_.isObject(args[0])) {
|
149 | objectMode = true;
|
150 | webhook = args[0];
|
151 | callback = !webhook.url && args[1];
|
152 | } else {
|
153 | fail();
|
154 | }
|
155 | args.shift();
|
156 | if (_.isString(args[0]) && !isUrl(args[0])) {
|
157 | webhook.name = args[0];
|
158 | args.shift();
|
159 | }
|
160 | if (_.isRegExp(args[0])) {
|
161 | patternRe = args[0];
|
162 | webhook.pattern = patternRe;
|
163 | args.shift();
|
164 | }
|
165 | if (!objectMode) {
|
166 | if (isUrl(args[0])) {
|
167 | webhook.url = args[0];
|
168 | } else if (_.isFunction(args[0])) {
|
169 | callback = args[0];
|
170 | } else {
|
171 | fail();
|
172 | }
|
173 | }
|
174 |
|
175 | if (webhook.event !== 'room_message' && webhook.pattern) {
|
176 | throw new Error('Webhook pattern not supported for event type ' + webhook.event);
|
177 | }
|
178 |
|
179 | webhooks.push(webhook);
|
180 | callbacks.addWebhook(webhook, callback);
|
181 |
|
182 | return this;
|
183 | },
|
184 |
|
185 |
|
186 |
|
187 | configurable: function (path, allowAccessToRoomAdmins) {
|
188 | checkMutable();
|
189 | verify.string(path);
|
190 | capabilities.configurable = capabilities.configurable || {};
|
191 | capabilities.configurable.url = path;
|
192 | capabilities.configurable.allowAccessToRoomAdmins = !!allowAccessToRoomAdmins;
|
193 | return this;
|
194 | },
|
195 |
|
196 | glance: function (key, name, icon, target, path) {
|
197 | checkMutable();
|
198 | verify.string(key);
|
199 | check.webUrl(path);
|
200 |
|
201 | if (_.isString(name)) {
|
202 | name = { value : name };
|
203 | }
|
204 |
|
205 | if (_.isString(icon)) {
|
206 | icon = { url: icon, "url@2x": icon };
|
207 | }
|
208 |
|
209 | var glance = {
|
210 | key: key,
|
211 | name: name,
|
212 | target: target,
|
213 | icon: icon,
|
214 | queryUrl: path
|
215 | };
|
216 |
|
217 | capabilities.glance = callbacks.glance || [];
|
218 | capabilities.glance.push(glance);
|
219 |
|
220 | return this;
|
221 | },
|
222 |
|
223 | webPanel: function(key, name, location, path) {
|
224 | checkMutable();
|
225 | verify.string(key);
|
226 | check.webUrl(path);
|
227 |
|
228 | if (_.isString(name)) {
|
229 | name = { value : name };
|
230 | }
|
231 |
|
232 | var webPanel = {
|
233 | key: key,
|
234 | name: name,
|
235 | location: location,
|
236 | url: path
|
237 | };
|
238 |
|
239 | capabilities.webPanel = capabilities.webPanel || [];
|
240 | capabilities.webPanel.push(webPanel);
|
241 |
|
242 | return this;
|
243 | },
|
244 |
|
245 | action: function(key, name, location, target) {
|
246 | checkMutable();
|
247 | verify.string(key);
|
248 | verify.string(target);
|
249 |
|
250 | if (_.isString(name)) {
|
251 | name = { value : name };
|
252 | }
|
253 |
|
254 | var action = {
|
255 | key: key,
|
256 | name: name,
|
257 | location: location,
|
258 | target: target
|
259 | };
|
260 |
|
261 | capabilities.action = capabilities.action || [];
|
262 | capabilities.action.push(action);
|
263 |
|
264 | return this;
|
265 | },
|
266 |
|
267 | externalPage: function(key, name, path) {
|
268 | checkMutable();
|
269 | verify.string(key);
|
270 | check.webUrl(path);
|
271 |
|
272 | if (_.isString(name)) {
|
273 | name = { value : name };
|
274 | }
|
275 |
|
276 | var externalPage = {
|
277 | key: key,
|
278 | name: name,
|
279 | url: path
|
280 | };
|
281 |
|
282 | capabilities.externalPage = capabilities.externalPage || [];
|
283 | capabilities.externalPage.push(externalPage);
|
284 |
|
285 | return this;
|
286 | },
|
287 |
|
288 | dialog: function(key, title, path, options) {
|
289 | checkMutable();
|
290 | verify.string(key);
|
291 | check.webUrl(path);
|
292 |
|
293 | if (_.isString(title)) {
|
294 | title = { value : title };
|
295 | }
|
296 |
|
297 | var dialog = {
|
298 | key: key,
|
299 | title: title,
|
300 | url: path,
|
301 | options: options || {}
|
302 | };
|
303 |
|
304 | capabilities.dialog = capabilities.dialog || [];
|
305 | capabilities.dialog.push(dialog);
|
306 |
|
307 | return this;
|
308 | }
|
309 |
|
310 | };
|
311 | };
|
312 |
|
313 | module.exports = Builder;
|