Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: extended parsing

...

You will want to thoroughly test your rules before putting this cookbook into production.

Extended parsing

This modification to the rule in the Aurora pico which parses the incoming email message shows how we can extend the mini-language to include event attributes. Basically, lines in the message follow the syntax of standard event URL's starting with the domain portion, so that a line like

     aurora/tempEffect?name=Nemo&duration=5

can result in the equivalent of

     raise aurora event "tempEffect" attributes { "name": "Nemo", "duration": 5 }

which allow events requiring attributes to be triggered by an email message.

Code Block
linenumberstrue
  global {
    newline = (13.chr() + "?" + 10.chr()).as("RegExp")
    var = function(s) {
      p = s.split(re#=#);
      {}.put(p[0],p[1])
    }
    vars = function(s) {
      p = s=>s.split(re#&#)|[];
      p.reduce(function(a,b){a.put(var(b))},{})
    }
    parse = function(text,domain) {
      dlpo = domain.length() + 1;
      text.split(newline)
          .filter(function(v){p=v.split(re#/#);p[0]==domain && p.length()==2})
          .map(function(v){p=v.split(re#[?]#);[p[0].substr(dlpo),vars(p[1])]})
    }
  }
  rule test_test {
    select when test test
    foreach parse(event:attr("text"),"aurora") setting(request)
    pre {
      type = request[0].klog("REQUESTED_TYPE")
      attrs = request[1].klog("REQUESTED_ATTRS")
    }
    if request.length() == 2 then noop()
    fired {
      raise aurora event type attributes attrs;
      ent:lastRequest := "aurora/"+type
    }
  }

As before, line 2 is a pattern matching line breaks, used to split the entire message body in line 13. The function var (lines 3-6) makes a map for a single attribute ("name=Nemo" becomes {"name": "Nemo"}) and is used in line 9. The function vars (lines 7-10) collects all of the attributes into a single map and is used in line 15. The parse function (lines 11-16) first splits the message body into lines, then (line 14) selects only those lines which start with the domain followed by a slash (the length of that leading substring is maintained in dlpo (domain length plus one)), and finally (line 15) collects for each desired message body line an array whose first entry is the event type, and whose second entry is any collected attributes as a map.

The rule loops over the parsed events (line 20), setting the type (line 22) and attributes (line 23) of each, and raises the corresponding events (line 27).