Authorize-Then-Use Pattern

In the authorize then use pattern, a rule is put in place to check if the app is authorized to take a certain action and, if not, do what is necessary to complete the authorization. What makes this work is using the rule postlude to ensure that the rest of the rules (which presumably rely on the authorization) don't run. Here's an example that needs to know Twitter OAuth has happened before the rest of the rules in the ruleset run:

rule auth {
 select when twitter new_tweet // write so that it's selected for any twitter action
 if(not twitter:authorized()) then 
   twitter:authorize() 
 fired {
   last
 }
}


Notice that this rule only fires if the predicate twitter:authorized() is false. The action, twitter:authorize(), is what initiates the OAuth ceremony.

This rule is placed before any other rules in the ruleset that depend on Twitter being authorized. The postlude of the rule (fired {...}) runs the last statement if the rule fires to ensure that nothing else happens. Of course, if the app is authorized, the rule doesn't fire, the OAuth ceremony is not initiated, the last is never executed, and the remaining rules in the ruleset are evaluated.

Copyright Picolabs | Licensed under Creative Commons.