Skip to content

Instantly share code, notes, and snippets.

@aozisik
Last active July 8, 2020 08:17
Show Gist options
  • Save aozisik/144637fa8db8590fa7e13c0ea01f7740 to your computer and use it in GitHub Desktop.
Save aozisik/144637fa8db8590fa7e13c0ea01f7740 to your computer and use it in GitHub Desktop.
Paw dynamically build simple XML string from key-value list

What's this?

We have an API that asks for XML formatted string in the query parameters. The problem is, that's a quite long and ugly string and it even contains some environment variables. This dynamic value extension allows us to generate that XML string from key-value pairs. Enjoy!

Install it

Once you run the commands below, just restart Paw and you should see "XML String" under "Extensions" when you right click any value field.

# Create the extension folder
mkdir ~/Library/Containers/com.luckymarmot.Paw/Data/Library/Application\ Support/com.luckymarmot.Paw/Extensions/com.swiftmade.XmlString

# Actually download the extension there
cd ~/Library/Containers/com.luckymarmot.Paw/Data/Library/Application\ Support/com.luckymarmot.Paw/Extensions/com.swiftmade.XmlString
wget https://gist.githubusercontent.com/aozisik/144637fa8db8590fa7e13c0ea01f7740/raw/f6face41ec23ccdd82a17c1c7f6b519ef388d9c9/XmlString.js
var XmlString = function() {
var wrapInTag = function(tagName, string) {
return "<" + tagName + ">" + string + "</" + tagName + ">";
};
var toTags = function(entries) {
var output = "";
for (var i = 0; i < entries.length; i++) {
if (!entries[i][2]) {
// Don't include in the request
continue;
}
var key = entries[i][0];
var value = entries[i][1];
output += wrapInTag(key, value);
}
return output;
};
// implement the evaluate() method to generate the dynamic value
this.evaluate = function(context) {
const contents = toTags(this.body);
if (!this.root) {
return contents;
}
return wrapInTag(this.root, contents);
};
};
// set the Extension Identifier (must be same as the directory name)
XmlString.identifier = "com.swiftmade.XmlString";
// give a display name to your Dynamic Value
XmlString.title = "XML String";
// link to the Dynamic Value documentation
XmlString.help = "https://luckymarmot.com/paw/doc/";
XmlString.inputs = [
InputField("root", "Root Element", "String"),
InputField("body", "Body", "KeyValueList", {
keyName: "Key",
valueName: "Value",
defaultValue: ""
})
];
// call to register function is required
registerDynamicValueClass(XmlString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment