Skip to content

Commit

Permalink
First version, search support.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Oct 21, 2013
1 parent 78da2cf commit e0c9cf6
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
# Node.js client for OSS
# Node.js client for OSS

A Node.js client for [Open Search Server](http://www.open-search-server.com/).

## Usage

```js
var oss = require('node-oss-client'),
client = oss.createClient();

client.search('my_index', {
query: 'my query'
}, function (err, res) {
// ...
});
```
10 changes: 10 additions & 0 deletions examples/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var oss = require('../index'),
client = oss.createClient();

client.search('my_index', {
query: 'my query'
}, function (err, res) {
if (err) return console.log(err);

console.log(res.body);
});
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/oss');
38 changes: 38 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var request = require('request'),
_ = require('lodash'),
urlParser = require('url');

var Client = function (options) {
this.options = _.defaults(options || {}, {
hostname: 'localhost',
port: 9090
});
};

Client.prototype.request = function (options, callback) {

var url = urlParser.format(_(this.options)
.pick(['hostname', 'port', 'protocol'])
.defaults({
protocol: 'http',
pathname: options.pathname
}).value());

request(_(options)
.omit(['hostname', 'port', 'protocol'])
.defaults({
url: url,
json: true
})
.value(), callback);
};


var searchFactory = require('./search');

Client.prototype.search = function () {
searchFactory(this).apply(this, arguments);
};


module.exports = Client;
8 changes: 8 additions & 0 deletions lib/oss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var Client = require('./client');

function createClient(options) {
return new Client(options);
}

exports.Client = Client;
exports.createClient = createClient;
17 changes: 17 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var _ = require('lodash');

function search(client) {
return function (index, options, callback) {
options = _.defaults(options || {}, {
type: 'field'
});

client.request({
pathname: '/services/rest/index/' + index + '/search/' + options.type,
method: 'POST',
json: _.omit(options, 'type')
}, callback);
};
}

module.exports = search;
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "node-oss-client",
"version": "0.1.0",
"description": "Node.js client for Open Search Server.",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git://github.com/lemonde/node-oss-client.git"
},
"keywords": [
"oss",
"open",
"search",
"server"
],
"author": "Greg Bergé <berge.greg@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lemonde/node-oss-client/issues"
},
"dependencies": {
"request": "~2.27.0",
"lodash": "~2.2.1"
},
"devDependencies": {
"mocha": "~1.13.0",
"chai": "~1.8.1",
"nock": "~0.22.1",
"sinon": "~1.7.3",
"sinon-chai": "~2.4.0"
}
}
45 changes: 45 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* globals describe, it, beforeEach */

var expect = require('chai').expect,
nock = require('nock'),
Client = require('../lib/client');

describe('Client', function () {

it('should have default options', function () {
var client = new Client();
expect(client.options).to.have.property('hostname');
expect(client.options).to.have.property('port');
});

describe('#request', function () {

beforeEach(function () {
nock('http://localhost:9090')
.post('/my-path')
.reply(200, {
foo: 'bar'
});
});

it('should be possible to make a request', function (done) {
var client = new Client();

client.request({
pathname: '/my-path',
method: 'POST',
json: {
kung: 'foo'
}
}, function (err, res) {
if (err) return done(err);

expect(res.body).to.deep.equal({
foo: 'bar'
});

done();
});
});
});
});
36 changes: 36 additions & 0 deletions test/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* globals describe, it, beforeEach */

var chai = require('chai').use(require('sinon-chai')),
expect = chai.expect,
sinon = require('sinon'),
searchFactory = require('../lib/search');

describe('Search', function () {

var client, search;

beforeEach(function () {
client = {
request: sinon.spy()
};

search = searchFactory(client);
});

it('should be possible to make a search', function () {

var callback = function () {};

search('my_index', {
query: 'My query'
}, callback);

expect(client.request).to.be.calledWith({
json: {
query: 'My query'
},
method: 'POST',
pathname: '/services/rest/index/my_index/search/field'
}, callback);
});
});

0 comments on commit e0c9cf6

Please sign in to comment.