Permalink
Browse files

add support for JSONPAth

  • Loading branch information...
1 parent d4f3ded commit 3525bd2793441ef7e026141e723278bda187e88a @bryanpaluch committed Feb 25, 2013
Showing with 51 additions and 8 deletions.
  1. +18 −5 libs/JsonRules.js
  2. +5 −3 package.json
  3. +28 −0 tests/2.jsonRules.js
View
@@ -1,6 +1,6 @@
var _ = require('underscore');
var async = require('async');
-
+var jsonpath = require('JSONPath').eval;
function JsonRules( options){
var options = options ? options : {};
@@ -65,6 +65,9 @@ JsonRules.prototype._testIf = function(ifstatement, values, cb){
return function(cb){cb(null, values[operand._object])};
else if(_.has(operand, '_value'))
return function(cb){cb(null, operand._value)};
+ else if(_.has(operand, '_JSONPath')){
+ return function(cb){cb(null, jsonpath(values, operand._JSONPath)[0])};
+ }
else if(_.has(operand,'_catalog')){
var fn = self.getWrappedCatalogFn(operand._catalog, values);
if(fn){
@@ -129,6 +132,8 @@ JsonRules.prototype.getWrappedCatalogFn = function(catalog, values){
return values[arg._object];
else if(arg._value)
return arg._value;
+ else if(arg._JSONPath)
+ return jsonpath(values, arg._JSONPath)[0];
else
return null;
});
@@ -155,10 +160,18 @@ JsonRules.prototype.getThen = function(rule, values){
// {_object: foo}
function parse(attribute){
if(_.isString(attribute)){
- var ar = attribute.split('.');
- var obj = {};
- obj[ar[0]] = ar[1];
- return obj;
+ if(attribute.charAt(0) === "$"){
+ //Don't actually parse JSONPath objects
+ var obj = {};
+ obj['_JSONPath'] = attribute;
+ return obj;
+ }
+ else{
+ var ar = attribute.split('.');
+ var obj = {};
+ obj[ar[0]] = ar[1];
+ return obj;
+ }
}else if(_.isObject(attribute)){
return attribute;
}
View
@@ -1,6 +1,6 @@
{
"name": "jsonrules",
- "version": "0.0.4",
+ "version": "0.0.5",
"description": "A rule engine with pure json based syntax and function catalog",
"main": "index.js",
"scripts": {
@@ -14,15 +14,17 @@
"license": "MIT",
"dependencies": {
"async": "~0.2.3",
- "underscore": "~1.4.4"
+ "underscore": "~1.4.4",
+ "JSONPath": "~0.9.1"
},
"gitHead": "220441c1bf795c423c18b5aab0f9def764d408e6",
"directories": {
"test": "tests",
"example": "example"
},
"devDependencies": {
- "mocha": "latest"},
+ "mocha": "latest"
+ },
"keywords": [
"rules",
"json",
View
@@ -61,6 +61,19 @@ var shortCircuitRule = {
args: ['_object.temp']}
}
};
+var jsonPathRule = {
+ ifs: [
+ { operands: [ '$.lastReading.temp', '$.lastReading.setTemp'],
+ test: "<"
+ }
+ ],
+ then:
+ { _catalog: {name: 'setTemp',
+ args: ['$.lastReading.setTemp']}
+ }
+}
+
+
var badFormatIfs =
{ operands: [ '_ovject.relay', '_object.relay'
],
@@ -154,6 +167,21 @@ describe("JsonRules.doRule", function(){
done('error');
});
});
+ it("tests a rule, and callbacks with fn if it passes rule uses JSONPath notation", function(done){
+ var exampleThen = function(word,cb){return cb()}
+ var catalog = new FnCatalog();
+ catalog.addFn(exampleThen, 'setTemp', this);
+ var ruleEngine = new JsonRules({catalog: catalog});
+ var value1= {lastReading:{temp: 60, setTemp: 70}, relay: 1};
+ ruleEngine.doRule(jsonPathRule, value1, function(err, fn){
+ if(fn){
+ fn(function(){
+ done();
+ });
+ }else
+ done('error');
+ });
+ });
it("tests a rule, and callback with no fn if it fails ", function(done){
var exampleThen = function(word){return "example"}
var catalog = new FnCatalog();

0 comments on commit 3525bd2

Please sign in to comment.