PROCESIO
OverviewPlatform ActionsIntegrations & DemosMarketplaceCustom ActionsDeveloper’s Guide

Ruby

Runs your own Ruby code inside a process and returns its result, its printed output and any error.

The Ruby action runs a piece of Ruby code that you write, as one step of your process. Use it for data manipulation, JSON and XML parsing, templating, statistics or any custom logic that no ready-made action covers. The value your code returns becomes an output you can use in the next steps.

Add the action

In the Process Designer toolbox, open Scripting and pick Ruby (or type Ruby in the search box).

The Ruby action properties: Code, Timeout, Single Result, List Result, Stdout and Error

Inputs and outputs

FieldDirectionTypeRequiredWhat to put / what you get
CodeInputText (Ruby code)YesYour Ruby code. The value it returns is sent back to the process.
TimeoutInputIntegerNoHow many seconds the code may run, from 10 to 180. Default 60.
Single ResultOutputAnyNoThe value your code returns, when it is not an array: a number, text, true/false or an object (a Ruby hash becomes a JSON object).
List ResultOutputList of anyNoThe value your code returns, when it is an array.
StdoutOutputTextNoEverything your code printed with puts.
ErrorOutputTextNoThe error message when your code fails or runs too long. Empty when the code worked.

How your code is run

Your code runs as the body of a function:

  • return <value> sends the value to Single Result, or to List Result when the value is an array. Only one of the two is filled. As in any Ruby method, the value of the last line is also returned, so 1 + 2 on its own gives 3.
  • puts does not set a result; it writes to Stdout.

Examples

CodeOutput
puts "Debug message" then return "OK"Single Result OK, Stdout Debug message
return [1, 2, 3, "abc"]List Result [1, 2, 3, "abc"]
return { success: true, message: "Done" }Single Result {"success": true, "message": "Done"}
1 + 2Single Result 3
raise "Something went wrong!"Error RuntimeError: Something went wrong!
sleep 15 with Timeout 10Error TimeoutError

Fill a text template (Liquid)

require "liquid"

tpl = Liquid::Template.parse("Hello, {{ user }}! You have {{ count }} new messages.")
return tpl.render("user" => "Ana", "count" => 3)

Single Result: Hello, Ana! You have 3 new messages.

Read and total JSON values (JSONPath)

require "json"
require "jsonpath"

doc = JSON.parse('{"store":{"book":[{"price":8.95},{"price":12.99},{"price":5.50}]}}')
prices = JsonPath.new("$..book[*].price").on(doc)

return { count: prices.size, min: prices.min, max: prices.max, sum: prices.sum }

Single Result: {"count": 3, "min": 5.5, "max": 12.99, "sum": 27.44}

Parse XML (Nokogiri)

require "nokogiri"

doc = Nokogiri::XML("<root><x>1</x><x>2</x></root>")
return doc.xpath("//x").map(&:text)

List Result: ["1", "2"]

Convert HTML to Markdown

require "reverse_markdown"

return ReverseMarkdown.convert("<h1>Title</h1><p>Hello <strong>world</strong></p>")

Single Result: # Title followed by Hello **world**.

Statistics

require "descriptive_statistics"

arr = [1, 2, 2, 3, 5, 8, 13, 21]
return({ mean: arr.mean, median: arr.median })

Single Result: {"mean": 6.875, "median": 4}

Sign and read a JWT

require "jwt"

token = JWT.encode({ user: "ana" }, "my-secret", "HS256")
decoded = JWT.decode(token, "my-secret", true, { algorithm: "HS256" })
return decoded.first

Single Result: {"user": "ana"}

Find duplicates

arr = ["a", "b", "a", "c", "b"]
return { unique: arr.uniq, duplicates: arr.tally.select { |k, v| v > 1 }.keys }

Single Result: {"unique": ["a", "b", "c"], "duplicates": ["a", "b"]}

Use process variables in the code

You can insert process variables into the code. Before the code runs, PROCESIO replaces each variable with its value, as plain text. So:

  • a number variable can be used as it is: total = <your number variable> * 2;
  • a text variable needs quotes around it in your code: name = "<your text variable>".

Good to know

  • A failing script does not stop the process. When your code raises an error or runs longer than Timeout, the step still finishes: Error holds the message and both results are empty. Add a Decisional after the action that checks whether Error is empty.
  • The code runs in a sandbox: it cannot read files from the machine, and the sandbox can block calls to the internet. To call a web API, use Call API or CURL and pass the response to the script.
  • Files: a script cannot take a file directly. Convert the file to text with File To Base64 and pass the text to the script. To create a file from a script, return Base64 text and convert it with Base64 To File.

Available gems

Besides the Ruby standard library (for example json, date, time), you can require these gems:

GemWhat it is for
jsonpathReading values from JSON with JSONPath
nokogiriParsing XML and HTML
jwtCreating and reading JSON Web Tokens
liquidText templates
reverse_markdownConverting HTML to Markdown
descriptive_statisticsMean, median, variance and other statistics
hashdiffComparing two hashes
fakerRandom test data

On this page