Skip to content

Instantly share code, notes, and snippets.

@ralphsaunders
Created December 26, 2011 10:42
Show Gist options
  • Save ralphsaunders/1520917 to your computer and use it in GitHub Desktop.
Save ralphsaunders/1520917 to your computer and use it in GitHub Desktop.
Loading Feed Entries With The Google Feeds API
<!doctype html>
<html>
<head>
<title>Google Feeds API</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body id="index">
<script type="text/javascript" src="https://www.google.com/jsapi?key=JS_API_KEY_HERE"></script>
<script type="text/javascript" src="scripts.js"></script>
</body>
</html>
/**
* Google Feed Namespace
*
* Namespace contains methods relating to RSS and general feed data returned
* from the google feeds API.
*/
var gFeed = function () {
/**
* Default Entry Count
*
* This int determines the default number of entries fetched from any given
* feed.
*
* @type int
*/
var defaultEntryCount = 5;
/**
* Construct
*
* Acts as the constructor to a feed, creating a new feed object when passed
* a url. If no entry count is set the default entry count will be used.
*
* @param string
* @param int
* @return object
*/
function construct( feedUrl, entryCount ) {
var feed = new google.feeds.Feed( feedUrl );
if( entryCount ) {
feed.setNumEntries( entryCount );
} else {
feed.setNumEntries( defaultEntryCount );
}
return feed;
}
/**
* Load
*
* Loads a feed when given the feed object and passes the result, either and
* error or the entries, to a callback.
*
* @param object
* @param callback
*/
function load( feed, callback ) {
feed.load( function( result ) {
if( result.error ) {
callback( result.error );
} else {
callback( result.feed.entries );
}
});
}
return {
/**
* Read
*
* Read handle for the Google Feed Namespace. Returns feed entries via a
* callback when given a feed's URL.
*
* @param string
* @param int ( optional )
* @param callback
*/
read : function( feedUrl, entryCount, callback ) {
load( construct( feedUrl, entryCount ), function( results ) {
callback( results );
});
}
};
}();
google.load( "feeds", "1" );
google.setOnLoadCallback( function() {
gFeed.read( "http://dribbble.com/ralphsaunders/shots/following.rss", 20, function( results ) {
for( i = 0; i < results.length; i++ ) {
var item = results[i].content;
$( 'body' ).append( item );
if( i == results.length - 1 ) {
$( 'body' ).append( "<h1>End of Stream</h1>" );
}
}
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment