@@ -17,67 +17,66 @@ function IntentoConnector(credentials = {}, debug = false) {
17
17
if ( ! apikey ) {
18
18
console . error ( 'Missing Intento API key' )
19
19
return {
20
- error : 'No Intento API key provided'
20
+ error : 'No Intento API key provided' ,
21
21
}
22
22
}
23
23
24
24
this . options = {
25
25
host : 'api.inten.to' ,
26
26
headers : {
27
- apikey
28
- }
27
+ apikey,
28
+ } ,
29
29
}
30
30
31
31
this . ai = Object . freeze ( {
32
32
text : {
33
33
translate : {
34
- fullfill : function ( context , fn ) {
34
+ fullfill : function ( context , fn ) {
35
35
this . fullfill ( 'translate' , context , fn )
36
36
} . bind ( this ) ,
37
- getProviders : function ( params , fn ) {
37
+ getProviders : function ( params , fn ) {
38
38
this . getProviders ( 'translate' , params , fn )
39
39
} . bind ( this ) ,
40
- withStrategy : function ( strategy , context , fn ) {
40
+ withStrategy : function ( strategy , context , fn ) {
41
41
this . withStrategy ( 'translate' , strategy , context , fn )
42
42
} . bind ( this ) ,
43
43
} ,
44
44
sentiment : {
45
- fullfill : function ( context , fn ) {
45
+ fullfill : function ( context , fn ) {
46
46
this . fullfill ( 'sentiment' , context , fn )
47
47
} . bind ( this ) ,
48
- getProviders : function ( params , fn ) {
48
+ getProviders : function ( params , fn ) {
49
49
this . getProviders ( 'sentiment' , params , fn )
50
50
} . bind ( this ) ,
51
- withStrategy : function ( strategy , context , fn ) {
51
+ withStrategy : function ( strategy , context , fn ) {
52
52
if ( this . debug ) {
53
53
console . warn ( 'Experimental feature' )
54
54
}
55
55
this . withStrategy ( 'sentiment' , strategy , context , fn )
56
56
} . bind ( this ) ,
57
57
} ,
58
58
dictionary : {
59
- fullfill : function ( context , fn ) {
59
+ fullfill : function ( context , fn ) {
60
60
this . fullfill ( 'dictionary' , context , fn )
61
61
} . bind ( this ) ,
62
- getProviders : function ( params , fn ) {
62
+ getProviders : function ( params , fn ) {
63
63
this . getProviders ( 'dictionary' , params , fn )
64
64
} . bind ( this ) ,
65
- withStrategy : function ( strategy , context , fn ) {
65
+ withStrategy : function ( strategy , context , fn ) {
66
66
if ( this . debug ) {
67
67
console . warn ( 'Experimental feature' )
68
68
}
69
69
this . withStrategy ( 'dictionary' , strategy , context , fn )
70
70
} . bind ( this ) ,
71
71
} ,
72
- }
72
+ } ,
73
73
} )
74
74
}
75
75
76
76
module . exports = IntentoConnector
77
77
78
78
module . exports . default = Object . assign ( { } , module . exports )
79
79
80
-
81
80
IntentoConnector . prototype . makeRequest = function ( {
82
81
path = '' ,
83
82
params,
@@ -94,7 +93,9 @@ IntentoConnector.prototype.makeRequest = function({
94
93
}
95
94
}
96
95
if ( data && content ) {
97
- console . warn ( 'Specify either `data` or `content` to pass data to POST request. \n For now `data` will be used.' )
96
+ console . warn (
97
+ 'Specify either `data` or `content` to pass data to POST request. \n For now `data` will be used.'
98
+ )
98
99
}
99
100
const urlParams = querystring . stringify ( params )
100
101
const settings = {
@@ -115,16 +116,15 @@ IntentoConnector.prototype.makeRequest = function({
115
116
req . end ( )
116
117
}
117
118
118
-
119
- IntentoConnector . prototype . fullfill = function ( slug , context = { } , fn ) {
119
+ IntentoConnector . prototype . fullfill = function ( slug , context = { } , fn ) {
120
120
const provider = context . provider
121
121
delete context . provider
122
122
123
123
const content = { context }
124
124
125
125
if ( slug === 'dictionary' ) {
126
126
content . service = {
127
- provider : 'ai.text.dictionary.yandex.dictionary_api.1-0'
127
+ provider : 'ai.text.dictionary.yandex.dictionary_api.1-0' ,
128
128
}
129
129
} else if ( slug === 'sentiment' ) {
130
130
if ( ! provider ) {
@@ -134,13 +134,13 @@ IntentoConnector.prototype.fullfill = function (slug, context = {}, fn) {
134
134
'\nProvider `id` is needed to be specified as a provider'
135
135
)
136
136
console . log ( 'No request will be made' )
137
- this . ai . text . sentiment . getProviders ( function ( err , data ) {
137
+ this . ai . text . sentiment . getProviders ( function ( err , data ) {
138
138
if ( ! err ) {
139
139
console . log ( 'Select one of the provider ids' )
140
140
data . forEach ( ( p , i ) => console . log ( ` ${ i + 1 } . ${ p . id } ` ) )
141
141
}
142
142
} )
143
- return
143
+ return
144
144
}
145
145
146
146
content . service = { provider }
@@ -150,43 +150,45 @@ IntentoConnector.prototype.fullfill = function (slug, context = {}, fn) {
150
150
path : getPath ( slug , this . debug ) ,
151
151
content,
152
152
method : 'POST' ,
153
- fn
153
+ fn,
154
154
} )
155
155
}
156
156
157
-
158
- IntentoConnector . prototype . getProviders = function ( slug , params , fn ) {
157
+ IntentoConnector . prototype . getProviders = function ( slug , params , fn ) {
159
158
if ( params instanceof Function ) {
160
159
fn = params
161
160
params = { }
162
161
}
163
-
162
+
164
163
this . makeRequest ( {
165
164
path : getPath ( slug , this . debug ) ,
166
165
params,
167
166
method : 'GET' ,
168
- fn
167
+ fn,
169
168
} )
170
169
}
171
170
172
-
173
- IntentoConnector . prototype . withStrategy = function ( slug , strategy , context , fn ) {
171
+ IntentoConnector . prototype . withStrategy = function (
172
+ slug ,
173
+ strategy ,
174
+ context ,
175
+ fn
176
+ ) {
174
177
const content = {
175
178
context,
176
179
service : {
177
- bidding : strategy
178
- }
180
+ bidding : strategy ,
181
+ } ,
179
182
}
180
183
181
184
this . makeRequest ( {
182
185
path : getPath ( slug , this . debug ) ,
183
186
content,
184
187
method : 'POST' ,
185
- fn
188
+ fn,
186
189
} )
187
190
}
188
191
189
-
190
192
// helpers
191
193
192
194
const pathBySlug = {
@@ -200,7 +202,9 @@ function getPath(slug, debug) {
200
202
if ( ! path ) {
201
203
path = pathBySlug . translate
202
204
if ( debug ) {
203
- console . error ( `getProviders: Unknown intent ${ slug } . Translate intent will be used` )
205
+ console . error (
206
+ `getProviders: Unknown intent ${ slug } . Translate intent will be used`
207
+ )
204
208
}
205
209
}
206
210
@@ -218,10 +222,10 @@ function defaultCallback(err, data) {
218
222
function response_handler ( response , fn ) {
219
223
response . setEncoding ( 'utf8' )
220
224
let body = ''
221
- response . on ( 'data' , function ( chunk ) {
225
+ response . on ( 'data' , function ( chunk ) {
222
226
body += chunk
223
227
} )
224
- response . on ( 'end' , function ( ) {
228
+ response . on ( 'end' , function ( ) {
225
229
try {
226
230
let data = null
227
231
if ( body . length > 0 ) {
@@ -232,7 +236,7 @@ function response_handler(response, fn) {
232
236
fn ( e , null )
233
237
}
234
238
} )
235
- response . on ( 'error' , function ( e ) {
239
+ response . on ( 'error' , function ( e ) {
236
240
console . log ( 'Error: ' + e . message )
237
241
} )
238
242
}
0 commit comments