Use proper cognitive naming. Allow whatever options
This commit is contained in:
parent
916ceb66f9
commit
e01bd31242
3 changed files with 51 additions and 97 deletions
111
lib/bing.js
111
lib/bing.js
|
@ -30,19 +30,14 @@ var Bing = function (options) {
|
|||
userAgent: 'Bing Search Client for Node.js',
|
||||
|
||||
//Request Timeout
|
||||
reqTimeout: 5000,
|
||||
|
||||
// Number of results (limited to 50 by API)
|
||||
top: 50,
|
||||
|
||||
// Number of skipped results (pagination)
|
||||
skip: 0
|
||||
|
||||
reqTimeout: 5000
|
||||
};
|
||||
|
||||
//merge options passed in with defaults
|
||||
// Merge options passed in with defaults
|
||||
this.options = _.extend(defaults, options);
|
||||
|
||||
|
||||
// Performs the search request for the given vertical
|
||||
this.searchVertical = function (query, vertical, options, callback) {
|
||||
|
||||
if (typeof options === 'function') {
|
||||
|
@ -59,66 +54,49 @@ var Bing = function (options) {
|
|||
_.extend(opts, options);
|
||||
}
|
||||
|
||||
opts.videoSortBy = opts.videoSortBy || opts.videosortby || null;
|
||||
opts.videoFilters = opts.videoFilters || opts.videofilters || null;
|
||||
// Map options and supported variation for old versions with the new names
|
||||
var opMap = {
|
||||
top: 'count',
|
||||
skip: 'offset',
|
||||
videosortby: 'videoSortBy',
|
||||
videofilters: 'videoFilters',
|
||||
adult: 'safeSearch',
|
||||
safesearch: 'safeSearch',
|
||||
market: 'mkt'
|
||||
}
|
||||
Object.keys(opts).forEach(function (opt) {
|
||||
var newOp = opMap[opt]
|
||||
if (newOp) {
|
||||
opts[newOp] = opts[newOp] || opts[opt]
|
||||
delete opts[opt]
|
||||
}
|
||||
})
|
||||
|
||||
var reqUri = opts.rootUri
|
||||
+ vertical
|
||||
+ (vertical == 'spellcheck' ? "?text=" : "?q=") + query
|
||||
+ (opts.top ? "&count=" + opts.top : "")
|
||||
+ (opts.offset ? "&offset=" + opts.skip : "")
|
||||
+ (opts.preContextText ? "&preContextText=" + opts.preContextText : "")
|
||||
+ (opts.mode ? "&mode=" + opts.mode : "")
|
||||
+ (opts.postContextText ? "&postContextText=" + opts.postContextText : "")
|
||||
// + "&Options='" + (opts.options || []).join('%2B') + "'"
|
||||
+ (opts.sources
|
||||
? "&Sources='" + encodeURIComponent(opts.sources) + "'"
|
||||
: '')
|
||||
+ (opts.market ? "&mkt='" + opts.market + "'" : '')
|
||||
+ (opts.adult ? "&safesearch=" + opts.adult : '');
|
||||
var reqUri = opts.rootUri + vertical + "?q=" + query
|
||||
|
||||
// Filter the no-query options (accKey, rootUri, userAgent)
|
||||
var queryOpts = {}
|
||||
|
||||
var ignore = [
|
||||
'spellcheck',
|
||||
'top',
|
||||
'offset',
|
||||
'preContextText',
|
||||
'mode',
|
||||
'postContextText',
|
||||
'sources',
|
||||
'market',
|
||||
'adult',
|
||||
'accKey',
|
||||
'reqTimeout',
|
||||
'rootUri',
|
||||
'skip',
|
||||
'top',
|
||||
'userAgent',
|
||||
'videoFilters',
|
||||
'videoSortBy'
|
||||
];
|
||||
Object.keys(opts).forEach(function (opt) {
|
||||
if (!~Object.keys(defaults).indexOf(opt)) {
|
||||
queryOpts[opt] = opts[opt]
|
||||
}
|
||||
})
|
||||
|
||||
// clone object
|
||||
var newOpts = JSON.parse(JSON.stringify(opts));
|
||||
var qStr = qs.stringify(queryOpts);
|
||||
|
||||
ignore.forEach(function(key) {
|
||||
delete newOpts[key];
|
||||
});
|
||||
|
||||
var qStr = require('querystring').stringify(newOpts);
|
||||
|
||||
reqUri += '&' + qStr;
|
||||
reqUri += qStr ? '&' + qStr : '';
|
||||
|
||||
request({
|
||||
uri: reqUri,
|
||||
method: opts.method || "GET",
|
||||
headers: {
|
||||
"User-Agent": opts.userAgent,
|
||||
"Ocp-Apim-Subscription-Key": opts.accKey
|
||||
"User-Agent": opts.userAgent,
|
||||
"Ocp-Apim-Subscription-Key": opts.accKey
|
||||
},
|
||||
timeout: opts.reqTimeout,
|
||||
pool: {
|
||||
maxSockets: opts.maxSockets ? opts.maxSockets : Infinity
|
||||
maxSockets: opts.maxSockets ? opts.maxSockets : Infinity
|
||||
}
|
||||
|
||||
}, function (err, res, body) {
|
||||
|
@ -127,8 +105,8 @@ var Bing = function (options) {
|
|||
err = new Error(body);
|
||||
} else {
|
||||
|
||||
// Parse body, if body
|
||||
body = typeof body === 'string' ? JSON.parse(body) : body;
|
||||
// Parse body, if body
|
||||
body = typeof body === 'string' ? JSON.parse(body) : body;
|
||||
}
|
||||
|
||||
callback(err, res, body);
|
||||
|
@ -153,7 +131,7 @@ var Bing = function (options) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip
|
||||
* userAgent, reqTimeout, count, offset
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -176,7 +154,7 @@ Bing.prototype.search = Bing.prototype.web;
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -193,7 +171,7 @@ Bing.prototype.composite = function (query, options, callback) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -210,7 +188,7 @@ Bing.prototype.news = function (query, options, callback) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -246,7 +224,7 @@ Bing.prototype.video = function (query, options, callback) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding of
|
||||
* rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
* imageFilters
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
|
@ -282,7 +260,7 @@ Bing.prototype.images = function (query, options, callback) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -300,7 +278,7 @@ Bing.prototype.relatedSearch = function (query, options, callback) {
|
|||
*
|
||||
* @param {Object} options Options to command, allows overriding
|
||||
* of rootUri, accKey (Bing API key),
|
||||
* userAgent, reqTimeout, top, skip,
|
||||
* userAgent, reqTimeout, count, offset,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
|
@ -317,7 +295,7 @@ Bing.prototype.spelling = function (query, options, callback) {
|
|||
*
|
||||
* @param {String} s String to be capitalised
|
||||
*
|
||||
* @funtion
|
||||
* @function
|
||||
*/
|
||||
function capitalise(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
|
@ -325,3 +303,4 @@ function capitalise(s) {
|
|||
|
||||
|
||||
module.exports = Bing;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue