TipTheWeb TipStream (jQuery)

Render a TipTheWeb TipStream by fetching the data using JSOP and YQL

HTML

<ul id="tipstream"></ul>

CSS

.tip {
    margin: 10px 0;
}

JavaScript

var ttwAccountId = '3mtvsfracef58',
    numTips = 5,
    yqlQuery = 'SELECT * from json WHERE url="http://tiptheweb.org/tipstream/' + ttwAccountId + '/?format=json"';

$.ajaxSetup({ cache: true });
$.get('http://query.yahooapis.com/v1/public/yql?format=json&q=' + encodeURIComponent(yqlQuery), function(r){

    if ( ! (r && r.query && r.query.results)) { return; }
    r = r.query.results.json;
    
    var tipStream = $('#tipstream');
    
    $.each(r.tips, function(i, tip){
    
        tipStream.append('<li class="tip">' + formatAmount(tip.amountCents) + ' Tip for <a href="' + tip.link + '">' + tip.title + '</a></li>');
        
        return i < (numTips - 1);
    
    });
    
}, 'jsonp');

function formatAmount(amount){
    
    amount = amount/100;
    
    var amountParts = amountParts = amount.toString().split('.');
                
    if ( ! amountParts[1]) {
        amountParts[1] = '00';  
    } else if (amountParts[1].length === 1) {
        amountParts[1] += '0';
    }
    
    return '$'+amountParts.join('.');
}