Adding images vertical, some refactoring. Adding Gruntfile for jshint, jsdoc, mocha. Adding basic mocha test for sunny-day case.

This commit is contained in:
noodlefrenzy 2014-09-29 08:12:08 -07:00
parent 8be332ad43
commit 5a8dcf8d59
4 changed files with 195 additions and 42 deletions

View file

@ -9,7 +9,12 @@ var request = require('request'),
_ = require('underscore'),
qs = require('querystring');
/**
* @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 );
@ -17,7 +22,7 @@ var Bing = function( options ) {
var defaults = {
//Bing Search API URI
rootUri: "https://api.datamarket.azure.com/Bing/Search/Web",
rootUri: "https://api.datamarket.azure.com/Bing/Search/",
//TODO move the web part, to choose also Images
//Account Key
@ -31,49 +36,82 @@ var Bing = function( options ) {
};
//merge options passed in with defaults
this.options = _.extend(defaults, options)
}
this.options = _.extend(defaults, options);
this.searchVertical = function(query, vertical, callback, options) {
if(typeof callback != 'function') {
throw "Error: Callback function required!";
}
// TODO check if valid options
var opts = this.options;
if (options !== null) {
opts = _.extend(this.options, options);
}
debugger;
var reqUri = opts.rootUri + vertical +
"?$format=json&" +
qs.stringify({ "Query": "'" + query + "'" });
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.
*/
/**
* 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
* @function
*/
Bing.prototype.search = function(query, callback, options) {
this.searchVertical(query, "Web", callback, options);
};
if(typeof callback != 'function') {
throw "Error: Callback function required!";
}
// TODO check if valid options
var opts = this.options;
if(options != null) {
opts = _.extend(this.options, options)
}
var reqUri = opts.rootUri
+ "?$format=json&"
+ qs.stringify({ "Query": "'" + query + "'" })
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);
});
/**
* 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
* @function
*/
Bing.prototype.images = function(query, callback, options) {
this.searchVertical(query, "Image", callback, options);
};
module.exports = Bing;