UNPKG

5.13 kBJavaScriptView Raw
1var _ = require('lodash');
2var check = require('check-types');
3var verify = check.verify;
4var rsvp = require('rsvp');
5
6function Builder(descriptor, callbacks) {
7 verify.object(descriptor);
8 // poor man's deep copy
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 // clients of this builder api only get one turn of the event loop to complete
22 // building operations; after that, the builder freezes and informs its client
23 // that it's ready by emitting the ready event
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 // event, url
119 // event, callback
120 // event, patternRe, url
121 // event, patternRe, callback
122 // event, name, url
123 // event, name, callback
124 // event, name, patternRe, url
125 // event, name, patternRe, callback
126 // object
127 // object, callback (callback only used when no url given)
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 configurable: function (path) {
186 checkMutable();
187 verify.string(path);
188 capabilities.configurable = capabilities.configurable || {};
189 capabilities.configurable.url = path;
190 return this;
191 }
192
193 };
194};
195
196module.exports = Builder;