-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryManager.js
More file actions
111 lines (95 loc) · 2.66 KB
/
Copy pathDictionaryManager.js
File metadata and controls
111 lines (95 loc) · 2.66 KB
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
'use strict';
const Base = require('./base');
/**
* @desc Can be accessed through `textrazor.dictionary`
*/
class DictionaryManager extends Base {
constructor(apiKey, opts) {
super(apiKey, opts);
}
/**
* @desc Creates a new dictionary
* https://www.textrazor.com/docs/rest#Dictionary
*
* @param {String} dictionary - dictionary id
* @param {Object} [opts] - Hash of dictionary options
* @returns {Promise}
*/
create(dictionary, opts) {
return this._makeRequest('put', `/entities/${dictionary}`, {form: opts});
}
/**
* @desc Lists all existing entity dictionaries
*
* @returns {Promise}
*/
list() {
return this._makeRequest('get', '/entities/');
}
/**
* @desc Gets details about a specific dictionary
*
* @param {String} dictionary - dictionary id
* @returns {*}
*/
get(dictionary) {
return this._makeRequest('get', `/entities/${dictionary}`);
}
/**
* @desc Deletes an entire dictionary
*
* @param {String} dictionary - dictionary id
* @returns {Promise}
*/
delete(dictionary) {
return this._makeRequest('delete', `/entities/${dictionary}`);
}
/**
* @desc Creates a new entity entry in a given dictionary.
* https://www.textrazor.com/docs/rest#DictionaryEntry
*
* @param {String} dictionary - dictionary id
* @param {Array} entityEntries - An array of objects
*
* @returns {Promise}
*/
addEntries(dictionary, entityEntries) {
return this._makeRequest('post', `/entities/${dictionary}/`, {form: entityEntries});
}
/**
* Lists all entries in a given dictionary
*
* @param {String} dictionary - dictionary id
* @param {Number} [pageLimit] - number of items to retrieve for large lists
* @param {Number} [page=0] - page number when using page limit for large lists
* @returns {Promise}
*/
listEntries(dictionary, pageLimit, page = 0) {
let qs = '';
if (typeof pageLimit !== `undefined`) {
qs = `?limit=${pageLimit}&offset=${page}`;
}
return this._makeRequest('get', `/entities/${dictionary}/_all${qs}`);
}
/**
* Gets details about a given dictionary entry
*
* @param {String} dictionary - dictionary id
* @param {String} entity - entity id
* @returns {Promise}
*/
getEntry(dictionary, entity) {
return this._makeRequest('get', `/entities/${dictionary}/${entity}`);
}
/**
* Deletes a given dictionary entry
*
* @param {String} dictionary - dictionary id
* @param {String} entity - entity id
* @returns {Promise}
*/
deleteEntry(dictionary, entity) {
return this._makeRequest('delete', `/entities/${dictionary}/${entity}`);
}
}
module.exports = DictionaryManager;