This guide explains how to create and use constants in rulesets.
Constants are placeholders with a fixed value that you can use throughout a ruleset. They are comparable to const
elements in JavaScript.
Rulecube supports these constant types:
Simple
This type lets you create simple number or string constants for, for example, a fixed discount percentage like 22
or an information message like You are too young to apply for this insurance
.
JSON
This type lets you create a JSON object, such as a table with discount ratings:
Table
This type lets you create a table with rows and columns:
Key | value |
---|---|
External
This type lets you fetch a value from any website or API in the world.
For each type of constant, we give the steps for creating and using it.
Create a ruleset named ProductPrices
.
Create a constant named MsgTooMuch
and set the Constant type to Simple
.
In the Value field, enter This product is scarce; therefore, you are allowed a maximum of 3 units
.
Click Save.
You use a simple constant by just referring to its name in a Rule or Function.
Create a constant named PercentagesJSON
and set the Constant type to JSON
.
↳ Rulecube enables the Value field for JSON content.
In the Value field, enter these key-value pairs:
Click Save.
If you have a JSON file, you only need to create the constant and set Constant type to JSON
. And then drop the JSON file onto the Value field.
Refer to an element from a JSON constant by addressing the constant and key whose value you want:
Below is a short introduction to Contant tables. In the next chapter, you will find a more elaborate explanation with more examples.
Create a constant named PercentagesTable
and set the Constant type to Table
.
↳ Rulecube shows the Constant Table Designer section.
Click Row twice to add extra rows to the table.
In the Constant Table Designer section, enter these keys and corresponding values:
Click Save.
If you have a CSV file that contains your table data, you only need to create the constant and set its Type to Table
. And then drop the CSV file onto the Value field.
Refer to an element from a table constant by addressing the table and key whose value you want:
In addition to explaining how to create and use an external constant, here we also present the External constant data page that lists all the external data constants that you defined.
Create a constant named Product
and set Constant type to External
.
↳ Rulecube shows the External data settings section.
In the URL field, enter the URL of the API you want to query. In this case: https://dummyjson.com/products/1
In the Fetch frequency list, select Daily
. And in the Time (GMT) field, enter 01:00 AM
.
↳ Now, Rulecube re-fetches the value every night at one o'clock.
Click HTTP settings. ↳ The HTTP settings section opens.
In the Method list, select GET
.
Click Fetch. ↳ Rulecube calls the API and shows the JSON response in the Data field:
Click Save.
Since Product
is the constant that contains the JSON response and price
is one of its elements, you can refer to the price
element like this:
Which returns the value 549
.
Likewise, you can refer to other elements:
If you create an external constant, Rulecube adds it to the External constant data page for your reference. This page shows the external constants of all your rulesets, and you open this page by clicking External Data from the top menu. Its content looks something like this:
Data defined in a constant could sometimes be considered "sensitive". For example, you may want to store a username and password, API-key or secret URL in a constant, but you don't want anyone that opens the ruleset to be able to see its value.
For those cases, a constant can be Encrypted. The constant can still be used as you normally would, but it is not visible to anyone that opens the ruleset in Rulecube itself or fetches it via the API.
We must first enable this in the Ruleset itself:
Go to the Version settings of a Ruleset
Check the "Contains sensitive data" checkbox
Now we can encrypt any constant we want:
Open an existing Constant or create a new one
NOTE: Once you save the Ruleset, the encrypted constant can no longer be read. You can only revert this by rolling back to a previous revision from History.
Any simple constant can be converted to an Environment Variable with the following button, found in the top-right of a constant:
This simplifies the process of manually creating an Environment Variable. After clicking the button above, the constant will be useable in other Rulesets as an Environment Variable.
Key | value |
---|---|
Click on the "Encrypt" button:
percentage_1
10
percentage_2
15
percentage_3
20
p1
10
p2
15
p3
20