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
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

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!