-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
115 lines (106 loc) · 3.3 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
const axios = require('axios');
const striptags = require('striptags');
const _ = require('lodash');
const COMMENT_ANALYZER_URL =
'https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze';
const MAX_LENGTH = 20480;
class PerspectiveAPIClientError extends Error {
constructor(message) {
super(message);
Error.captureStackTrace(this, this.constructor);
this.name = 'PerspectiveAPIClientError';
}
}
class TextEmptyError extends PerspectiveAPIClientError {
constructor() {
super('text must not be empty');
this.name = 'TextEmptyError';
}
}
class TextTooLongError extends PerspectiveAPIClientError {
constructor() {
super(`text must not be greater than ${MAX_LENGTH} characters in length`);
this.name = 'TextTooLongError';
}
}
class ResponseError extends PerspectiveAPIClientError {
constructor(message, response) {
super(message);
this.response = response;
this.name = 'ResponseError';
}
}
class Perspective {
constructor(options) {
this.options = options || {};
if (!this.options.apiKey) {
throw new Error('Must provide options.apiKey');
}
}
analyze(text, options) {
return new Promise((resolve, reject) => {
let resource;
try {
resource = this.getAnalyzeCommentPayload(text, options);
} catch (error) {
reject(error);
}
axios
.post(COMMENT_ANALYZER_URL, resource, {
params: {key: this.options.apiKey},
})
.then(response => {
resolve(response.data);
}).catch(error => {
const message = _.get(error, 'response.data.error.message', error.message);
reject(new ResponseError(message, error.response));
});
});
}
getAnalyzeCommentPayload(text, options) {
const opts = options || {};
const stripHTML = opts.stripHTML == undefined ? true : opts.stripHTML;
const truncate = opts.truncate == undefined ? false : opts.truncate;
const doNotStore = opts.doNotStore == undefined ? true : opts.doNotStore;
const validate = opts.validate == undefined ? true : opts.validate;
const processText = str => {
const ret = stripHTML ? striptags(str) : str;
if (validate && !ret) {
throw new TextEmptyError();
}
if (validate && !truncate && ret.length > MAX_LENGTH) {
throw new TextTooLongError();
}
return truncate ? ret.substr(0, MAX_LENGTH) : ret;
};
let resource = {};
if (typeof text === 'object') {
resource = text;
if (stripHTML && resource.comment.text) {
resource.comment.text = processText(resource.comment.text);
}
} else {
resource.comment = {text: processText(text)};
}
let attributes =
opts.attributes == undefined && !resource.requestedAttributes
? {TOXICITY: {}}
: opts.attributes;
if (Array.isArray(opts.attributes)) {
attributes = {};
opts.attributes.forEach(each => {
attributes[each.toUpperCase()] = {};
});
}
return _.merge({}, resource, {
requestedAttributes: attributes,
doNotStore,
});
}
}
Perspective.PerspectiveAPIClientError = PerspectiveAPIClientError;
Perspective.TextEmptyError = TextEmptyError;
Perspective.TextTooLongError = TextTooLongError;
Perspective.ResponseError = ResponseError;
module.exports = Perspective;