Day 23 – Decode Function – Convert JSON To Objects

Sometimes you just need to pass JavaScript Objects from the client to the server be it arrays or hashes.

ruleset a60x515 {
  meta {
    name "encode-decode-example"
    description <<
      encode-decode-example
    >>
    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("a60x515");

          // 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)
    {
      notify("#{link}","") with sticky = true;
    }
  }
}
  • 14 let the user know that the app is working
  • 16 watch for clicks on the tweet stream interface
  • 25 only process tweet and raise event if there are links in the tweet
  • 36 convert the JavaScript array of links found in the tweet to a JSON stringified string
  • 39 raise web event to app with stringified array of links with the key of ‘links’
  • 49 select when web event of ‘check_clicked_tweet’ is raised to the app
  • 50 loop through each of the links found in the raised event by converting the JSON to an understandable object using the decode operator and set the current link to ‘link’

After clicking on tweets haphazardly on twitter.com after running the app with a bookmarklet

Get the bookmarklet to try it out yourself!

Gratuitous day 23 Grace face

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

One Response to Day 23 – Decode Function – Convert JSON To Objects

  1. Pingback: Day 24 – Selection Expression Filtering On Variable Value | Kynetx App A Day

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