Being able to write selection expressions that select on an event AND only when a variable’s value matches is really nice. The example app code is a progression of the app shown on day 23
ruleset a60x514 {
meta {
name "select-statement-filtering"
description <<
select-statement-filtering
>>
author "Mike Grace"
logging on
}
rule check_for_multiple_links {
select when pageview ".*"
{
notify("Running","Will show links from tweets when you click on them") with sticky = true;
emit <|
$K(".stream-item").live("click", function() {
// get app object for raising web events
app = KOBJ.get_application("a60x514");
// grab jQuery reference to links
var $links = $K(this).find(".twitter-timeline-link");
// only do the work if there is a link in the tweet
if ($links.length > 0) {
// create array to push links into
var linksArray = [];
// go through links in tweet
$links.each(function() {
linksArray.push( $K(this).text() );
});
// JSON stringify array so it can make it safely through a GET request
var stringifiedLinks = JSON.stringify(linksArray);
// raise event with links
app.raise_event("check_clicked_tweet", {"links":stringifiedLinks});
} // end of check $links length
}); // end of live click listener
|>;
}
}
rule show_links_from_tweet {
select when web check_clicked_tweet
foreach event:param("links").decode() setting (link)
pre {
// check link for supported domain
bitly = link.match(re/bit\.ly/g);
google = link.match(re/goo\.gl/g);
stackoverflow = link.match(re/stackoverflow\.com/g);
// set found domain to for filtered select statements
linkDomain = (bitly) => "bitly" |
(google) => "googl" |
(stackoverflow) => "stackoverflow" | "not supported";
}
fired {
raise explicit event process_twitter_link
with linkDomain = linkDomain
and link = link;
}
}
rule process_bitly_links {
select when explicit process_twitter_link linkDomain "bitly"
pre {
link = event:param("link");
}
{
notify("bit.ly link",link) with sticky = true;
}
}
rule process_google_links {
select when explicit process_twitter_link linkDomain "googl"
pre {
link = event:param("link");
}
{
notify("goo.gl link",link) with sticky = true;
}
}
rule process_stackoverflow_links {
select when explicit process_twitter_link linkDomain "stackoverflow"
pre {
link = event:param("link");
}
{
notify("stackoverflow.com link",link) with sticky = true;
}
}
rule process_all_other_links {
select when explicit process_twitter_link linkDomain "not supported"
pre {
link = event:param("link");
}
{
notify("???","#{link} is not yet a supported domain for this app.") with sticky = true;
}
}
}
- 1-46 code previously explained on day 23
- 53-55 check to see if the link matches any of the domains the app is built to process
- 58 check to see if any of the domains were found in the link and set it to ‘linkDomain’ for the explicit event
- 61.5 action block would go here but is optional – if the action block is left off it defaults to fired for the postlude
- 63-65 raise explicit event of ‘process_twitter_link’ and pass the link and the domain that was found or not found
- 70 run on ‘process_twitter_link’ when the variable linkDomain from the event is ‘bitly’
- 80 run on ‘process_twitter_link’ when the variable linkDomain from the event is ‘googl’
- 90 run on ‘process_twitter_link’ when the variable linkDomain from the event is ‘stackoverflow’
- 100 run on ‘process_twitter_link’ when the variable linkDomain from the event is ‘not supported’
Clicking tweets haphazardly on twitter.com after running the app with a bookmarklet
Get the bookmarklet to try it out yourself!
Gratuitous day 24 Grace face

