forked from guidone/node-red-contrib-quotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquotes.js
50 lines (42 loc) · 1.28 KB
/
quotes.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
var request = require('request');
module.exports = function(RED) {
function Quotes(config) {
RED.nodes.createNode(this, config);
var node = this;
this.source = config.source;
this.markdown = config.markdown;
this.formatQuote = function(text, author, link) {
if (node.markdown) {
return text + '\n' + '*' + author + '*\n' + link;
} else {
return text + '\n' + author + '\n' + link;
}
};
this.on('input', function(msg) {
switch(node.source) {
case 'quotzzy':
request('http://www.quotzzy.co/api/quote', function (error, response, body) {
if (error !== null) {
node.error('Timeout from http://www.quotzzy.co');
return;
}
var json = null;
try {
json = JSON.parse(body);
} catch(e) {
// do nothing
}
// send parsed
if (json !== null) {
msg.payload = node.formatQuote(json.text, json.author.name, json.author.wiki);
node.send(msg);
} else {
node.error('Unable to parse from http://www.quotzzy.co');
}
});
break;
}
});
}
RED.nodes.registerType('quotes', Quotes);
};