Skip to content

Instantly share code, notes, and snippets.

@wehrhaus
Last active August 29, 2015 13:59
Show Gist options
  • Save wehrhaus/10938136 to your computer and use it in GitHub Desktop.
Save wehrhaus/10938136 to your computer and use it in GitHub Desktop.
Filter BazaarVoice Results
// Util for cleaning up the BV Timestamp
var decomposeTimestamp = function (timestamp) {
var month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
d = new Date(timestamp),
component = {};
return component {
day: function () { return d.getDate(); },
month: function (type) { // call month('num') for 1 thru 12 instead of Names
if (type === 'num') {
return d.getMonth() +1;
}
return month[d.getMonth()];
},
year: function () { return d.getFullYear(); }
};
};
// Match Review ProductId with Product Id array
var productIdFilter = function () {
var acceptedIds = [], i, j;
if (arguments[1] instanceof Array) {
acceptedIds = arguments[1];
} else {
for (i = 1; i < arguments.length; i += 1) {
acceptedIds.push(arguments[i]);
}
}
for (j = 0; j < acceptedIds.length; j += 1) {
if (parseInt(arguments[0], 10) === acceptedIds[j]) {
return true;
}
}
return false;
};
$.getJSON('http://stg.api.bazaarvoice.com/data/reviews.json?apiversion=5.4&passkey=PASSKEY', function(data) {
var results = data.Results || {}, o;
for (o in results) {
if (results.hasOwnProperty(o)) {
var r = results[o], // shorthand results
ts = decomposeTimestamp(r.SubmissionTime);
if (productIdFilter(r.ProductId)) {
console.group('Review Item - ProductId: ' + r.ProductId);
console.log('Review Title: ', r.Title);
console.log('Review Rating: ' + r.Rating + ' out of ' + r.RatingRange);
console.log('Review Text: ', r.ReviewText);
console.log('Reviewed on: ' + ts.month() + ' ' + ts.day() + ', ' + ts.year());
console.groupEnd();
}
}
}
// display all Results data, useful for viewing all data that is available to you
return console.log('\n\n\n', data.Results);
});
@wehrhaus
Copy link
Author

Rework so that id's passed in with 00 in front of them won't be considered octal.
Example: 003500046112
SytaxError: octal literals and octal escape sequences are deprecated //--> strict mode
486558794 //--> non-strict mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment