Created
August 15, 2010 15:44
Revisions
-
keeguon revised this gist
Oct 24, 2010 . 1 changed file with 53 additions and 58 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,49 +1,26 @@ var Twitter = { version: '1.0', apiUrl: 'http://api.twitter.com', apiVersion: '1', api: function(endpoint, options, callback) { var fullUrl = this.apiUrl + '/' + this.apiVersion + '/' + endpoint + '.json'; $.ajax({ async: true, url: fullUrl, data: options.data, dataType: 'jsonp', success: function(response, status, xhr) { callback(response); }, error: function(xhr, status, error) { console.log(error); } }); } }; var TwitterHelper = { htmlize: 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 @@ -52,26 +29,18 @@ function Twitter() { tweet = tweet.replace(/@(\w+)/g, '<a href="http://twitter.com/$1" target="_blank">@$1</a>'); // return HTMLized tweet return tweet; }, timediff: 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'; @@ -91,5 +60,31 @@ function Twitter() { return Math.floor(timediff / day) + ' days ago'; else return 'over a year ago'; } }; // Demo usage Twitter.api('statuses/user_timeline', { data: 'screen_name=keeguon&count=50&include_rts=true' }, function(response) { // variables var limit = 10; var timeline = ''; var now = new Date(); // tweet loop for(var i = 0; i < response.length; i++) { if (!limit) break; if (response[i].in_reply_to_screen_name || response[i].in_reply_to_status_id || response[i].in_reply_to_user_id) { continue; } else { var tweet = ''; var created_at = new Date(response[i].created_at); tweet += '<div class="tweet">'; tweet += '<p class="text">'+TwitterHelper.htmlize(response[i].text)+'</p>'; tweet += '<p class="meta">'+TwitterHelper.timediff(now, created_at)+'</p>'; tweet += '</div>'; tweet += '<hr />'; timeline+=tweet; limit--; } } // changing the html $('div.timeline').html(timeline); }); -
keeguon revised this gist
Aug 20, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -87,7 +87,7 @@ function Twitter() { return Math.floor(timediff / hour) + ' hours ago'; if (timediff > day && timediff < day * 2) return 'yesterday'; if (timediff < week * 52) return Math.floor(timediff / day) + ' days ago'; else return 'over a year ago'; -
keeguon revised this gist
Aug 15, 2010 . 1 changed file with 40 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -53,15 +53,43 @@ function Twitter() { // 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'; }; }; -
keeguon revised this gist
Aug 15, 2010 . 1 changed file with 30 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,9 +11,18 @@ 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) { @@ -24,9 +33,16 @@ function Twitter() { } } // 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>'); @@ -37,4 +53,15 @@ function Twitter() { // return HTMLized tweet return tweet; }; }; // Demo usage var twitter = new Twitter(); twitter.api({ api: 'statuses', method: 'user_timeline', format: 'json', params: {'screen_name': 'keeguon', 'count': 50, 'include_rts': true} }, function(response) { console.log(response); }); -
keeguon revised this gist
Aug 15, 2010 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,12 +15,12 @@ function Twitter() { 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] + '&'; } } -
keeguon revised this gist
Aug 15, 2010 . 1 changed file with 13 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ Object.prototype.size = function () { for (var k in this) len++; return len; }; // Twitter class function Twitter() { @@ -26,4 +26,15 @@ function Twitter() { 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; }; }; -
keeguon created this gist
Aug 15, 2010 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ // 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=?'; }; }