Skip to content

Instantly share code, notes, and snippets.

@DinoChiesa
Last active August 1, 2018 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DinoChiesa/b20db2bef5f763a1df1a85209a1f8d61 to your computer and use it in GitHub Desktop.
Save DinoChiesa/b20db2bef5f763a1df1a85209a1f8d61 to your computer and use it in GitHub Desktop.
// extractJsonToContextVars.js
// ------------------------------------------------------------------
//
// For a JSON payload, set context vars for all fields.
//
// Use a policy config like this:
//
// <Javascript name='JS-RipJiraResponse' timeLimit='200' >
// <Properties>
// <Property name='prefix'>json</Property>
// <Property name='source'>jiraResponse</Property>
// </Properties>
// <IncludeURL>jsc://walkObj.js</IncludeURL>
// <ResourceURL>jsc://extractJsonToContextVars.js</ResourceURL>
// </Javascript>
//
// created: Tue Mar 7 16:32:14 2017
// last saved: <2018-February-26 19:26:07>
function isValidMessage(sourceMsg) {
var verb = context.getVariable(sourceMsg + '.verb');
if (verb) return true;
// else a response message, check the status code
var statusCode = context.getVariable(sourceMsg + '.status.code') + '';
return (statusCode == '200');
}
var sourceMsg = properties.source || 'message';
if (isValidMessage(sourceMsg)) {
var ctype = context.getVariable(sourceMsg + '.header.content-type');
if (ctype.indexOf("application/json") === 0) {
walkObj(JSON.parse(context.getVariable(sourceMsg + '.content')),
properties.prefix || 'json',
function(name, value) {
context.setVariable(name, value);
});
}
else {
context.setVariable((properties.prefix || 'json') + '.' + sourceMsg + '.error', "not json content");
}
}
else {
context.setVariable((properties.prefix || 'json') + '.' + sourceMsg + '.error', "bad inbound message");
}
// example inbound payload:
//
// {
// "name": "rooms/BBG-BjYE/conversations/qo4A887E/messages/Ko4NQABE",
// "sender": {
// "name": "users/11388443469853495",
// "displayName": "Dino Chiesa",
// "avatarUrl": "https://lh4.googleusercontent.com/ADg/hxfWDAGRhDY/photo.jpg",
// "email": "dchiesa@google.com"
// },
// "createTime": "2017-03-07T23:53:04.504207Z",
// "text": "/jira MGMT-3909"
// };
// result is, context variables set like this:
// json.name = "rooms/BBG-BjYE/conversations/qo4A887E/messages/Ko4NQABE"
// json.sender.name = "users/11388443469853495"
// json.sender.displayName = "Dino Chiesa"
// json.sender.avatarUrl = "https://lh4.googleusercontent.com/ADg/hxfWDAGRhDY/photo.jpg"
// json.sender.email = "dchiesa@google.com"
// json.createTime = "2017-03-07T23:53:04.504207Z"
// json.text = "/jira MGMT-3909"
// walkObj.js
// ------------------------------------------------------------------
//
// created: Mon Feb 26 19:00:50 2018
// last saved: <2018-August-01 16:13:20>
(function (){
'use strict';
var what = Object.prototype.toString;
function walkObj(obj, path, fn) {
if ( ! fn && typeof path == 'function') {
fn = path;
path = 'json';
}
var wo = what.call(obj), p;
path = path || '';
if (wo == "[object Object]") {
Object.keys(obj).forEach(function(key){
var item = obj[key], w = what.call(item);
var pathelts = (path == '')?[] : path.split('.');
pathelts.push(key);
var newpath = pathelts.join('.');
if (w == "[object Object]" || w == "[object Array]") {
walkObj(item, newpath, fn);
}
else {
fn(newpath, item, key, obj);
}
});
}
else if (wo == "[object Array]") {
obj.forEach(function(item, ix){
var w = what.call(item);
var pathelts = path.split('.');
pathelts.push('['+ix+']');
var newpath = pathelts.join('.');
if (w == "[object Object]" || w == "[object Array]") {
walkObj(item, newpath, fn);
}
else {
fn(newpath, item);
}
});
}
else {
var msg = "Unknown object to convert: " + wo + "("+ JSON.stringify(obj, null, 2).slice(0, 34) +")";
//console.log(msg);
throw {error: true, message: msg };
}
}
// export to global scope
var globalScope = (function(){ return this; }).call(null);
if (globalScope)
globalScope.walkObj = walkObj;
if (module) {
module.exports = walkObj;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment