diff --git a/lib/bing.js b/lib/bing.js index cdbd0df..0f6ad17 100644 --- a/lib/bing.js +++ b/lib/bing.js @@ -10,107 +10,128 @@ var request = require('request'), qs = require('querystring'); /** - * @param {Object} options Options to all Bing calls, allows overriding of rootUri, - * accKey (Bing API key), userAgent, reqTimeout + * @param {Object} options Options to all Bing calls, allows overriding of + * rootUri, accKey (Bing API key), userAgent, reqTimeout * @returns {Bing} * @constructor */ 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/", - //TODO move the web part, to choose also Images + //Bing Search API URI + rootUri: "https://api.datamarket.azure.com/Bing/Search/", - //Account Key - accKey: null, - - //Bing UserAgent - userAgent: 'Bing Search Client for Node.js', + //Account Key + accKey: null, - //Request Timeout - reqTimeout: 5000 - }; + //Bing UserAgent + userAgent: 'Bing Search Client for Node.js', - //merge options passed in with defaults - this.options = _.extend(defaults, options); + //Request Timeout + reqTimeout: 5000, - this.searchVertical = function(query, vertical, callback, options) { - if(typeof callback != 'function') { - throw "Error: Callback function required!"; - } + // Number of results (limited to 50 by API) + top: 50, - // TODO check if valid options + // Number of skipped results (pagination) + skip: 0 - var opts = this.options; + }; - if (options !== null) { - opts = _.extend(this.options, options); - } + //merge options passed in with defaults + this.options = _.extend(defaults, options); - var reqUri = opts.rootUri + vertical + - "?$format=json&" + - qs.stringify({ "Query": "'" + query + "'" }); + this.searchVertical = function(query, vertical, callback, options) { - request({ - uri: reqUri, - method: opts.method || "GET", - headers: { - "User-Agent": opts.userAgent - }, - auth: { - user: opts.accKey, - pass: opts.accKey - }, - timeout: opts.reqTimeout + if(typeof callback != 'function') { + throw "Error: Callback function required!"; + } - }, function(err, res, body){ + var opts = this.options; - // Parse body, if body - body = typeof body === 'string' ? - JSON.parse(body) : - body; + if (options !== null) { + opts = _.extend(this.options, options); + } - callback(err, res, body); - }); - }; + var reqUri = opts.rootUri + + vertical + + "?$format=json&" + + qs.stringify({ "Query": "'" + query + "'" }) + + "&$top=" + opts.top + + "&$skip=" + opts.skip; + + request({ + uri: reqUri, + method: opts.method || "GET", + headers: { + "User-Agent": opts.userAgent + }, + auth: { + user: opts.accKey, + pass: opts.accKey + }, + timeout: opts.reqTimeout + + }, function(err, res, body){ + + // Parse body, if body + body = typeof body === 'string' + ? JSON.parse(body) + : body; + + callback(err, res, body); + }); + }; }; + /** * @callback requestCallback - * @param {String} error Error evaluates to true when an error has occurred. - * @param {Object} response Response object from the Bing call. - * @param {Object} body JSON of the response. + * @param {String} error Error evaluates to true when an error has occurred. + * @param {Object} response Response object from the Bing call. + * @param {Object} body JSON of the response. */ + /** * Performs a Bing search in the Web vertical. * - * @param {String} query Query term to search for. - * @param {requestCallback} callback Callback called with (potentially json-parsed) response. - * @param {Object} options Options to command, allows overriding of rootUri, - * accKey (Bing API key), userAgent, reqTimeout + * @param {String} query Query term to search for. + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * + * @param {Object} options Options to command, allows overriding + * of rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip * @function */ Bing.prototype.search = function(query, callback, options) { this.searchVertical(query, "Web", callback, options); }; + /** * Performs a Bing search in the Images vertical. * - * @param {String} query Query term to search for. - * @param {requestCallback} callback Callback called with (potentially json-parsed) response. - * @param {Object} options Options to command, allows overriding of rootUri, - * accKey (Bing API key), userAgent, reqTimeout + * @param {String} query Query term to search for. + * + * @param {requestCallback} callback Callback called with (potentially + * json-parsed) response. + * + * @param {Object} options Options to command, allows overriding of + * rootUri, accKey (Bing API key), + * userAgent, reqTimeout, top, skip * @function */ Bing.prototype.images = function(query, callback, options) { this.searchVertical(query, "Image", callback, options); }; + module.exports = Bing; +