-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ud.js
40 lines (33 loc) · 1.03 KB
/
ud.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
'use strict';
const https = require('https');
function ud(request, callback) {
let response = '';
const url = `https://api.urbandictionary.com/v0/define?term=${encodeURIComponent(request)}`;
https.get(url, res => {
if (res.statusCode !== 200) {
const { statusCode, statusMessage } = res;
response = 'Нічого не знайдено\n' +
`_(Status Code: ${statusCode} ${statusMessage})_`;
callback(new Error(response));
return;
}
let body = '';
res.on('data', chunk => {
body += chunk.toString();
});
res.on('end', () => {
const parsed = JSON.parse(body);
if (!parsed.list[0]) {
response = 'Нічого не знайдено';
callback(new Error(response));
} else {
response = '*Визначення:*\n' +
`${parsed.list[0].definition}\n` +
'\n*Приклад використання:*\n' +
`${parsed.list[0].example}`;
callback(null, response);
}
});
});
}
module.exports = { ud };