MooTools TwitterGitter and Cookies

By  on  
TwitterGitter

One of my MooTools plugins I use most is TwitterGitter. TwitterGitter is a small MooTools plugin I've created to fetch any number of tweets from an Twitter user's account. Since clients don't tweet very often, there's no advantage to pinging Twitter for a tweet every page load. To save Twitter a bunch of repetitive requests, I use cookies to save the latest tweet.

The MooTools JavaScript

var tweet = '';
if(!Cookie.read('latestTweet')) {
	var myTwitterGitter = new TwitterGitter(init.twitterHandle,{
		count: 1,
		onComplete: function(tweets,user) {
			tweets.each(function(tweet,i) {
				if(tweet.text) {
					Cookie.write('latestTweet',tweet.text, { duration: 1 });
					tweet = tweet.text;
				}
			});
		}
	}).retrieve();
}
else {
	tweet = Cookie.read('latestTweet');
}

Pretty simple: if the cookie is there and fresh, use the cookie's value as the tweet; if not, go get the tweet from Twitter. Of course the most optimal method of saving tweets is caching them server-side but if you use TwitterGitter, I recommend using cookies to avoid unnecessary pings to Twitter.

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Conquering Impostor Syndrome

    Two years ago I documented my struggles with Imposter Syndrome and the response was immense.  I received messages of support and commiseration from new web developers, veteran engineers, and even persons of all experience levels in other professions.  I've even caught myself reading the post...

Incredible Demos

Discussion

  1. Hey this is great. I have been using your twitter plugin for a while and was kinda forced to stop using it because twitter would block my site as a referer pretty often. That might solve this issue

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!