Force camelCase for search options

This commit is contained in:
goferito 2015-10-07 03:02:35 +02:00
parent 2501d2a637
commit 346c7030a1
3 changed files with 50 additions and 31 deletions

View file

@ -38,7 +38,7 @@ Bing.composite("xbox", {
top: 10, // Number of results (max 15 for news, max 50 if other) top: 10, // Number of results (max 15 for news, max 50 if other)
skip: 3, // Skip first 3 results skip: 3, // Skip first 3 results
sources: "web+news", //Choises are web+image+video+news+spell sources: "web+news", //Choises are web+image+video+news+spell
newssortby: "Date" //Choices are Date, Relevance newsSortBy: "Date" //Choices are Date, Relevance
}, function(error, res, body){ }, function(error, res, body){
console.log(body); console.log(body);
}); });
@ -49,8 +49,8 @@ Bing.composite("xbox", {
Bing.news("xbox", { Bing.news("xbox", {
top: 10, // Number of results (max 15) top: 10, // Number of results (max 15)
skip: 3, // Skip first 3 results skip: 3, // Skip first 3 results
newssortby: "Date", //Choices are: Date, Relevance newsSortBy: "Date", //Choices are: Date, Relevance
newscategory: "rt_Business" // Choices are: newsCategory: "rt_Business" // Choices are:
// rt_Business // rt_Business
// rt_Entertainment // rt_Entertainment
// rt_Health // rt_Health
@ -59,6 +59,7 @@ Bing.news("xbox", {
// rt_US // rt_US
// rt_World // rt_World
// rt_ScienceAndTechnology // rt_ScienceAndTechnology
newsLocationOverride: "US.WA" // Only for en-US market
}, function(error, res, body){ }, function(error, res, body){
console.log(body); console.log(body);
}); });

View file

@ -59,25 +59,37 @@ var Bing = function (options) {
_.extend(opts, options); _.extend(opts, options);
} }
// Use camelCased options
// Note: this translation is needed for compatibility with older versions.
// At some point it could be deprecated and removed in a major version
opts.newsSortBy = opts.newsSortBy || opts.newssortby || null;
opts.newsCategory = opts.newsCategory || opts.newscategory || null;
opts.newsLocationOverride = opts.newsLocationOverride
|| opts.newslocationoverride
|| null;
opts.imageFilters = opts.imageFilters || opts.imagefilters || null;
opts.videoSortBy = opts.videoSortBy || opts.videosortby || null;
opts.videoFilters = opts.videoFilters || opts.videofilters || null;
var reqUri = opts.rootUri var reqUri = opts.rootUri
+ vertical + vertical
+ "?$format=json&" + qs.stringify({ "Query": "'" + query + "'" }) + "?$format=json&" + qs.stringify({ "Query": "'" + query + "'" })
+ "&$top=" + opts.top + "&$top=" + opts.top
+ "&$skip=" + opts.skip + "&$skip=" + opts.skip
+ (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '') + (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '')
+ (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') + (opts.newsSortBy ? "&NewsSortBy=%27" + opts.newsSortBy + "%27" : '')
+ (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') + (opts.newsCategory ? "&NewsCategory=%27" + opts.newsCategory + "%27" : '')
+ (opts.newslocationoverride + (opts.newsLocationOverride
? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" ? "&NewsLocationOverride=%27" + opts.newsLocationOverride + "%27"
: '') : '')
+ (opts.market ? "&Market=%27" + opts.market + "%27" : '') + (opts.market ? "&Market=%27" + opts.market + "%27" : '')
+ (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '')
+ (opts.imagefilters + (opts.imageFilters
? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" }) ? '&' + qs.stringify({ "ImageFilters": "'" + opts.imageFilters + "'" })
: '') : '')
+ (opts.videoSortBy ? "&VideoSortBy=%27" + opts.videoSortBy + "%27" : '') + (opts.videoSortBy ? "&VideoSortBy=%27" + opts.videoSortBy + "%27" : '')
+ (opts.videofilters + (opts.videoFilters
? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" }) ? '&' + qs.stringify({ "VideoFilters": "'" + opts.videoFilters + "'" })
: ''); : '');
request({ request({
@ -188,19 +200,22 @@ Bing.prototype.news = function (query, options, callback) {
* @function * @function
*/ */
Bing.prototype.video = function (query, options, callback) { Bing.prototype.video = function (query, options, callback) {
if (options if (options && typeof options === 'object') {
&& typeof options === 'object'
&& options.videofilters
&& typeof options.videofilters === 'object') {
var filterQuery = Object.keys(options.videofilters) //compatibility with older versions
options.videoFilters = options.videoFilters || options.videofilters || '';
if (options.videoFilters && typeof options.videoFilters === 'object') {
var filterQuery = Object.keys(options.videoFilters)
.map(function(key){ .map(function(key){
return capitalise(key) + ':' return capitalise(key) + ':'
+ capitalise(options.videofilters[key]); + capitalise(options.videoFilters[key]);
}) })
.join('+'); .join('+');
options.videofilters = filterQuery; options.videoFilters = filterQuery;
}
} }
this.searchVertical(query, "Video", options, callback); this.searchVertical(query, "Video", options, callback);
}; };
@ -215,26 +230,29 @@ Bing.prototype.video = function (query, options, callback) {
* @param {Object} options Options to command, allows overriding of * @param {Object} options Options to command, allows overriding of
* rootUri, accKey (Bing API key), * rootUri, accKey (Bing API key),
* userAgent, reqTimeout, top, skip, * userAgent, reqTimeout, top, skip,
* imagefilters * imageFilters
* *
* @param {requestCallback} callback Callback called with (potentially * @param {requestCallback} callback Callback called with (potentially
* json-parsed) response. * json-parsed) response.
* @function * @function
*/ */
Bing.prototype.images = function (query, options, callback) { Bing.prototype.images = function (query, options, callback) {
if (options if (options && typeof options === 'object') {
&& typeof options === 'object'
&& options.imagefilters
&& typeof options.imagefilters === 'object') {
var filterQuery = Object.keys(options.imagefilters) //compatibility with older versions
options.imageFilters = options.imageFilters || options.imagefilters || '';
if (options.imageFilters && typeof options.imageFilters === 'object') {
var filterQuery = Object.keys(options.imageFilters)
.map(function(key){ .map(function(key){
return capitalise(key) + ':' return capitalise(key) + ':'
+ capitalise(options.imagefilters[key]); + capitalise(options.imageFilters[key]);
}) })
.join('+'); .join('+');
options.imagefilters = filterQuery; options.imageFilters = filterQuery;
}
} }
this.searchVertical(query, "Image", options, callback); this.searchVertical(query, "Image", options, callback);
}; };

View file

@ -87,7 +87,7 @@ describe("Bing Images", function () {
{ {
top: 3, top: 3,
adult: 'Off', adult: 'Off',
imagefilters: { imageFilters: {
size: 'small', size: 'small',
color: 'monochrome' color: 'monochrome'
} }
@ -116,11 +116,11 @@ describe("Bing News", function () {
Bing.news('ps4', Bing.news('ps4',
{ {
skip: 1, skip: 1,
newsortby: 'Date' newSortBy: 'Date'
}, },
function (err, res, body) { function (err, res, body) {
//TODO try unaccepted options like imagefilters //TODO try unaccepted options like imageFilters
should.not.exist(err); should.not.exist(err);
should.exist(res); should.exist(res);
@ -144,7 +144,7 @@ describe("Bing Video", function () {
Bing.video('monkey vs frog', Bing.video('monkey vs frog',
{ {
top: 10, top: 10,
videofilters: { videoFilters: {
duration: 'short', duration: 'short',
resolution: 'high' resolution: 'high'
}, },
@ -158,7 +158,7 @@ describe("Bing Video", function () {
body.d.results.should.have.length(10); body.d.results.should.have.length(10);
//TODO try here unaccepted options like imagefilters //TODO try here unaccepted options like imageFilters
done(); done();
}); });