Created
January 4, 2014 10:07
-
-
Save fbiville/8253751 to your computer and use it in GitHub Desktop.
Custom feed aggregator. Stack: requirejs (+text.js)+underscore+async+moment.js+jquery+handlebars
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
define( ['jquery', 'underscore', 'async', 'moment', 'Handlebars'], | |
function($, _, async, moment, Handlebars) { | |
var crossOriginFeedParserUrl = function(limit, feedUrl) { | |
return document.location.protocol + | |
'//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='+ | |
limit+ | |
'&callback=?&q=' + | |
encodeURIComponent(feedUrl); | |
}, | |
doAggregate = function(limit, target, feedUrl, callback) { | |
$.ajax({ | |
url: crossOriginFeedParserUrl(limit, feedUrl), | |
dataType: 'json', | |
success: function(data) { | |
callback(null, data.responseData.feed.entries); | |
} | |
}); | |
}, | |
sortEntries = function(limit, entries) { | |
return _.sortBy( | |
_.union.apply(_, entries), | |
function (entry) { | |
return -1 * moment(entry.publishedDate).unix(); | |
} | |
).slice(0, limit); | |
}, | |
mergeEntries = function(postSkeleton, entries) { | |
var template = Handlebars.compile(postSkeleton); | |
return _.map(entries, function(entry) { | |
return template({ | |
author: entry.author, | |
contents: entry.contentSnippet, | |
date: moment(entry.publishedDate).fromNow(), | |
url: entry.link, | |
title: entry.title | |
}); | |
}); | |
}, | |
updateContents = function (limit, target, postSkeleton, error, success) { | |
var entries = sortEntries(limit, success), | |
feeds = mergeEntries(postSkeleton, entries); | |
$(target).html(feeds); | |
}; | |
return { | |
aggregate: function(feedUrls, limit, target, postTemplate) { | |
async.map( | |
feedUrls, | |
_.partial(doAggregate, limit, target), | |
_.partial(updateContents, limit, target, postTemplate) | |
); | |
} | |
} | |
}); |
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
<h3><a href="{{url}}" target="_blank">{{title}}</a></h3> | |
<p>By {{author}}, <span class="date">{{date}}</p> | |
<blockquote cite="{{url}}"> | |
{{{contents}}} | |
</blockquote> |
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
require(['jquery', 'moment', 'aggregator', 'text!example_post.tpl'], | |
function($, moment, aggregator, postTemplate) { | |
moment.lang('fr'); | |
var feedUrls = [ | |
'http://www.eventuallycoding.com/index.php/feed/', | |
'http://florent.biville.net/?feed/atom', | |
'http://www.java-freelance.fr/feed', | |
'http://ogirardot.wordpress.com/feed/' | |
]; | |
aggregator.aggregate( | |
feedUrls, | |
10, | |
$('#feeds'), | |
postTemplate | |
); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment