From 73a15ed341137705e8bade75bbe8dd85db0d260d Mon Sep 17 00:00:00 2001 From: Tilo Mitra Date: Thu, 7 Mar 2013 19:47:31 -0500 Subject: [PATCH 1/5] add extras routes --- app.js | 4 +++- lib/routes/extras.js | 33 +++++++++++++++++++++++++++++++++ lib/routes/index.js | 3 ++- views/extras.handlebars | 20 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 lib/routes/extras.js create mode 100644 views/extras.handlebars diff --git a/app.js b/app.js index e8dfad96..f76be34e 100644 --- a/app.js +++ b/app.js @@ -30,7 +30,8 @@ app.locals({ {id: 'grids', url: '/grids/', label: 'Grids'}, {id: 'forms', url: '/forms/', label: 'Forms'}, {id: 'tables', url: '/tables/', label: 'Tables'}, - {id: 'lists', url: '/lists/', label: 'Navigation'} + {id: 'lists', url: '/lists/', label: 'Navigation'}, + {id: 'extras', url: '/extras/', label: 'Extras'} ], yui : config.yui, @@ -68,6 +69,7 @@ app.get('/grids/', routes.render('grids')); app.get('/forms/', routes.render('forms')); app.get('/tables/', routes.render('tables')); app.get('/lists/', routes.render('lists')); +app.get('/extras/', routes.extras); // -- Exports ------------------------------------------------------------------ diff --git a/lib/routes/extras.js b/lib/routes/extras.js new file mode 100644 index 00000000..9884f85c --- /dev/null +++ b/lib/routes/extras.js @@ -0,0 +1,33 @@ +var config = require('../../config'), + github = require('../githubApi'), + inProgress = false; + +module.exports = function (req, res, next) { + + //If an API call is in progress, render the last collected data. + if (inProgress) { + res.render('extras', { + results: github.data.results + }); + } + + //If an API call isn't in progress, set the inProgress flag to true and make the call to getRepos() + else { + inProgress = true; + github.getRepos(config.extras.modules, function (error, results) { + inProgress = false; + if (!error) { + res.render('extras', { + results: results + }); + } + else { + console.log(error); + //We can probably serve the older results if we get an error for the newer results. + res.render('extras', { + results: github.data.results + }); + } + }); + } +}; diff --git a/lib/routes/index.js b/lib/routes/index.js index 2a2e4de9..34ebaa74 100644 --- a/lib/routes/index.js +++ b/lib/routes/index.js @@ -5,5 +5,6 @@ function render(viewName) { } module.exports = { - render: render + render: render, + extras: require('./extras') }; diff --git a/views/extras.handlebars b/views/extras.handlebars new file mode 100644 index 00000000..3d142b7e --- /dev/null +++ b/views/extras.handlebars @@ -0,0 +1,20 @@ +{{setTitle "Extras"}} +{{setSubTitle "Add-ons built on YUI CSS"}} +{{setActiveNav "extras"}} + +{{addCdnCSS "3.9.0pr3" "cssnormalize"}} +{{addGithubCSS "tilomitra" "csstables" "master/css/tables.css"}} + +{{> header}} + +
+ +
+ {{#each results}} +
+ +
+ {{/each}} +
+ +
From b3b069b2099659f9bab3cbe35c501825d57e634f Mon Sep 17 00:00:00 2001 From: Tilo Mitra Date: Thu, 7 Mar 2013 19:50:59 -0500 Subject: [PATCH 2/5] ignore github application ID and secret --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 153216eb..b0f241e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store node_modules/ npm-debug.log +config/github.json From 85beaf923934461f42affbda857350edd6958d7d Mon Sep 17 00:00:00 2001 From: Tilo Mitra Date: Thu, 7 Mar 2013 19:52:00 -0500 Subject: [PATCH 3/5] update config to include extras and github --- config/extras.js | 8 ++++++++ config/index.js | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 config/extras.js diff --git a/config/extras.js b/config/extras.js new file mode 100644 index 00000000..4d83c3b4 --- /dev/null +++ b/config/extras.js @@ -0,0 +1,8 @@ +module.exports = Object.freeze({ + modules: [ + { + username: 'tilomitra', + repo : 'cssextras' + } + ] +}); diff --git a/config/index.js b/config/index.js index bab041ab..d1aea84c 100644 --- a/config/index.js +++ b/config/index.js @@ -16,5 +16,7 @@ module.exports = Object.freeze({ }), typekit: 'ajf8ggy', - yui : require('./yui') + yui : require('./yui'), + extras : require('./extras'), + github : require('./github.json') }); From 3b9a7419b83db95c0429cd6a3d548a488d0980e1 Mon Sep 17 00:00:00 2001 From: Tilo Mitra Date: Thu, 7 Mar 2013 19:52:16 -0500 Subject: [PATCH 4/5] add githubApi module --- lib/githubApi.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 lib/githubApi.js diff --git a/lib/githubApi.js b/lib/githubApi.js new file mode 100644 index 00000000..2d8b02de --- /dev/null +++ b/lib/githubApi.js @@ -0,0 +1,56 @@ +var request = require('request'), + config = require('../config'), + async = require('async'); + +module.exports = { + + data: { + lastFetched: undefined, + results: undefined + }, + + timeThreshold: 3600000, //60 minutes in milliseconds + + //Get information for an individual Github repo and execute a callback + getRepo: function (username, repo, callback) { + request({ + url: 'https://api.github.com/repos/' + username + '/' + repo, + qs: { + client_id: config.github.clientId, + client_secret: config.github.clientSecret + } + }, function (error, response, body) { + callback(error, response, JSON.parse(body)); + }); + }, + + getRepos: function (userArray, callback) { + var parallelTasks = [], + self = this, + now = new Date(); + + //If results exist and those results are less than 60 minutes old (this.timeThreshold) + if (this.data.results && (now.getTime() - this.data.lastFetched) < this.timeThreshold) { + //return with those results + callback(null, this.data.results); + } + + else { + userArray.forEach(function (elem, i, arr) { + parallelTasks.push(function (callback) { + self.getRepo(elem.username, elem.repo, function (error, response, body) { + callback(error, body); + }); + }); + }); + + async.parallel(parallelTasks, function (error, response) { + if (!error) { + self.data.results = response; + self.data.lastFetched = new Date().getTime(); + } + callback(error, self.data.results); + }); + } + } +} From 436c6d3e4d5a4b025cdaadd9a911b9f5a98f4938 Mon Sep 17 00:00:00 2001 From: Tilo Mitra Date: Thu, 7 Mar 2013 19:52:37 -0500 Subject: [PATCH 5/5] add async and request --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f24d7dad..cdaf0938 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "dependencies": { "express" : "3.x", "express3-handlebars": "0.3.x", - "handlebars" : "1.x" + "handlebars" : "1.x", + "async" : "0.2.x", + "request" : "2.x" }, "main": "server",