Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ translate.getLanguages(function(err, languages) {
// ]
}
});

// Promises are also supported by omitting callbacks.
translate.getLanguages().then(function(data) {
var languages = data[0];
});

// It's also possible to integrate with third-party Promise libraries.
var translate = require('@google-cloud/translate')({
promise: require('bluebird')
});
```


Expand Down
2 changes: 1 addition & 1 deletion packages/translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"translate"
],
"dependencies": {
"@google-cloud/common": "^0.6.0",
"@google-cloud/common": "^0.7.0",
"arrify": "^1.0.0",
"extend": "^3.0.0",
"is": "^3.0.1",
Expand Down
33 changes: 31 additions & 2 deletions packages/translate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ function Translate(options) {
* @param {number=} callback.results[].confidence - A float 0 - 1. The higher
* the number, the higher the confidence in language detection. Note, this
* is not always returned from the API.
* @param {string} callback.input - The original input that this was result was
* based on.
* @param {object} callback.apiResponse - Raw API response.
*
* @example
Expand Down Expand Up @@ -142,6 +140,14 @@ function Translate(options) {
* // ]
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* translate.detect('Hello').then(function(data) {
* var results = data[0];
* var apiResponse = data[2];
* });
*/
Translate.prototype.detect = function(input, callback) {
input = arrify(input);
Expand Down Expand Up @@ -237,6 +243,14 @@ Translate.prototype.detect = function(input, callback) {
* // ]
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* translate.getLanguages().then(function(data) {
* var languages = data[0];
* var apiResponse = data[1];
* });
*/
Translate.prototype.getLanguages = function(target, callback) {
if (is.fn(target)) {
Expand Down Expand Up @@ -340,6 +354,14 @@ Translate.prototype.getLanguages = function(target, callback) {
* // ]
* }
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* translate.translate('Hello', 'es').then(function(data) {
* var translation = data[0];
* var apiResponse = data[1];
* });
*/
Translate.prototype.translate = function(input, options, callback) {
input = arrify(input);
Expand Down Expand Up @@ -411,4 +433,11 @@ Translate.prototype.request = function(reqOpts, callback) {
common.util.makeRequest(reqOpts, this.options, callback);
};

/*! Developer Documentation
*
* All async methods (except for streams) will return a Promise in the event
* that a callback is omitted.
*/
common.util.promisifyAll(Translate);

module.exports = Translate;
10 changes: 10 additions & 0 deletions packages/translate/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ var proxyquire = require('proxyquire');
var util = require('@google-cloud/common').util;

var makeRequestOverride;
var promisified = false;
var fakeUtil = extend({}, util, {
makeRequest: function() {
if (makeRequestOverride) {
return makeRequestOverride.apply(null, arguments);
}

return util.makeRequest.apply(null, arguments);
},
promisifyAll: function(Class) {
if (Class.name === 'Translate') {
promisified = true;
}
}
});

Expand All @@ -55,6 +61,10 @@ describe('Translate', function() {
});

describe('instantiation', function() {
it('should promisify all the things', function() {
assert(promisified);
});

it('should normalize the arguments', function() {
var normalizeArguments = fakeUtil.normalizeArguments;
var normalizeArgumentsCalled = false;
Expand Down