Skip to content

Commit

Permalink
Introducing ability for the system to automatically parse all respons…
Browse files Browse the repository at this point in the history
…es into JSON
  • Loading branch information
ecaron committed Sep 11, 2013
1 parent 80cdbb3 commit 4ac1842
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
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();
});
});
});

0 comments on commit 4ac1842

Please sign in to comment.