Methods

Not all ruby public instance methods are available when building connectors on Workato's SDK framework. Below we go through a list of whitelisted ruby methods which you can use in any block when building your connector.

Furthermore, you'll be able to declare your own reusable methods to use in any block when using your SDK.

Pre-defined Ruby methods

Method Description
each Basic iterator
[1, 2, 3].each { |i| puts i }
group_by Group arrays into sets.
More details here.
headers Add headers to a request
.headers(Authorization: "Bearer HTB674HJK1")
params Add parameter to a request
.params(api_key: "HTB674HJK1")
payload Add payload to a request
.payload(id: "345")
ignored Ignore a comma-separate list of fields
object_definition["user"].ignored("id", "created_at")
only White list a comma-separate of fields
object_definition["user"].only("id", "name")
required Make a comma-separate list of fields required
object_definition["user"].required("id", "created_at")
inject Combine elements in an array using an operation.
More details here.
iso8601 Convert a date/date-time variable to ISO8601 format
map Returns a new array after invoking block on each element
merge Returns a new hash containing merged contents.
More details here.
pluck Select one or more attributes from an array of objects
[
  {"id": 1, "name": "David"},
  {"id": 2, "name": "Peter"}
].pluck("id")
returns [1, 2]
rand Random number between 0 and 1
select Selectively returns elements for which the block returns true.
More details here.
error raise a job error with a user-defined error body.
error("Unable to find Account with ID: 123")
reject Selectively returns elements for which the block returns false. Similar but opposite of select.
More details here.
sort Sort function returning new sorted array.
More details here.
sort_by Sort function returning self.
More details here.
utc Convert Time to UTC timezone.
More details here.
puts ruby version of console.log or stdout, not the same as put method
Any outputs using the puts method shows up in the console log when testing in the code editor. Use this to aid in your debugging.
while while loop statement.
More details here.

This list can and will be expanded constantly, feel free to contact us to update/add to this list.

Reusable Methods

Reusable methods are supported in Workato. Reusable methods help keep your custom adapter code DRY and may be used in any code block.

Such methods are declared using the methods block. This block is a top-level block, similar to triggers and actions.

Sample code snippet

{
  title: "Math",

  connection: {...},

  methods: {
    factorial: lambda do |input|
      number = input[:number]
      if number > 1
        number * call(:factorial, { number: number - 1 })
      else
        number
      end
    end,

    hello: lambda do
      puts "Hello world"
    end
  },

  actions: {
    factorial: {
      input_fields: lambda do
        [
          { name: "number", type: :integer }
        ]
      end,

      execute: lambda do |connection, input|
        { factorial: call(:factorial, { number: input["number"] }) }
      end
    },

    say_hello: {
      execute: lambda do
        call(:hello)
      end
    }
  },

The call method

call(:name, { number: 1 })

Use the call() method to reference a method. This method takes in two parameters:

  1. Method name
    • Use the method name defined. You can use either :method_name (symbol) or "method_name" (string) representations.
  2. Input hash (optional)
    • This is a hash of input variables. Leave blank if your method does not take in an input.

The input hash is passed into the method as an argument, which you may reference as so:

methods: {
  factorial: lambda do |input|
    number = input[:number]
    ...

Recursion

Methods can also be called within method code blocks. This means that a method can be called by another method or by itself. In the case of the factorial example provided above, note that the factorial method has some degree of recursion. Be careful when writing recursive loops by setting failsafes as in the example below:

if number > 1
  number * call(:factorial, { number: number - 1 })
else
  number
end

Testing

The SDK console only allows testing of actions and triggers. To test your reusable method, write an action which encapsulates it.

Summary

Reusable methods are powerful tools that can make development more efficient. It is especially useful when making schema introspection calls within the object_definitions or input_schema lambdas.

Next section

Find out more about how to catch and expose errors that return from the API to users configuring your connector. This makes it much easier to debug actions or triggers using your custom connector. Go to our error handling documentation.

results matching ""

    No results matching ""