(Classic) Creating a CloudOS Application

Creating The Ruleset

First things first, you need to create a ruleset. You can either do that through the KRL Ruby Gem or through AppBuilder. For everything after this point, I'll be using the KRL Gem, and I suggest you do too.

Once you've installed the KRL Gem installed, run

krl create 'Hello World'

to create an app. It will create a directory with a name corresponding the RID of the new app. A file with RID and a extension will be in the directory.  You can rename the directory to anything you want.  In my case, I changed mine from a41x216 to HelloWorld.

Modifying the Ruleset

Next, modify the ruleset to respond to the CloudOS events. Open your ruleset in your favorite editor. You should be greeted by a ruleset that looks like the following:

ruleset a41x216 {
  meta {
    name "Hello World"
    description <<
      Hello World
    >>
    author ""
    // Uncomment this line to require Marketplace purchase to use this app.
    // authz require user
    logging off
  }
  dispatch {
    // Some example dispatch domains
    // domain "example.com"
    // domain "other.example.com"
  }
  global {
  
  }
  rule first_rule is active {
    select when pageview ".*" setting ()
    // pre {   }
    // notify("Hello World", "This is a sample rule.");
    noop();
  }
}

This is the default ruleset that the KRL command line tool creates. Since we're writing this app to run inside the CloudOS we'll be modifying it quite a bit. If you're not at all familiar with KRL, be sure to read all of the children of the Rulesets page. These will help explain the different parts of the ruleset.

Responding to the cloudAppSelected event

The first task is to remove the placeholder rule (the rule named first_rule). Then create a new rule named HelloWorld:

rule HelloWorld {
 
}

The select statement defines the event conditions under which this rule will be selected. The CloudOS raises the web:cloudAppSelected event whenever a user clicks on an app to run it. So, we want our HelloWorld app to be looking for that event and respond to it. With the select statement added, your rule should look like this:

rule HelloWorld {
 select when web cloudAppSelected
 
}

Rules have preludes for calculating values used later in the rule. Our prelude is merely going to define some HTML to display and bind it to a variable 

rule HelloWorld {
  select when web cloudAppSelected
  pre {
    my_html = <<
      <h5>Hello, World!</h5>
    >>;
  }
}

Everything inside the << >>'s are assigned to the variable my_html.

Next, add an action block. Inside the action block, paste the following three lines:

rule HelloWorld {
 select when web cloudAppSelected
  pre {
    my_html = <<
      <h5>Hello, World!</h5>
    >>;
  }
 {
  SquareTag:inject_styling();
  CloudRain:createLoadPanel("Hello World!", {}, my_html);
 }
}

The first action, inject_styling(), makes the styling of our app compatible with SquareTag. The second, createLoadPanel() creates a panel in the CloudOS dashboard with the title "Hello World!" and loads the HTML we defined in the prelude into the panel we just created. 

These are actions are defined within the SquareTag and CloudRain modules. Note that we have not yet imported these modules. Let's go import those modules now.

Importing the CloudRain and SquareTag modules

KRL modules can be defined and imported into other rulesets. A module is literally just a ruleset that provides functions and actions for you to use. See the Modules page for more info on modules. We have two modules that we are trying to use (CloudRain and SquareTag) that have not been imported yet. To import these two modules, add the following two lines into the meta block:

use module a169x701 alias CloudRain

Commiting and Deploying the Ruleset

After all of these changes, your ruleset should now look like this except that the RID after ruleset will be yours instead of a41x216 (I've also deleted some comments from the code):

ruleset a41x216 {
  meta {
    name "Hello World"
    description <<
      Hello World
    >>
    author ""
    logging off
    use module a169x701 alias CloudRain
  }
  dispatch {
  }
  global {
  }
  rule HelloWorld is active {
    select when web cloudAppSelected
    pre {
      my_html = <<
        <h5>Hello, world!</h5>
      >>;
    }
    {
      CloudRain:createLoadPanel("Hello World!", {}, my_html);
    }
  }
}

You can make any changes to the author and description portions of the meta section that you like. 

Open up your terminal and navigate to the directory that your ruleset is located.

  1. Run krl commit. You should get "Committed version: 1" back.
  2. Run krl deploy. You should get "Deployed version: 1" back from this command. If you did, everything worked correctly!
The only thing that can cause an error at this point is a syntax error. So if there are errors, use the command krl check to check the syntax. It will provide line and character numbers of any syntax errors. 

Running the App

After you've created, commited, and deployed the ruleset, the next step is to run your app!

  1. Log in to SquareTag
  2. Open up your Settings (click on your profile image and click Settings).
  3. Select "myCloud" from the list of settings. Change your Cloud Type from cloudTypePerson to cloudTypeDeveloper. Save your changes. (Note: you only need to set this once. From now on, you can follow the following steps to deploy an app.)
  4. Go to "myApps".
  5. Click the gear in the app panel and select "Add devApp". Enter your app's RID, Name, and upload an image if you want. Save your app.
  6. Go back to "myApps".
  7. Click on your application to run it.

If everything worked, you should see the following:

If you don't see this, check the preceding steps. The information on debugging KRL rulesets might also be helpful. 

Next Steps

Once you've got your CloudOS app running, you might want to know how to make it run when someone scans it. The Creating a SquareTag application tutorial builds on this one to say hello to you every time you scan a tag. 

Copyright Picolabs | Licensed under Creative Commons.