/*! * jQuery socialFlair - Stackoverflow like User Flairs to showcase your * Social Profiles * --------------------------------------------------------------------- * * This is a simple jQuery plugin which lets you embed social flairs * into your personal website or blog. * * Licensed under Mozilla Public License * * @version 1.0.0 * @since 2012.05.01 * @author Amit Gharat a.k.a. codef0rmer * @blog http://goo.gl/frl5a * @twitter twitter.com/codef0rmer * * Usage: * --------------------------------------------------------------------- * Twitter Flair: * $(ele).socialFlair('twitter', 'twitterUsername', {}); * * Github Flair: * $(ele).socialFlair('github', 'githubUsername', {}); * * Facebook Flair: * Create an App and get the details here: https://developers.facebook.com/apps * $(ele).socialFlair('facebook', 'facebookUsername', {clientId : clientId, clientSecret : clientSecrete}); * */ (function($) { $.fn.extend({ socialFlair : function(service, handler, options) { var defaults = { }; options = $.extend({}, defaults, options); return this.each(function() { var $ele = $(this), followers = 0, following = 0, access_token = null, public_repos = 0; // Twitter API if (service === 'twitter' && handler !== undefined) { $(document).queue("ajaxRequests", function() { $.ajax({ url : 'http://api.twitter.com/1/followers/ids.json?screen_name=' + handler + '&cursor=-1', method : 'get', dataType : 'jsonp', success : function (data) { followers = data.ids.length; } }); }); $(document).queue("ajaxRequests", function() { $.ajax({ url : 'https://api.twitter.com/1/friends/ids.json?cursor=-1&screen_name=' + handler, method : 'get', dataType : 'jsonp', success : function (data) { following = data.ids.length; $ele.html( "" + " " + " " + " "+ " " + " " + truncateName(handler) + "" + " "+ " " + following + "" + " " + followers + "" + " " + "" + "" ); } }); }); $(document).dequeue("ajaxRequests"); $(document).dequeue("ajaxRequests"); } else if (service === 'github' && handler !== undefined) { // Github API $.ajax({ url : 'https://api.github.com/users/' + handler, method : 'get', dataType : 'jsonp', success : function (data) { followers = data.data.followers; public_repos = data.data.public_repos; $ele.html( "" + " " + " " + " " + " " + " " + truncateName(data.data.name || data.data.login) + "" + " " + " " + public_repos + "" + " " + followers + "" + ( data.data.hireable === true ? "Hire Me!" : '' ) + " " + "" + "" ); } }); } else if (service === 'bitbucket' && handler !== undefined) { // BitBucket API $.ajax({ url : 'https://api.bitbucket.org/1.0/users/' + handler + '/followers', method : 'get', dataType : 'jsonp', success : function (data) { var followers = data.count; $.ajax({ url : 'https://api.bitbucket.org/1.0/users/' + handler, method : 'get', dataType : 'jsonp', success : function (data) { var public_repos = data.repositories.length; $ele.html( "" + " " + " " + " " + " " + " " + truncateName(data.user.username) + "" + " " + " " + public_repos + "" + " " + followers + "" + " " + "" + "" ); } }); } }); } else if (service === 'facebook' && handler !== undefined) { // Facebook API $.ajax({ url : 'https://graph.facebook.com/oauth/access_token?client_id=' + options.clientId + '&client_secret=' + options.clientSecret + '&grant_type=client_credentials', method : 'get', dataType : 'text', success : function (data) { access_token = data; $.ajax({ url : 'https://api.facebook.com/method/friends.get?uid=' + handler + '&' + access_token + '&format=json', method : 'get', dataType : 'json', success : function (data) { following = data.length; $.ajax({ url : 'https://graph.facebook.com/' + handler, method : 'get', dataType : 'json', success : function (data) { $ele.html( "" + " " + " " + " " + " " + " " + truncateName(data.name) + "" + " " + " " + following + "" + " " + "" + "" ); } }); } }); } }); } }); } }); })(jQuery); function truncateName(handler) { return ( handler.length > 28 ? handler.substring(0, 28) + '...' : handler ); }