Some refactoring

This commit is contained in:
goferito 2015-05-24 00:50:48 +02:00
parent 94398b9d45
commit 79098f0c93
3 changed files with 282 additions and 232 deletions

View file

@ -44,6 +44,7 @@ var Bing = function (options) {
this.options = _.extend(defaults, options);
this.searchVertical = function (query, vertical, options, callback) {
if (typeof options === 'function') {
callback = options;
}
@ -58,13 +59,25 @@ var Bing = function (options) {
_.extend(opts, options);
}
var reqUri = opts.rootUri + vertical + "?$format=json&" + qs.stringify({
"Query": "'" + query + "'"
}) + "&$top=" + opts.top + "&$skip=" + opts.skip + (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '') + (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '') + (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '') + (opts.newslocationoverride ? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27" : '') + (opts.market ? "&Market=%27" + opts.market + "%27" : '') + (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '') + (opts.imagefilters ? '&' + qs.stringify({
"ImageFilters": "'" + opts.imagefilters + "'"
}) : '') + (opts.videofilters ? '&' + qs.stringify({
"VideoFilters": "'" + opts.videofilters + "'"
}) : '');
var reqUri = opts.rootUri
+ vertical
+ "?$format=json&" + qs.stringify({ "Query": "'" + query + "'" })
+ "&$top=" + opts.top
+ "&$skip=" + opts.skip
+ (opts.sources ? "&Sources=%27" + opts.sources + "%27" : '')
+ (opts.newssortby ? "&NewsSortBy=%27" + opts.newssortby + "%27" : '')
+ (opts.newscategory ? "&NewsCategory=%27" + opts.newscategory + "%27" : '')
+ (opts.newslocationoverride
? "&NewsLocationOverride=%27" + opts.newslocationoverride + "%27"
: '')
+ (opts.market ? "&Market=%27" + opts.market + "%27" : '')
+ (opts.adult ? "&Adult=%27" + opts.adult + "%27" : '')
+ (opts.imagefilters
? '&' + qs.stringify({ "ImageFilters": "'" + opts.imagefilters + "'" })
: '')
+ (opts.videofilters
? '&' + qs.stringify({ "VideoFilters": "'" + opts.videofilters + "'" })
: '');
request({
uri: reqUri,
@ -174,15 +187,18 @@ Bing.prototype.news = function (query, options, callback) {
* @function
*/
Bing.prototype.video = function (query, options, callback) {
if (options && typeof options === 'object' && options.videofilters && typeof options.videofilters === 'object') {
var filterQuery = '';
var filters = Object.keys(options.videofilters);
filters.map(function (key, i) {
filterQuery += capitalizeFirstLetter(key) + ':';
filterQuery += capitalizeFirstLetter(options.videofilters[key]);
if (i < filters.length - 1)
filterQuery += '+';
});
if (options
&& typeof options === 'object'
&& options.videofilters
&& typeof options.videofilters === 'object') {
var filterQuery = Object.keys(options.videofilters)
.map(function(key){
return capitalise(key) + ':'
+ capitalise(options.videofilters[key]);
})
.join('+');
options.videofilters = filterQuery;
}
this.searchVertical(query, "Video", options, callback);
@ -205,22 +221,35 @@ Bing.prototype.video = function (query, options, callback) {
* @function
*/
Bing.prototype.images = function (query, options, callback) {
if (options && typeof options === 'object' && options.imagefilters && typeof options.imagefilters === 'object') {
var filterQuery = '';
var filters = Object.keys(options.imagefilters);
filters.map(function (key, i) {
filterQuery += capitalizeFirstLetter(key) + ':';
filterQuery += capitalizeFirstLetter(options.imagefilters[key]);
if (i < filters.length - 1)
filterQuery += '+';
});
if (options
&& typeof options === 'object'
&& options.imagefilters
&& typeof options.imagefilters === 'object') {
var filterQuery = Object.keys(options.imagefilters)
.map(function(key){
return capitalise(key) + ':'
+ capitalise(options.imagefilters[key]);
})
.join('+');
options.imagefilters = filterQuery;
}
this.searchVertical(query, "Image", options, callback);
};
function capitalizeFirstLetter(s) {
/**
* Capitalises the first word of the passed string
*
* @param {String} s String to be capitalised
*
* @funtion
*/
function capitalise(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
module.exports = Bing;

View file

@ -58,13 +58,16 @@ describe('Bing', function () {
});
it('should cope with valid responses', function (done) {
app.get('/hello/Web', function (req, res) {
res.status(200).send(JSON.stringify(validWebResponse));
});
var bingClient = bing({
rootUri: 'http://localhost:' + port + '/hello/',
accKey: '123'
});
bingClient.search('xbox', function (error, response, body) {
response.statusCode.should.eql(200);
body.should.eql(validWebResponse);
@ -73,17 +76,21 @@ describe('Bing', function () {
});
it('should cope with errors', function (done) {
// No actual data on what the failure looks like.
var failure = {
message: 'Failed request'
};
app.get('/hello/Image', function (req, res) {
res.status(500).send(failure);
});
var bingClient = bing({
rootUri: 'http://localhost:' + port + '/hello/',
accKey: '123'
});
bingClient.images('xbox', function (error, response, body) {
response.statusCode.should.eql(500);
body.should.eql(JSON.stringify(failure));

View file

@ -13,10 +13,8 @@ if (!accKey) {
}
var Bing = require('../')({
accKey: accKey
});
var should = require('should');
var Bing = require('../')({ accKey: accKey })
, should = require('should')
describe("Bing Search", function () {
@ -39,11 +37,14 @@ describe("Bing Search", function () {
});
it('finds only 5 results', function (done) {
Bing.search('monkey vs frog', {
Bing.search('monkey vs frog',
{
top: 5,
market: 'en-US',
adult: 'Strict'
}, function (err, res, body) {
},
function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
@ -62,14 +63,17 @@ describe("Bing Images", function () {
this.timeout(1000 * 10);
it('finds images with specific options', function (done) {
Bing.images('pizza', {
Bing.images('pizza',
{
top: 3,
adult: 'Off',
imagefilters: {
size: 'small',
color: 'monochrome'
}
}, function (err, res, body) {
},
function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
@ -89,11 +93,14 @@ describe("Bing News", function () {
it('finds news with specific options', function (done) {
Bing.news('ps4', {
Bing.news('ps4',
{
top: 10,
skip: 1,
newsortby: 'Date'
}, function (err, res, body) {
},
function (err, res, body) {
//TODO try unaccepted options like imagefilters
should.not.exist(err);
@ -115,9 +122,16 @@ describe("Bing Video", function () {
it('finds videos with specific options', function (done) {
Bing.video('monkey vs frog', {
top: 10
}, function (err, res, body) {
Bing.video('monkey vs frog',
{
top: 10,
videofilters: {
duration: 'short',
resolution: 'high'
}
},
function (err, res, body) {
should.not.exist(err);
should.exist(res);
should.exist(body);
@ -129,5 +143,5 @@ describe("Bing Video", function () {
done();
});
});
});