diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md index 4d8c95d..213acb5 100644 --- a/README.md +++ b/README.md @@ -1 +1,16 @@ -# Node.js client for OSS \ No newline at end of file +# 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) { + // ... +}); +``` \ No newline at end of file diff --git a/examples/search.js b/examples/search.js new file mode 100644 index 0000000..5981f1c --- /dev/null +++ b/examples/search.js @@ -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); +}); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..f7b5cba --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/oss'); \ No newline at end of file diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..9825c15 --- /dev/null +++ b/lib/client.js @@ -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; \ No newline at end of file diff --git a/lib/oss.js b/lib/oss.js new file mode 100644 index 0000000..da564a9 --- /dev/null +++ b/lib/oss.js @@ -0,0 +1,8 @@ +var Client = require('./client'); + +function createClient(options) { + return new Client(options); +} + +exports.Client = Client; +exports.createClient = createClient; \ No newline at end of file diff --git a/lib/search.js b/lib/search.js new file mode 100644 index 0000000..a5d8d00 --- /dev/null +++ b/lib/search.js @@ -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; diff --git a/package.json b/package.json new file mode 100644 index 0000000..754f0fb --- /dev/null +++ b/package.json @@ -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é ", + "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" + } +} diff --git a/test/client.js b/test/client.js new file mode 100644 index 0000000..3a6980f --- /dev/null +++ b/test/client.js @@ -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(); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/search.js b/test/search.js new file mode 100644 index 0000000..8248e1d --- /dev/null +++ b/test/search.js @@ -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); + }); +}); \ No newline at end of file