Scripting
Ruby
27 min
ruby action the ruby action allows you to execute ruby code directly inside your workflow the code you write is executed as the body of a function , meaning you can use puts to print to standard output logs you can return simple values, arrays, objects, or complex structures ruby actions are ideal for data manipulation , http calls , json/xml parsing , statistics , templating , and custom business logic available gems the following gems are preinstalled and available data manipulation jmespath jsonpath nokogiri http client httpx auth / tokens jwt utilities liquid reverse markdown faker statistics descriptive statistics you can freely require any of these gems inside the ruby action usage examples return a simple value return "hello from ruby!" return a list return \[1, 2, 3, "abc"] return an object return { success true, message "done" } use standard output puts "debug message" return "ok" raise error raise "something went wrong!" http requests (httpx) require "httpx" require "json" resp = httpx get("https //httpbin org/json") raise "bad response" unless resp status == 200 doc = json parse(resp to s, symbolize names true) return { ok true, title doc dig(\ slideshow, \ title) } xml parsing (nokogiri) require "nokogiri" xml = "\<root>\<x>1\</x>\<x>2\</x>\</root>" doc = nokogiri xml(xml) return doc xpath("//x") map(&\ text) extract values (jsonpath) require "jsonpath" doc = { "store" => { "book" => \[ { "price" => 8 95 }, { "price" => 12 99 } ] } } prices = jsonpath new("$ store book\[ ] price") on(doc) return prices max aggregate 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 } string templates (liquid) require "liquid" tpl = liquid template parse("hello, {{ user }}! you have {{ count }} new messages ") output = tpl render("user" => "razvan", "count" => 3) return output html → markdown (reversemarkdown) require "reverse markdown" html = "\<h1>title\</h1>\<p>hello \<strong>world\</strong> \& \<em>friends\</em> \</p>" md = reversemarkdown convert(html) return md statistics require "descriptive statistics" arr = \[1,2,2,3,5,8,13,21] return({ mean arr mean, median arr median, variance arr variance, stdev arr standard deviation }) jwt tokens require "jwt" payload = { user "razvan", exp time now\ to i + 3600 } secret = "my secret" token = jwt encode(payload, secret, "hs256") decoded = jwt decode(token, secret, true, { algorithm "hs256" }) return { token token, decoded decoded } faker – random test data require "faker" return { name faker name name, email faker internet email, city faker address city } jmespath example require "jmespath" data = { users \[ { name "john", age 30 }, { name "mike", age 25 } ] } return jmespath search("users\[?age > `26`] name", data) trim, normalize, and sanitize strings text = " hello world " clean = text strip gsub(/\s+/, " ") return clean remove nils and empty strings from arrays arr = \["a", nil, "", "b", " ", "c"] clean = arr map { |x| x to s strip } reject(&\ empty?) return clean deep clean a hash def deep clean(value) case value when hash value map { |k, v| \[k, deep clean(v)] } to h reject { | k, v| v nil? || v == "" } when array value map { |v| deep clean(v) } reject { |v| v nil? || v == "" } else value is a?(string) ? value strip value end end input = { name " razvan ", email "", tags \[" ruby ", nil, " ", "ai"], details { city " bucharest ", postcode nil } } return deep clean(input) convert keys of a hash to snake case def snake keys(obj) case obj when hash obj map do |k, v| new key = k to s gsub(/(\[a z])/, ' \1') downcase sub(/^ /, '') \[new key, snake keys(v)] end to h when array obj map { |v| snake keys(v) } else obj end end input = { "fullname" => "razvan", "userid" => 123, "address" => { "postalcode" => "050000" } } return snake keys(input) normalize numeric input values = \["10", " 20 5 ", nil, "abc", 7] clean = values map do |v| float(v) rescue nil end compact return clean detect duplicates & uniques arr = \["a", "b", "a", "c", "b"] return { unique arr uniq, duplicates arr tally select { |k,v| v > 1 } keys } best practices always return the final output prefer structured arrays and hashes use puts for logging debug information use the whitelisted gems listed above
