Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New samples for Cloud Functions docs #132

Merged
merged 23 commits into from
Jun 23, 2016
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add fix for property names
  • Loading branch information
jmdobry committed Jun 22, 2016
commit d278df75e8199af60c03e2d100b35b6953cda3b2
18 changes: 18 additions & 0 deletions functions/sendgrid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ function verifyWebhook (authorization) {
}
// [END verifyWebhook]

function fixNames (obj) {
if (Array.isArray(obj)) {
obj.forEach(fixNames);
} else if (obj && typeof obj === 'object') {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var fixedKey = key.replace('-', '_');
if (fixedKey !== key) {
obj[fixedKey] = obj[key];
delete obj[key];
}
}
}
}
}

// [START webhook]
/**
* Receive a webhook from SendGrid.
Expand All @@ -198,6 +214,8 @@ exports.sendgridWebhook = function sendgridWebhook (req, res) {

var events = req.body || [];

fixNames(events);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you documented fixNames above, but a small comment on why you're calling this would be nice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Generate newline-delimite JSON

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// See https://cloud.google.com/bigquery/data-formats#json_format
var json = events.map(function (event) {
Expand Down