TwitterGitter

Everyone loves Twitter. Everyone loves MooTools. That's why everyone should love TwitterGitter, a MooTools plugin that retrieves a user's recent tweets and allows the user to format them however the user would like. TwitterGitter allows the user to choose the number of tweets to retrieve and returns an object containing the data provided by Twitter.

Note: TwitterGitter requires Aaron Newton's JSONP plugin which you may download here.

Download in Package Debut Article Example Usage

 

Plugin Code (Version 1.0)

var TwitterGitter = new Class({
	
	//implements
	Implements: [Options,Events],

	//options
	options: {
		count: 2,
		sinceID: 1,
		link: true,
		onRequest: $empty,
		onComplete: $empty
	},
	
	//initialization
	initialize: function(username,options) {
		//set options
		this.setOptions(options);
		this.info = {};
		this.username = username;
	},
	
	//get it!
	retrieve: function() {
		new JsonP('http://twitter.com/statuses/user_timeline/' + this.username + '.json',{
			data: {
				count: this.options.count,
				since_id: this.options.sinceID
			},
			onRequest: this.fireEvent('request'),
			onComplete: function(data) {
				//linkify?
				if(this.options.link) {
					data.each(function(tweet) { tweet.text = this.linkify(tweet.text); },this);
				}
				//complete!
				this.fireEvent('complete',[data,data[0].user]);
			}.bind(this)
		}).request();
		return this;
	},
	
	//format
	linkify: function(text) {
		//courtesy of Jeremy Parrish (rrish.org)
		return text.replace(/(https?:\/\/\S+)/gi,'$1').replace(/(^|\s)@(\w+)/g,'$1@$2').replace(/(^|\s)#(\w+)/g,'$1#$2');
	}
});

/* usage */
window.addEvent('domready',function() {
	$('git').addEvent('click',function(e) {
		e.stop();
		$('tweets-here').set('html','');
		//get information
		var myTwitterGitter = new TwitterGitter($('username').value,{
			count: 5,
			onComplete: function(tweets,user) {
				tweets.each(function(tweet,i) {
					new Element('div',{
						html: '' + user.name + ' ' + user.name + '
' + tweet.text + '
' + tweet.created_at + ' via ' + tweet.source.replace("\\",'') + '', 'class': 'tweet clear' }).inject('tweets-here'); }); } }).retrieve(); }); });

Options & Events

  • count: (defaults to 2) The number of tweets you would like returned.
  • sinceID: (defaults to 1) The baseline for the tweets to be returned.
  • link: (defaults to true) Want the class to linkify URLs, @ replies, and #topics?
  • onRequest: The function to execute when the TwitterGitter request is made.
  • onComplete: The function to execute when the TwitterGitter request is complete. This is where you want to put your tweet formatting.

Methods

retrieve

myGitter.retrieve();
  • Returns the twitter tweets object and a shortcut user object

Code Revisions & Bug Fixes

None.