Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ability for the system to parse JSON responses #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var serverOptions = {
host: 'localhost',
port: 9200,
pathPrefix:'optional pathPrefix',
parseJSON: true||false,//default false
secure: true||false,
//Optional basic HTTP Auth
auth: {
Expand Down Expand Up @@ -53,7 +54,7 @@ var qryObj = {

elasticSearchClient.search('my_index_name', 'my_type_name', qryObj)
.on('data', function(data) {
console.log(JSON.parse(data))
console.log(JSON.parse(data)) //Necessary if parseJSON is false
})
.on('done', function(){
//always returns 0 right now
Expand Down
2 changes: 2 additions & 0 deletions lib/elasticsearchclient/calls/elasticSearchCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function ElasticSearchCall(params, options, cb) {
self.defaultMethod = options.defaultMethod || 'GET';
self.auth = options.auth || false;
self.params = params || {};
self.parseJSON = options.parseJSON || false;
self.path = [ options.pathPrefix || '', options.path || ''].join('');
self.timeout = options.timeout || false;
self.callback = cb || false;
Expand Down Expand Up @@ -65,6 +66,7 @@ ElasticSearchCall.prototype.exec = function (cb) {
body += chunk;
});
response.on('end', function () {
if (self.parseJSON) body = JSON.parse(body);
if (typeof self.callback == 'function') {
self.callback(undefined, body);
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "elasticsearchclient",
"description": "A client for Elastic Search",
"keywords": ["elasticsearch", "elastic", "search", "client"],
"version": "0.5.1",
"version": "0.5.2",
"repository": {
"url": ""
},
Expand Down
14 changes: 14 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,18 @@ describe("ElasticSearchClient Core api", function(){
.exec();
});
});

describe('#automatically parse JSON', function() {
it('should automatically parse the JSON response into an object', function(done) {
var serverOptionsWithParseJSON = JSON.parse(JSON.stringify(serverOptions));
serverOptionsWithParseJSON.parseJSON = true;
var elasticSearchClient = new ElasticSearchClient(serverOptionsWithParseJSON;
elasticSearchClient.get(indexName, objName, "sushi")
.on('data', function(data) {
data.should.be.an('object');
done();
})
.exec();
});
});
});