Skip to content

Commit 6d7d1b6

Browse files
reqOpts -> document & encodingType
1 parent 1d11051 commit 6d7d1b6

2 files changed

Lines changed: 44 additions & 75 deletions

File tree

packages/language/src/document.js

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,39 +70,34 @@ function Document(language, config) {
7070

7171
this.api = language.api;
7272

73-
// `reqOpts` is the payload passed to each API request. This object is used as
74-
// the default for all API requests made with this Document.
75-
this.reqOpts = {
76-
document: {}
77-
};
73+
this.document = {};
7874

7975
if (config.encoding) {
80-
var encodingType = config.encoding.toUpperCase().replace(/[ -]/g, '');
81-
this.reqOpts.encodingType = encodingType;
76+
this.encodingType = config.encoding.toUpperCase().replace(/[ -]/g, '');
8277
}
8378

8479
if (config.language) {
85-
this.reqOpts.document.language = config.language;
80+
this.document.language = config.language;
8681
}
8782

8883
if (config.type) {
89-
this.reqOpts.document.type = config.type.toUpperCase();
84+
this.document.type = config.type.toUpperCase();
9085

91-
if (this.reqOpts.document.type === 'TEXT') {
92-
this.reqOpts.document.type = 'PLAIN_TEXT';
86+
if (this.document.type === 'TEXT') {
87+
this.document.type = 'PLAIN_TEXT';
9388
}
9489
} else {
9590
// Default to plain text.
96-
this.reqOpts.document.type = 'PLAIN_TEXT';
91+
this.document.type = 'PLAIN_TEXT';
9792
}
9893

9994
if (common.util.isCustomType(content, 'storage/file')) {
100-
this.reqOpts.document.gcsContentUri = format('gs://{bucket}/{file}', {
95+
this.document.gcsContentUri = format('gs://{bucket}/{file}', {
10196
bucket: encodeURIComponent(content.bucket.id),
10297
file: encodeURIComponent(content.id)
10398
});
10499
} else {
105-
this.reqOpts.document.content = content;
100+
this.document.content = content;
106101
}
107102
}
108103

@@ -392,8 +387,8 @@ Document.prototype.annotate = function(options, callback) {
392387

393388
var verbose = options.verbose === true;
394389

395-
var doc = this.reqOpts.document;
396-
var encType = this.reqOpts.encodingType;
390+
var doc = this.document;
391+
var encType = this.encodingType;
397392

398393
this.api.Language.annotateText(doc, features, encType, function(err, resp) {
399394
if (err) {
@@ -536,8 +531,8 @@ Document.prototype.detectEntities = function(options, callback) {
536531

537532
var verbose = options.verbose === true;
538533

539-
var doc = this.reqOpts.document;
540-
var encType = this.reqOpts.encodingType;
534+
var doc = this.document;
535+
var encType = this.encodingType;
541536

542537
this.api.Language.analyzeEntities(doc, encType, function(err, resp) {
543538
if (err) {
@@ -602,8 +597,8 @@ Document.prototype.detectSentiment = function(options, callback) {
602597

603598
var verbose = options.verbose === true;
604599

605-
var doc = this.reqOpts.document;
606-
var encType = this.reqOpts.encodingType;
600+
var doc = this.document;
601+
var encType = this.encodingType;
607602

608603
this.api.Language.analyzeSentiment(doc, encType, function(err, resp) {
609604
if (err) {

packages/language/test/document.js

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,70 +70,49 @@ describe('Document', function() {
7070
assert.strictEqual(document.api, LANGUAGE.api);
7171
});
7272

73-
it('should set the correct reqOpts for inline content', function() {
74-
assert.deepEqual(document.reqOpts, {
75-
document: {
76-
content: CONFIG,
77-
type: 'PLAIN_TEXT'
78-
}
73+
it('should set the correct document for inline content', function() {
74+
assert.deepEqual(document.document, {
75+
content: CONFIG,
76+
type: 'PLAIN_TEXT'
7977
});
8078
});
8179

82-
it('should set the correct reqOpts for content with encoding', function() {
80+
it('should set and uppercase the correct encodingType', function() {
8381
var document = new Document(LANGUAGE, {
8482
content: CONFIG,
8583
encoding: 'utf-8'
8684
});
8785

88-
assert.deepEqual(document.reqOpts, {
89-
document: {
90-
content: CONFIG,
91-
type: 'PLAIN_TEXT'
92-
},
93-
encodingType: 'UTF8'
94-
});
86+
assert.strictEqual(document.encodingType, 'UTF8');
9587
});
9688

97-
it('should set the correct reqOpts for content with language', function() {
89+
it('should set the correct document for content with language', function() {
9890
var document = new Document(LANGUAGE, {
9991
content: CONFIG,
10092
language: 'EN'
10193
});
10294

103-
assert.deepEqual(document.reqOpts, {
104-
document: {
105-
content: CONFIG,
106-
type: 'PLAIN_TEXT',
107-
language: 'EN'
108-
}
109-
});
95+
assert.strictEqual(document.document.language, 'EN');
11096
});
11197

112-
it('should set the correct reqOpts for content with type', function() {
98+
it('should set the correct document for content with type', function() {
11399
var document = new Document(LANGUAGE, {
114100
content: CONFIG,
115101
type: 'html'
116102
});
117103

118-
assert.deepEqual(document.reqOpts, {
119-
document: {
120-
content: CONFIG,
121-
type: 'HTML'
122-
}
123-
});
104+
assert.strictEqual(document.document.type, 'HTML');
124105
});
125106

126-
it('should set the correct reqOpts for text', function() {
107+
it('should set the correct document for text', function() {
127108
var document = new Document(LANGUAGE, {
128109
content: CONFIG,
129110
type: 'text'
130111
});
131112

132-
assert.deepEqual(document.reqOpts, {
133-
document: {
134-
content: CONFIG,
135-
type: 'PLAIN_TEXT'
136-
}
113+
assert.deepEqual(document.document, {
114+
content: CONFIG,
115+
type: 'PLAIN_TEXT'
137116
});
138117
});
139118

@@ -156,17 +135,12 @@ describe('Document', function() {
156135
content: file
157136
});
158137

159-
assert.deepEqual(document.reqOpts, {
160-
document: {
161-
gcsContentUri: [
162-
'gs://',
163-
encodeURIComponent(file.bucket.id),
164-
'/',
165-
encodeURIComponent(file.id),
166-
].join(''),
167-
type: 'PLAIN_TEXT'
168-
}
169-
});
138+
assert.deepEqual(document.document.gcsContentUri, [
139+
'gs://',
140+
encodeURIComponent(file.bucket.id),
141+
'/',
142+
encodeURIComponent(file.id),
143+
].join(''));
170144
});
171145
});
172146

@@ -197,21 +171,21 @@ describe('Document', function() {
197171
it('should make the correct API request', function(done) {
198172
document.api.Language = {
199173
annotateText: function(doc, features, encType) {
200-
assert.strictEqual(doc, document.reqOpts.document);
174+
assert.strictEqual(doc, document.document);
201175

202176
assert.deepEqual(features, {
203177
extractDocumentSentiment: true,
204178
extractEntities: true,
205179
extractSyntax: true
206180
});
207181

208-
assert.strictEqual(encType, document.reqOpts.encodingType);
182+
assert.strictEqual(encType, document.encodingType);
209183

210184
done();
211185
}
212186
};
213187

214-
document.reqOpts.encodingType = 'encoding-type';
188+
document.encodingType = 'encoding-type';
215189
document.annotate(assert.ifError);
216190
});
217191

@@ -453,13 +427,13 @@ describe('Document', function() {
453427
it('should make the correct API request', function(done) {
454428
document.api.Language = {
455429
analyzeEntities: function(doc, encType) {
456-
assert.strictEqual(doc, document.reqOpts.document);
457-
assert.strictEqual(encType, document.reqOpts.encodingType);
430+
assert.strictEqual(doc, document.document);
431+
assert.strictEqual(encType, document.encodingType);
458432
done();
459433
}
460434
};
461435

462-
document.reqOpts.encodingType = 'encoding-type';
436+
document.encodingType = 'encoding-type';
463437
document.detectEntities(assert.ifError);
464438
});
465439

@@ -542,13 +516,13 @@ describe('Document', function() {
542516
it('should make the correct API request', function(done) {
543517
document.api.Language = {
544518
analyzeSentiment: function(doc, encType) {
545-
assert.strictEqual(doc, document.reqOpts.document);
546-
assert.strictEqual(encType, document.reqOpts.encodingType);
519+
assert.strictEqual(doc, document.document);
520+
assert.strictEqual(encType, document.encodingType);
547521
done();
548522
}
549523
};
550524

551-
document.reqOpts.encodingType = 'encoding-type';
525+
document.encodingType = 'encoding-type';
552526
document.detectSentiment(assert.ifError);
553527
});
554528

0 commit comments

Comments
 (0)