Skip to content

Commit

Permalink
reqOpts -> document & encodingType
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Sep 29, 2016
1 parent 1d11051 commit 6d7d1b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 75 deletions.
35 changes: 15 additions & 20 deletions packages/language/src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,34 @@ function Document(language, config) {

this.api = language.api;

// `reqOpts` is the payload passed to each API request. This object is used as
// the default for all API requests made with this Document.
this.reqOpts = {
document: {}
};
this.document = {};

if (config.encoding) {
var encodingType = config.encoding.toUpperCase().replace(/[ -]/g, '');
this.reqOpts.encodingType = encodingType;
this.encodingType = config.encoding.toUpperCase().replace(/[ -]/g, '');
}

if (config.language) {
this.reqOpts.document.language = config.language;
this.document.language = config.language;
}

if (config.type) {
this.reqOpts.document.type = config.type.toUpperCase();
this.document.type = config.type.toUpperCase();

if (this.reqOpts.document.type === 'TEXT') {
this.reqOpts.document.type = 'PLAIN_TEXT';
if (this.document.type === 'TEXT') {
this.document.type = 'PLAIN_TEXT';
}
} else {
// Default to plain text.
this.reqOpts.document.type = 'PLAIN_TEXT';
this.document.type = 'PLAIN_TEXT';
}

if (common.util.isCustomType(content, 'storage/file')) {
this.reqOpts.document.gcsContentUri = format('gs://{bucket}/{file}', {
this.document.gcsContentUri = format('gs://{bucket}/{file}', {
bucket: encodeURIComponent(content.bucket.id),
file: encodeURIComponent(content.id)
});
} else {
this.reqOpts.document.content = content;
this.document.content = content;
}
}

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

var verbose = options.verbose === true;

var doc = this.reqOpts.document;
var encType = this.reqOpts.encodingType;
var doc = this.document;
var encType = this.encodingType;

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

var verbose = options.verbose === true;

var doc = this.reqOpts.document;
var encType = this.reqOpts.encodingType;
var doc = this.document;
var encType = this.encodingType;

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

var verbose = options.verbose === true;

var doc = this.reqOpts.document;
var encType = this.reqOpts.encodingType;
var doc = this.document;
var encType = this.encodingType;

this.api.Language.analyzeSentiment(doc, encType, function(err, resp) {
if (err) {
Expand Down
84 changes: 29 additions & 55 deletions packages/language/test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,70 +70,49 @@ describe('Document', function() {
assert.strictEqual(document.api, LANGUAGE.api);
});

it('should set the correct reqOpts for inline content', function() {
assert.deepEqual(document.reqOpts, {
document: {
content: CONFIG,
type: 'PLAIN_TEXT'
}
it('should set the correct document for inline content', function() {
assert.deepEqual(document.document, {
content: CONFIG,
type: 'PLAIN_TEXT'
});
});

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

assert.deepEqual(document.reqOpts, {
document: {
content: CONFIG,
type: 'PLAIN_TEXT'
},
encodingType: 'UTF8'
});
assert.strictEqual(document.encodingType, 'UTF8');
});

it('should set the correct reqOpts for content with language', function() {
it('should set the correct document for content with language', function() {
var document = new Document(LANGUAGE, {
content: CONFIG,
language: 'EN'
});

assert.deepEqual(document.reqOpts, {
document: {
content: CONFIG,
type: 'PLAIN_TEXT',
language: 'EN'
}
});
assert.strictEqual(document.document.language, 'EN');
});

it('should set the correct reqOpts for content with type', function() {
it('should set the correct document for content with type', function() {
var document = new Document(LANGUAGE, {
content: CONFIG,
type: 'html'
});

assert.deepEqual(document.reqOpts, {
document: {
content: CONFIG,
type: 'HTML'
}
});
assert.strictEqual(document.document.type, 'HTML');
});

it('should set the correct reqOpts for text', function() {
it('should set the correct document for text', function() {
var document = new Document(LANGUAGE, {
content: CONFIG,
type: 'text'
});

assert.deepEqual(document.reqOpts, {
document: {
content: CONFIG,
type: 'PLAIN_TEXT'
}
assert.deepEqual(document.document, {
content: CONFIG,
type: 'PLAIN_TEXT'
});
});

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

assert.deepEqual(document.reqOpts, {
document: {
gcsContentUri: [
'gs://',
encodeURIComponent(file.bucket.id),
'/',
encodeURIComponent(file.id),
].join(''),
type: 'PLAIN_TEXT'
}
});
assert.deepEqual(document.document.gcsContentUri, [
'gs://',
encodeURIComponent(file.bucket.id),
'/',
encodeURIComponent(file.id),
].join(''));
});
});

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

assert.deepEqual(features, {
extractDocumentSentiment: true,
extractEntities: true,
extractSyntax: true
});

assert.strictEqual(encType, document.reqOpts.encodingType);
assert.strictEqual(encType, document.encodingType);

done();
}
};

document.reqOpts.encodingType = 'encoding-type';
document.encodingType = 'encoding-type';
document.annotate(assert.ifError);
});

Expand Down Expand Up @@ -453,13 +427,13 @@ describe('Document', function() {
it('should make the correct API request', function(done) {
document.api.Language = {
analyzeEntities: function(doc, encType) {
assert.strictEqual(doc, document.reqOpts.document);
assert.strictEqual(encType, document.reqOpts.encodingType);
assert.strictEqual(doc, document.document);
assert.strictEqual(encType, document.encodingType);
done();
}
};

document.reqOpts.encodingType = 'encoding-type';
document.encodingType = 'encoding-type';
document.detectEntities(assert.ifError);
});

Expand Down Expand Up @@ -542,13 +516,13 @@ describe('Document', function() {
it('should make the correct API request', function(done) {
document.api.Language = {
analyzeSentiment: function(doc, encType) {
assert.strictEqual(doc, document.reqOpts.document);
assert.strictEqual(encType, document.reqOpts.encodingType);
assert.strictEqual(doc, document.document);
assert.strictEqual(encType, document.encodingType);
done();
}
};

document.reqOpts.encodingType = 'encoding-type';
document.encodingType = 'encoding-type';
document.detectSentiment(assert.ifError);
});

Expand Down

0 comments on commit 6d7d1b6

Please sign in to comment.