-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (55 loc) · 1.4 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
var request = require('request');
var _ = require('lodash');
var endpoints = {
intl: 'https://chat.meatspac.es/add/chat',
fr: 'https://fr.meatspac.es/add/chat',
staging: 'http://chat-staging.meatspac.es/add/chat',
tv: 'https://meatspaces.tv/api/add/show'
};
var Poster = function(options) {
var target = 'tv';
var endpoint = endpoints.tv;
this.sending = false;
function apiKey() {
return {
intl: options.apiKey,
fr: options.frApiKey,
staging: options.stagingApiKey,
tv: options.tvApiKey
}[target];
}
function formData(message, gif) {
var data = {
fingerprint: options.fingerprint,
apiKey: apiKey(),
message: message,
picture: 'data:image/gif;base64,' + gif
};
if (target === 'tv') {
_.extend(data, {
twitter: {
username: options.twitterUsername,
id: options.twitterId
}
});
}
return data;
}
this.setTarget = function(t) {
target = t;
endpoint = endpoints[t] || t;
};
this.send = function(message, gif, callback) {
if (this.sending) {
return callback(new Error('Cannot send two gifs at the same time'));
}
this.sending = true;
request.post(endpoint, {
form: formData(message, gif)
}, function(err, response, body) {
this.sending = false;
callback(err, response, body);
}.bind(this));
};
};
module.exports = Poster;