Skip to content

Instantly share code, notes, and snippets.

@keeguon
Created August 15, 2010 15:44
Show Gist options
  • Save keeguon/525624 to your computer and use it in GitHub Desktop.
Save keeguon/525624 to your computer and use it in GitHub Desktop.
Basic Twitter object, demo at the bottom (require jQuery here but could also write your own Ajax function). This should work w/ all Twitter APIs which do not require authentification.
// Extending the JS Object (not really advised in theory but we really need this to know an associative array size)
Object.prototype.size = function () {
var len = this.length ? --this.length : -1;
for (var k in this)
len++;
return len;
};
// Twitter class
function Twitter() {
this.base_url = 'http://api.twitter.com';
this.version = 1;
/**
* Make an API call and get the result w/ a callback
*
* @param settings mixed type (associative array) containing the informations to build the API call (see demo)
* @param callback function
*
*/
Twitter.prototype.api = function(settings, callback) {
// Generate the basic API URI
var api_uri = this.base_url + '/' + this.version + '/' + settings.api + '/' + settings.method + '.' + settings.format;
// Append parameters to the URI (if any)
if (settings.params.size()) {
api_uri += '?';
for (var param in settings.params) {
if (settings.params[param] == null || settings.params[param] == false || settings.param == 'size')
continue;
api_uri += param + '=' + settings.params[param] + '&';
}
}
// Perform an Ajax request (using jQuery here but could be something else, also jQuery requires the format to be JSONP)
jQuery.getJSON(api_uri + 'callback=?', callback);
};
/**
* Take a tweet and HTMLize the content of the tweet (URIs, hashtags, @user)
*
* @param tweet (text) to HTMLize
*
*/
Twitter.prototype.htmlize_tweet = function(tweet) {
// replace url w/ links
tweet = tweet.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi, '<a href="$&" target="_blank">$&</a>');
// replace hashtags w/ links
tweet = tweet.replace(/#(\w+)/g, '<a href="http://search.twitter.com/search?q=%23$1" target="_blank">#$1</a>');
// replace @username w/ links
tweet = tweet.replace(/@(\w+)/g, '<a href="http://twitter.com/$1" target="_blank">@$1</a>');
// return HTMLized tweet
return tweet;
};
/**
* Calculate how old a tweet is
*
* @param timestamp (Date object) which is in theory right now but could also be some other date in the future (past is not advised for obvious reasons)
* @param tweet creation time (also a Date object)
*
*/
Twitter.prototype.time_string = function(timestamp, created_at) {
// Time constants
var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
// Timediff
timediff = timestamp - created_at;
// Create timestring
if (timediff < second * 7)
return 'right now';
if (timediff < minute)
return Math.floor(timediff / second) + ' seconds ago';
if (timediff < minute * 2)
return 'about a minute ago';
if (timediff < hour)
return Math.floor(timediff / minute) + ' minutes ago';
if (timediff < hour * 2)
return 'about an hour ago';
if (timediff < day)
return Math.floor(timediff / hour) + ' hours ago';
if (timediff > day && timediff < day * 2)
return 'yesterday';
if (timediff < day * 365)
return Math.floor(timediff / day) + ' days ago';
else
return 'over a year ago';
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment