Day 11 – Global Block

Goal: demonstrate using global block in ruleset

ruleset a60x477 {
  meta {
    name "global-domain-test"
    description <<
      global-domain-test
    >>
    author "Mike Grace"
    logging on
  }

  global {
    // pre block type stuff
    currentDomain = page:url("domain");
    a = "a";
    testArray = [1,2,3,4];
    superData = {
      "cool":"yes",
      "uncool":"no",
      "location":"unknown : )"
    };

    // special global block stuff
    css <<
      h1 {
        font-size: 50px;
      }
    >>;
    emit <|
      alert("hello");
    |>;
    dataset public_timeline <-  "http://twitter.com/statuses/public_timeline.json";
    datasource tsearch <- "http://search.twitter.com/search.json";
  }

  rule tester_rule is active {
    select when pageview ".*"
    {
      notify("currentDomain",currentDomain) with sticky = true;
      notify("a",a) with sticky = true;
      notify("testArray",testArray[0]) with sticky = true;
    }
  }

  rule second_tester {
    select when pageview ".*"
    pre {
      kynetxTweets = datasource:tsearch("?q=kynetx");
      location = superData.pick("$.location");
      stuff =<<
        <h1>#{currentDomain}</h1>
        <p>#{testArray[1]}</p>
        <p>#{location}</p>
      >>;
      timeline = public_timeline;
    }
    {
      append("body", stuff);
      emit <|
        console.log("public twitter timeline");
        console.log(timeline);
        console.log("kynetx tweets");
        console.log(kynetxTweets);
      |>;
    }
  }
}
  • 11-33 global block
  • 13 gets the domain from the event request
  • 15 sets up basic array
  • 16 sets up basic hash
  • 12-20 all stuff that can be done in a pre block of a rule
  • 22-32 stuff that can be done in a global block but not a pre block
  • 23-27 emitted CSS block can only be in the global block (*there are other ways of doing CSS)
  • 28-30 JS emit block can be done as an action in the action block
  • 31 dataset is setup and queried but can’t be accessed raw from within the action block unless it has been accessed in the pre block of a rule -> see line 54
  • 32 datasource query setup but not executed

App run on example.com with bookmarklet

Browser console output on example.com after running app with bookmarklet

  • The global block is executed for each ruleset evaluation so be careful what you put in the global block
  • I think of the global block as being a pre block with special powers and all the rules in my ruleset have access to the global block variables

Get the bookmarklet to try it out yourself!

Gratuitous day 11 crazy face

About these ads
This entry was posted in Kynetx and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s