Making callbacks the last parameter (standard node practice)
This commit is contained in:
parent
1b926b75c7
commit
82cd4e5118
3 changed files with 88 additions and 97 deletions
48
README.md
48
README.md
|
@ -16,34 +16,29 @@ var Bing = require('node-bing-api')({ accKey: "your-account-key" });
|
|||
|
||||
#### Web Search:
|
||||
```js
|
||||
Bing.web("Pizza", function(error, res, body){
|
||||
console.log(body);
|
||||
},
|
||||
{
|
||||
Bing.web("Pizza", {
|
||||
top: 10, // Number of results (max 50)
|
||||
skip: 3, // Skip first 3 results
|
||||
}, function(error, res, body){
|
||||
console.log(body);
|
||||
});
|
||||
```
|
||||
|
||||
#### Composite Search:
|
||||
```js
|
||||
Bing.composite("xbox", function(error, res, body){
|
||||
console.log(body);
|
||||
},
|
||||
{
|
||||
Bing.composite("xbox", {
|
||||
top: 10, // Number of results (max 50)
|
||||
skip: 3, // Skip first 3 results
|
||||
sources: "web+news", //Choises are web+image+video+news+spell
|
||||
newssortby: "Date" //Choices are Date, Relevance
|
||||
}, function(error, res, body){
|
||||
console.log(body);
|
||||
});
|
||||
```
|
||||
|
||||
#### News Search:
|
||||
```js
|
||||
Bing.news("xbox", function(error, res, body){
|
||||
console.log(body);
|
||||
},
|
||||
{
|
||||
Bing.news("xbox", {
|
||||
top: 10, // Number of results (max 50)
|
||||
skip: 3, // Skip first 3 results
|
||||
newssortby: "Date" //Choices are: Date, Relevance
|
||||
|
@ -56,40 +51,40 @@ Bing.news("xbox", function(error, res, body){
|
|||
// rt_US
|
||||
// rt_World
|
||||
// rt_ScienceAndTechnology
|
||||
}, function(error, res, body){
|
||||
console.log(body);
|
||||
});
|
||||
```
|
||||
|
||||
#### Video Search:
|
||||
```js
|
||||
Bing.video("monkey vs frog", function(error, res, body){
|
||||
console.log(body);
|
||||
},
|
||||
{
|
||||
Bing.video("monkey vs frog", {
|
||||
top: 10, // Number of results (max 50)
|
||||
skip: 3, // Skip first 3 result
|
||||
videofilters: {
|
||||
duration: 'short',
|
||||
resolution: 'high'
|
||||
}
|
||||
}, function(error, res, body){
|
||||
console.log(body);
|
||||
});
|
||||
```
|
||||
|
||||
#### Images Search:
|
||||
```js
|
||||
Bing.images("Ninja Turtles", function(error, res, body){
|
||||
Bing.images("Ninja Turtles", {skip: 50}, function(error, res, body){
|
||||
console.log(body);
|
||||
}, {skip: 50});
|
||||
});
|
||||
```
|
||||
Adding filter(s) for the Image Search
|
||||
```js
|
||||
Bing.images("Ninja Turtles", function(error, res, body){
|
||||
console.log(body);
|
||||
},
|
||||
{
|
||||
Bing.images("Ninja Turtles", {
|
||||
imagefilters: {
|
||||
size: 'small',
|
||||
color: 'monochrome'
|
||||
}
|
||||
}, function(error, res, body){
|
||||
console.log(body);
|
||||
});
|
||||
```
|
||||
Accepted filter values:
|
||||
|
@ -105,18 +100,18 @@ Accepted filter values:
|
|||
#### Specify Market
|
||||
Getting spanish results:
|
||||
```js
|
||||
Bing.images("Ninja Turtles", function(error, res, body){
|
||||
Bing.images("Ninja Turtles", {top: 5, market: 'es-ES'}, function(error, res, body){
|
||||
console.log(body);
|
||||
}, {top: 5, market: 'es-ES'});
|
||||
});
|
||||
```
|
||||
[List of Bing Markets](https://msdn.microsoft.com/en-us/library/dd251064.aspx)
|
||||
|
||||
|
||||
#### Adult Filter
|
||||
```js
|
||||
Bing.images('Kim Kardashian', function(error, res, body){
|
||||
Bing.images('Kim Kardashian', {market: 'en-US', adult: 'Strict'}, function(error, res, body){
|
||||
console.log(body.d.results);
|
||||
}, { market: 'en-US', adult: 'Strict'});
|
||||
});
|
||||
```
|
||||
Accepted values: "Off", "Moderate", "Strict".
|
||||
|
||||
|
@ -126,4 +121,3 @@ or videos, but may include sexually explicit text.*
|
|||
|
||||
## License
|
||||
MIT
|
||||
|
||||
|
|
64
lib/bing.js
64
lib/bing.js
|
@ -44,8 +44,10 @@ var Bing = function( options ) {
|
|||
//merge options passed in with defaults
|
||||
this.options = _.extend(defaults, options);
|
||||
|
||||
this.searchVertical = function(query, vertical, callback, options) {
|
||||
|
||||
this.searchVertical = function(query, vertical, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options;
|
||||
}
|
||||
if(typeof callback != 'function') {
|
||||
throw "Error: Callback function required!";
|
||||
}
|
||||
|
@ -53,7 +55,9 @@ var Bing = function( options ) {
|
|||
// Create a copy of the options, to avoid permanent overwrites
|
||||
var opts = JSON.parse(JSON.stringify(this.options));
|
||||
|
||||
_.extend(opts, options);
|
||||
if (typeof options === 'object') {
|
||||
_.extend(opts, options);
|
||||
}
|
||||
|
||||
var reqUri = opts.rootUri
|
||||
+ vertical
|
||||
|
@ -118,16 +122,17 @@ var Bing = function( options ) {
|
|||
*
|
||||
* @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
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
*
|
||||
* @function
|
||||
*/
|
||||
Bing.prototype.web = function(query, callback, options) {
|
||||
this.searchVertical(query, "Web", callback, options);
|
||||
Bing.prototype.web = function(query, options, callback) {
|
||||
this.searchVertical(query, "Web", options, callback);
|
||||
};
|
||||
|
||||
// Alias Bing.search to Bing.web
|
||||
|
@ -140,16 +145,16 @@ Bing.prototype.search = Bing.prototype.web;
|
|||
*
|
||||
* @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,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
* @function
|
||||
*/
|
||||
Bing.prototype.composite = function(query, callback, options) {
|
||||
this.searchVertical(query, "Composite", callback, options);
|
||||
Bing.prototype.composite = function(query, options, callback) {
|
||||
this.searchVertical(query, "Composite", options, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -157,16 +162,16 @@ Bing.prototype.composite = function(query, callback, options) {
|
|||
*
|
||||
* @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,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
* @function
|
||||
*/
|
||||
Bing.prototype.news = function(query, callback, options) {
|
||||
this.searchVertical(query, "News", callback, options);
|
||||
Bing.prototype.news = function(query, options, callback) {
|
||||
this.searchVertical(query, "News", options, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -174,16 +179,17 @@ Bing.prototype.news = function(query, callback, options) {
|
|||
*
|
||||
* @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,
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
* @function
|
||||
*/
|
||||
Bing.prototype.video = function(query, callback, options) {
|
||||
Bing.prototype.video = function(query, options, callback) {
|
||||
if (options
|
||||
&& typeof options === 'object'
|
||||
&& options.videofilters
|
||||
&& typeof options.videofilters === 'object') {
|
||||
var filterQuery = '';
|
||||
|
@ -196,7 +202,7 @@ Bing.prototype.video = function(query, callback, options) {
|
|||
});
|
||||
options.videofilters = filterQuery;
|
||||
}
|
||||
this.searchVertical(query, "Video", callback, options);
|
||||
this.searchVertical(query, "Video", options, callback);
|
||||
};
|
||||
|
||||
|
||||
|
@ -206,17 +212,18 @@ Bing.prototype.video = function(query, callback, options) {
|
|||
*
|
||||
* @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,
|
||||
* imagefilters
|
||||
*
|
||||
* @param {requestCallback} callback Callback called with (potentially
|
||||
* json-parsed) response.
|
||||
* @function
|
||||
*/
|
||||
Bing.prototype.images = function(query, callback, options) {
|
||||
Bing.prototype.images = function(query, options, callback) {
|
||||
if (options
|
||||
&& typeof options === 'object'
|
||||
&& options.imagefilters
|
||||
&& typeof options.imagefilters === 'object') {
|
||||
var filterQuery = '';
|
||||
|
@ -229,7 +236,7 @@ Bing.prototype.images = function(query, callback, options) {
|
|||
});
|
||||
options.imagefilters = filterQuery;
|
||||
}
|
||||
this.searchVertical(query, "Image", callback, options);
|
||||
this.searchVertical(query, "Image", options, callback);
|
||||
};
|
||||
|
||||
function capitalizeFirstLetter(s) {
|
||||
|
@ -237,4 +244,3 @@ function capitalizeFirstLetter(s) {
|
|||
}
|
||||
|
||||
module.exports = Bing;
|
||||
|
||||
|
|
|
@ -6,15 +6,15 @@
|
|||
try{
|
||||
var accKey = require('./secrets').accKey;
|
||||
}
|
||||
catch(e){ console.log(e) }
|
||||
catch(e){ console.log(e); }
|
||||
|
||||
if(!accKey){
|
||||
return console.error("Need to include an access key in your secrets.js");
|
||||
}
|
||||
|
||||
|
||||
var Bing = require('../')({ accKey: accKey})
|
||||
, should = require('should')
|
||||
var Bing = require('../')({ accKey: accKey});
|
||||
var should = require('should');
|
||||
|
||||
|
||||
describe("Bing Search", function(){
|
||||
|
@ -23,7 +23,7 @@ describe("Bing Search", function(){
|
|||
|
||||
it('works without options', function(done){
|
||||
|
||||
Bing.search('nigger vs chink', function(err, res, body){
|
||||
Bing.search('monkey vs frog', function(err, res, body){
|
||||
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
|
@ -37,8 +37,11 @@ describe("Bing Search", function(){
|
|||
});
|
||||
|
||||
it('finds only 5 results', function(done){
|
||||
Bing.search('nigger vs chink', function(err, res, body){
|
||||
|
||||
Bing.search('monkey vs frog', {
|
||||
top: 5,
|
||||
market: 'en-US',
|
||||
adult: 'Strict'
|
||||
}, function(err, res, body){
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
@ -46,11 +49,6 @@ describe("Bing Search", function(){
|
|||
body.d.results.should.have.length(5);
|
||||
|
||||
done();
|
||||
},
|
||||
{
|
||||
top: 5,
|
||||
market: 'en-US',
|
||||
adult: 'Strict'
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -62,9 +60,11 @@ describe("Bing Images", function(){
|
|||
this.timeout(1000 * 10);
|
||||
|
||||
it('finds images with specific options', function(done){
|
||||
Bing.images('pizza',
|
||||
function(err, res, body){
|
||||
|
||||
Bing.images('pizza', {
|
||||
top: 3,
|
||||
adult: 'Off',
|
||||
imagefilters: { size: 'small', color: 'monochrome' }
|
||||
}, function(err, res, body){
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
@ -72,11 +72,6 @@ describe("Bing Images", function(){
|
|||
body.d.results.should.have.length(3);
|
||||
|
||||
done();
|
||||
},
|
||||
{
|
||||
top: 3,
|
||||
adult: 'Off',
|
||||
imagefilters: { size: 'small', color: 'monochrome' }
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -89,7 +84,11 @@ describe("Bing News", function(){
|
|||
|
||||
it('finds news with specific options', function(done){
|
||||
|
||||
Bing.news('ps4', function(err, res, body){
|
||||
Bing.news('ps4', {
|
||||
top: 10,
|
||||
skip: 1,
|
||||
newsortby: 'Date'
|
||||
}, function(err, res, body){
|
||||
//TODO try unaccepted options like imagefilters
|
||||
|
||||
should.not.exist(err);
|
||||
|
@ -99,11 +98,6 @@ describe("Bing News", function(){
|
|||
body.d.results.should.have.length(10);
|
||||
|
||||
done();
|
||||
},
|
||||
{
|
||||
top: 10,
|
||||
skip: 1,
|
||||
newsortby: 'Date'
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -116,8 +110,9 @@ describe("Bing Video", function(){
|
|||
|
||||
it('finds videos with specific options', function(done){
|
||||
|
||||
Bing.video('monkey vs frog', function(err, res, body){
|
||||
|
||||
Bing.video('monkey vs frog', {
|
||||
top: 10
|
||||
}, function(err, res, body){
|
||||
should.not.exist(err);
|
||||
should.exist(res);
|
||||
should.exist(body);
|
||||
|
@ -127,11 +122,7 @@ describe("Bing Video", function(){
|
|||
//TODO try here unaccepted options like imagefilters
|
||||
|
||||
done();
|
||||
},
|
||||
{
|
||||
top: 10
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue