Skip to content

Commit

Permalink
Update code to work with the current google cloud function structure
Browse files Browse the repository at this point in the history
  • Loading branch information
fcappi authored and Fernando Cappi committed Jun 16, 2018
1 parent 69ba534 commit ca9151d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
24 changes: 9 additions & 15 deletions functions/gcs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,16 @@ function getFileStream (file) {
* Reads file and responds with the number of words in the file.
*
* @example
* gcloud alpha functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}'
* gcloud functions call wordCount --data '{"bucket":"YOUR_BUCKET_NAME","name":"sample.txt"}'
*
* @param {object} event The Cloud Functions event.
* @param {object} event.data A Google Cloud Storage File object.
* @param {string} event.data.bucket Name of a Cloud Storage bucket.
* @param {string} event.data.name Name of a file in the Cloud Storage bucket.
* @param {function} callback The callback function.
* @param {Object} req Cloud Function request context.
* @param {object} req.body A Google Cloud Storage File object.
* @param {string} req.body.bucket Name of a Cloud Storage bucket.
* @param {string} req.body.name Name of a file in the Cloud Storage bucket.
* @param {Object} res Cloud Function response context.
*/
exports.wordCount = (event, callback) => {
const file = event.data;

if (file.resourceState === 'not_exists') {
// This is a file deletion event, so skip it
callback();
return;
}
exports.wordCount = (req, res) => {
const file = req.body;

let count = 0;
const options = {
Expand All @@ -67,7 +61,7 @@ exports.wordCount = (event, callback) => {
count += line.trim().split(/\s+/).length;
})
.on('close', () => {
callback(null, `File ${file.name} has ${count} words`);
res.send(`File ${file.name} has ${count} words`);
});
};
// [END functions_word_count_read]
14 changes: 5 additions & 9 deletions functions/gcs/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test.serial(`Fails without a bucket`, (t) => {
const expectedMsg = `Bucket not provided. Make sure you have a "bucket" property in your request`;

t.throws(
() => getSample().program.wordCount({ data: { name: `file` } }),
() => getSample().program.wordCount({ name: `file` }),
Error,
expectedMsg
);
Expand All @@ -63,17 +63,15 @@ test.serial(`Fails without a file`, (t) => {
const expectedMsg = `Filename not provided. Make sure you have a "file" property in your request`;

t.throws(
() => getSample().program.wordCount({ data: { bucket: `bucket` } }),
() => getSample().program.wordCount({ bucket: `bucket` }),
Error,
expectedMsg
);
});

test.cb.serial(`Does nothing for deleted files`, (t) => {
const event = {
data: {
resourceState: `not_exists`
}
resourceState: `not_exists`
};
const sample = getSample();

Expand All @@ -89,10 +87,8 @@ test.cb.serial(`Does nothing for deleted files`, (t) => {
test.cb.serial(`Reads the file line by line`, (t) => {
const expectedMsg = `File ${filename} has 114 words`;
const event = {
data: {
bucket: `bucket`,
name: `sample.txt`
}
bucket: `bucket`,
name: `sample.txt`
};

const sample = getSample();
Expand Down

0 comments on commit ca9151d

Please sign in to comment.