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

@ -16,81 +16,94 @@ var request = require('request'),
*/
var Bing = function (options) {
if (!(this instanceof Bing)) return new Bing(options);
if (!(this instanceof Bing)) return new Bing(options);
var defaults = {
var defaults = {
//Bing Search API URI
rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/",
//Bing Search API URI
rootUri: "https://api.datamarket.azure.com/Bing/Search/v1/",
//Account Key
accKey: null,
//Account Key
accKey: null,
//Bing UserAgent
userAgent: 'Bing Search Client for Node.js',
//Bing UserAgent
userAgent: 'Bing Search Client for Node.js',
//Request Timeout
reqTimeout: 5000,
//Request Timeout
reqTimeout: 5000,
// Number of results (limited to 50 by API)
top: 50,
// Number of results (limited to 50 by API)
top: 50,
// Number of skipped results (pagination)
skip: 0
// Number of skipped results (pagination)
skip: 0
};
};
//merge options passed in with defaults
this.options = _.extend(defaults, options);
//merge options passed in with defaults
this.options = _.extend(defaults, options);
this.searchVertical = function (query, vertical, options, callback) {
if (typeof options === 'function') {
callback = options;
}
if (typeof callback != 'function') {
throw "Error: Callback function required!";
}
this.searchVertical = function (query, vertical, options, callback) {
// Create a copy of the options, to avoid permanent overwrites
var opts = JSON.parse(JSON.stringify(this.options));
if (typeof options === 'function') {
callback = options;
}
if (typeof callback != 'function') {
throw "Error: Callback function required!";
}
if (typeof options === 'object') {
_.extend(opts, options);
}
// Create a copy of the options, to avoid permanent overwrites
var opts = JSON.parse(JSON.stringify(this.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 + "'"
}) : '');
if (typeof options === 'object') {
_.extend(opts, options);
}
request({
uri: reqUri,
method: opts.method || "GET",
headers: {
"User-Agent": opts.userAgent
},
auth: {
user: opts.accKey,
pass: opts.accKey
},
timeout: opts.reqTimeout
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 + "'" })
: '');
}, function (err, res, body) {
request({
uri: reqUri,
method: opts.method || "GET",
headers: {
"User-Agent": opts.userAgent
},
auth: {
user: opts.accKey,
pass: opts.accKey
},
timeout: opts.reqTimeout
if (res && res.statusCode !== 200) {
err = new Error(body);
} else {
}, function (err, res, body) {
// Parse body, if body
body = typeof body === 'string' ? JSON.parse(body) : body;
}
if (res && res.statusCode !== 200) {
err = new Error(body);
} else {
callback(err, res, body);
});
};
// Parse body, if body
body = typeof body === 'string' ? JSON.parse(body) : body;
}
callback(err, res, body);
});
};
};
@ -118,7 +131,7 @@ var Bing = function (options) {
* @function
*/
Bing.prototype.web = function (query, options, callback) {
this.searchVertical(query, "Web", options, callback);
this.searchVertical(query, "Web", options, callback);
};
// Alias Bing.search to Bing.web
@ -140,7 +153,7 @@ Bing.prototype.search = Bing.prototype.web;
* @function
*/
Bing.prototype.composite = function (query, options, callback) {
this.searchVertical(query, "Composite", options, callback);
this.searchVertical(query, "Composite", options, callback);
};
/**
@ -157,7 +170,7 @@ Bing.prototype.composite = function (query, options, callback) {
* @function
*/
Bing.prototype.news = function (query, options, callback) {
this.searchVertical(query, "News", options, callback);
this.searchVertical(query, "News", options, callback);
};
/**
@ -174,18 +187,21 @@ 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 += '+';
});
options.videofilters = filterQuery;
}
this.searchVertical(query, "Video", options, callback);
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 += '+';
});
options.imagefilters = filterQuery;
}
this.searchVertical(query, "Image", options, callback);
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) {
return s.charAt(0).toUpperCase() + s.slice(1);
/**
* 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;