We're building an outgoing webhook functionality into our app. We want to enable users who add a webhook to customize the json payload that's sent. To do this, we need a syntax that allows variables to be interpolated into the final payload.
Example: User managed template:
{:id=>"%{id}", :message=>"%{message}", :badge=>"%{badge}"}
Hopeful output:
{
"id": "f1ih5g3",
"message": "Thanks for your help",
"badge": {
"id": "M7nk8ojK",
"name": "Thanks Badge",
"points": 10,
}
}
So, in the above example, there is a main webhook object and the basic variables of id and message are interpolated and returned as strings. But we also want to be able to support nested structures when a reference to a full object is included in the template. When a reference to a full object is included, it should return a json representation of that object.
We have tried using the String#% operator, which works for basic variables, but for nested objects, it results in a stringified version like this:
{
"id": "f1ih5g3",
"message": "Thanks for your help",
"badge": "{:id=>\"M7nk8ojK\", :name=>\"Thanks Badge\", :points=>10}"
}
I've explored RABL and Jsonnet, they don't really seem to support dynamic templates (ie ones managed by users).
ERB syntax would work but seems unsafe as any arbitrary ruby could be included in the template.