Created
October 31, 2011 21:08
-
-
Save marcust/1328963 to your computer and use it in GitHub Desktop.
Playing a little bit with Github Event streams
This file contains 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
(function() { | |
"use strict"; | |
var user = "marcust"; | |
var url = "https://api.github.com/users/" + user +"/events?callback=?"; | |
function handleDefaultEvent( repoElement, timeElement, payloadHandler ) { | |
var element = document.createElement('div'); | |
element.appendChild( payloadHandler() ); | |
if ( repoElement !== null ) { | |
element.appendChild( repoElement ); | |
} | |
element.appendChild( timeElement ); | |
return element; | |
} | |
function linkFor( url, text ) { | |
var link = document.createElement('a'); | |
link.href = url; | |
link.target = '_blank'; | |
link.appendChild( document.createTextNode( text ) ); | |
return link; | |
} | |
function makeFragmentWithLink( prefixText, url, linkText, postfixText ) { | |
return function() { | |
var fragment = document.createDocumentFragment(); | |
fragment.appendChild( document.createTextNode( prefixText ) ); | |
if ( url !== undefined && linkText !== undefined ) { | |
fragment.appendChild( linkFor( url, linkText ) ); | |
} | |
if ( postfixText !== undefined ) { | |
fragment.appendChild( document.createTextNode( postfixText ) ); | |
} | |
return fragment; | |
}; | |
} | |
function handleCreateEvent( payloadData, repoElement, timeElement ) { | |
var value = 'Created ' + payloadData.ref_type + ' '; | |
value += payloadData.ref !== null ? payloadData.ref : '' ; | |
value += ' at '; | |
return handleDefaultEvent( repoElement, timeElement, makeFragmentWithLink( value ) ); | |
} | |
function handleGistEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( null, timeElement, | |
makeFragmentWithLink( 'Created Gist ', payloadData.gist.html_url, payloadData.gist.id ) ); | |
} | |
function handlePullRequestEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( repoElement, timeElement, | |
makeFragmentWithLink( payloadData.action + ' pull request ', | |
payloadData.pull_request.html_url, payloadData.pull_request.number, ' on ' ) ); | |
} | |
function handleDownloadEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( repoElement, timeElement, | |
makeFragmentWithLink( 'Created Download ', | |
payloadData.download.html_url, payloadData.download.name, ' at ' ) ); | |
} | |
function handleForkEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( repoElement, timeElement, | |
makeFragmentWithLink( 'Forked ', | |
payloadData.forkee.html_url, payloadData.forkee.name, ' from ' ) ); | |
} | |
function handleIssueCommentEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( repoElement, timeElement, | |
makeFragmentWithLink( 'Commented on Issue ', | |
payloadData.issue.html_url, payloadData.issue.number, ' at ' ) ); | |
} | |
function handlePushEvent(payloadData, repoElement, timeElement ) { | |
return handleDefaultEvent( repoElement, timeElement, function () { | |
var value = 'Pushed to '; | |
return document.createTextNode( value ); | |
} ); | |
} | |
var eventFunctions = { | |
CreateEvent : handleCreateEvent, | |
DownloadEvent : handleDownloadEvent, | |
ForkEvent : handleForkEvent, | |
GistEvent : handleGistEvent, | |
PullRequestEvent : handlePullRequestEvent, | |
PushEvent : handlePushEvent, | |
IssueCommentEvent : handleIssueCommentEvent | |
}; | |
function makeRepoElement( repoData ) { | |
return linkFor( 'http://github.com/' + repoData.name, repoData.name ); | |
} | |
function makeTimeElement( timestamp ) { | |
return document.createTextNode( ' about ' + moment( timestamp ).fromNow() ); | |
} | |
function handleEvent( event ) { | |
if ( eventFunctions[event.type] === undefined ) { | |
return null; | |
} | |
var mainDiv = document.createElement('li'); | |
mainDiv.classList.add('eventEntry'); | |
mainDiv.classList.add( event.type ); | |
var repoElement = makeRepoElement( event.repo ); | |
var timeElement = makeTimeElement( event.created_at ); | |
mainDiv.appendChild( eventFunctions[event.type]( event.payload, repoElement, timeElement ) ); | |
return mainDiv; | |
} | |
function handleUserEventData( element ) { | |
return function( data ) { | |
jQuery.each( data.data, function ( index, event ) { | |
var newElement = handleEvent( event, element ); | |
if ( newElement !== null ) { | |
element.appendChild( newElement ); | |
} | |
} ); | |
}; | |
} | |
jQuery(document).ready( | |
function() { | |
var element = document.getElementById( 'github' ); | |
jQuery.ajax( { | |
url : url, | |
dataType : 'jsonp', | |
success : handleUserEventData( element ) | |
} ); | |
} | |
); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment