Created
August 15, 2010 15:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
Twitter.prototype.api = function(settings) { | |
var api_uri = this.base_url + '/' + this.version + '/' + settings.api + '/' + settings.method + '.' + settings.format; | |
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] + '&' | |
} | |
} | |
return api_uri + 'callback=?'; | |
}; | |
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; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment