Permalink
Browse files

looks like refactor is ready

  • Loading branch information...
1 parent 4732413 commit d590127e92f2b8cf1a5eb93bb038dfcd5a9b4ebe @dominictarr committed Jun 15, 2011
View
@@ -0,0 +1,52 @@
+
+var log = console.log
+ , render = require('render')
+ , overwrite = require('../model/overwrite')
+
+module.exports = function (db,Repo,config) { //I think dependency injection may be an antipattern
+
+ return function (req,cont){
+ var payload
+ if(!req.body)
+ return cont ({statusCode: 400, error: "Expected payload: property"})
+
+ try {
+ payload = JSON.parse(req.body.payload)
+ } catch (err) {
+ console.error('could not parse POST JSON')
+ return cont ({
+ statusCode: 400,
+ error: "was not valid JSON",
+ was: req.body.payload,
+ exception: err.stack
+ })
+ }
+
+ log('loading new repo')
+ render.ct.log(payload)
+
+ repo = Repo(
+ payload.repository.owner.name,
+ payload.repository.name,
+ config.basedir) // load that from config.
+
+ repo.post = payload
+
+ repo.on('change',function (event){
+ console.log('change',event, repo._id)
+ })
+
+ repo.integrate(function (err,data){
+ //log
+ if(err){//not necessarily the right error message.
+ return cont({statusCode: 503, error: err, reason: "could not connect to github or npm", data: data})
+ }
+ overwrite(db,repo,function (err2,data2){ //save to database.
+ if(err)
+ return cont({statusCode: 503, error: err, reason: "database error", data: data})
+
+ cont(null, data2)
+ })
+ })
+ }
+}
View
@@ -1,6 +1,9 @@
-module.exports = {
- summary: require('./summary')
-, result: require('./result')
-//, summary: require('./summary')
-}
+module.exports = function (db,Repo,config){
+ return {
+ summary: require('./summary')(db)
+ , result: require('./result')(db)
+ , github: require('./github')(db,Repo,config)
+ //, summary: require('./summary')
+ }
+}
@@ -1,8 +1,10 @@
-module.exports =
- function (req,cont){
+module.exports = function (db){
+
+ return function (req,cont){
db.get([req.params.username, req.params.project, req.params.commit].join(','),
function (err,data){
cont(null,data)
})
- }
+ }
+}
@@ -1,48 +1,50 @@
-function summary(opts,cont){
+module.exports = function (db){
+ function summary(opts,cont){
- db.view('all/summary',opts, function (err,data){
- if(err) {
- console.error(err)
- res.statusCode = 500
- return cont(err)
- }
- data.rows.sort(function (x,y){
- return x.value.time < y.value.time ? 1 : -1
- })
- if(data.rows.length)
- cont(null,data)
- else
- cont(404)
+ db.view('all/summary',opts, function (err,data){
+ if(err) {
+ console.error(err)
+ res.statusCode = 500
+ return cont(err)
+ }
+ data.rows.sort(function (x,y){
+ return x.value.time < y.value.time ? 1 : -1
+ })
+ if(data.rows.length)
+ cont(null,data)
+ else
+ cont(404)
- })
-}
+ })
+ }
-module.exports = {
- home: function (req,cont){
- var opts = {
- group_level: 2,
- reduce: true
- }
- summary(opts,cont)
- },
- username:
- function (req,cont){
+ return {
+ home: function (req,cont){
var opts = {
- startkey: [req.params.username,'_______'],
- endkey: [req.params.username,'ZZZZZZZZZ'],
group_level: 2,
reduce: true
}
summary(opts,cont)
},
- username_project: function (req,cont){
- var opts = {
- endkey: [req.params.username,req.params.project,'ZZZZZZZ'],
- startkey: [req.params.username,req.params.project,'_____'],
- reduce: false
+ username:
+ function (req,cont){
+ var opts = {
+ startkey: [req.params.username,'_______'],
+ endkey: [req.params.username,'ZZZZZZZZZ'],
+ group_level: 2,
+ reduce: true
+ }
+ summary(opts,cont)
+ },
+ username_project: function (req,cont){
+ var opts = {
+ endkey: [req.params.username,req.params.project,'ZZZZZZZ'],
+ startkey: [req.params.username,req.params.project,'_____'],
+ reduce: false
+ }
+ summary(opts,cont)
}
- summary(opts,cont)
}
}
View
@@ -11,12 +11,14 @@ var it = require('it-is')
module.exports =
function Handler (){
- function handleError(err,res){
+ function handleError (err,res) {
var status =
'number' == typeof err ? err : (err.statusCode || 500)
- if(!handler.error[status])
- status = 500
- if('string' == typeof handler.error[status])
+
+ if (!handler.error[status]) {
+ return res.send(JSON.stringify(err),status)
+ }
+ if ('string' == typeof handler.error[status])
return res.render(handler.error[status], {self: err})
handler.error[status](err,res)
}
View
@@ -0,0 +1,73 @@
+/*
+ upload a new document, and overwrite what ever is in that place.
+
+ couch makes you save documents with the right document revision,
+ and we do not delete the document since, since it will briefly be unavailable.
+
+ using FSM to handle the async control flows
+*/
+
+var FSM = require('fsm')
+
+/*
+ I think I want to turn the fsm into a function...
+ call it and start._in is triggered with the arguments you've passed.
+
+ everything else is effectively hidden, until a halting state is reached.
+ ('end' or 'fatal' will be the conventions)
+ then callback is invoked.
+
+ hmm, and say an event emitter is returned, so that each event listened to if you like.
+
+ or is that a bad idea? hmm. could be useful for progress bars or something.
+*/
+
+//f.filter(null,/^_/)
+
+function filter (obj,keys) {
+ var n = {}
+ Object.keys(obj).forEach(function (i){
+ if(!~keys.indexOf(i))
+ n[i] = obj[i]
+ })
+ return n
+}
+
+module.exports = exports = function (db,item,callback){
+ return new FSM({
+ start: {
+ _in: function (db,item){
+ this.local.item = item = item || this.local.item
+ this.local.db = db = db || this.local.db
+
+ if(!item)
+ throw new Error('item cannot be null')
+ if(!item._id)
+ throw new Error('expected ._id property')
+
+ this.local.item = item = filter(item,['_events'])
+ console.log('filtered', this.local.item)
+ db.get(item._id, this.callback('ready'))
+ },
+ ready: ['save', function (n,data) {
+ this.local.item._rev = data._rev
+ this.local.db.save(this.local.item,this.callback('saved'))//will generate 'saved' or 'error'
+ }],
+ error: ['save', function (err) {//hopefully a not_found.
+ console.log('****************',err,'get error, try and save')
+ this.local.db.save(this.local.item,this.callback('saved'))//will generate 'saved' or 'error'
+ }]
+ },
+ save: {
+ saved:'end',
+ //try again... this is where i need namespaced events.
+ //'error.conflict': 'start'
+ error: function (err) {
+ if('conflict' === err.error)
+ this.retry()
+ else throw err
+ },
+ retry: 'start'
+ }
+ }).call(db,item,callback)
+}
View
@@ -3,22 +3,16 @@
var testbed = new (require('./testbed'))(process.cwd() + '/workspace')
, appSetup = require('./app-setup')
, request = require('request')
- , w = require('winston')
- , eyes = require('eyes')
, render = require('render')
, url = require('url')
, db
, config
, handler = require('./handler')()
- , controllers = require('./controllers')
-
+ , controllers
+
handler.error['500'] = '500'
handler.error['404'] = '404'
- w.cli()
- w.info('TESTBED')
- w.info(new Date)
-
if(!module.parent) {
config = require('./setup').deploy()
} else {
@@ -32,12 +26,15 @@ var testbed = new (require('./testbed'))(process.cwd() + '/workspace')
db = require('./initialize')(config.database, function (err, db){
if(err){
- w.error("DATABASE SETUP ERROR")
+ console.error("DATABASE SETUP ERROR")
throw err
}
- w.info("DATABASE '" + config.database + "' IS READY")
+ console.log("DATABASE '" + config.database + "' IS READY")
})
+ controllers = require('./controllers')(db,testbed.Repo,config) //dependency injection antipattern
+
+/*
function save (repo) { //pull this out also.
if(repo._id && !repo.saving){
repo.saving = true
@@ -48,20 +45,10 @@ function save (repo) { //pull this out also.
for (var key in repo)
obj[key] = repo[key]
console.log(obj)
- db.save({
- _id: '' + repo._id,
- _rev: doc && doc._rev,
- commit: repo.commit,
- installation: repo.installation,
- package: repo.package,
- post: repo.post,
- project: repo.project,
- report: repo.report,
- state: repo.state,
- time: new Date,
- type: repo.type,
- username: repo.username,
- },
+ obj._id = '' + repo._id
+ obj._rev = doc && dov._rev
+ obj.time = new Date()
+ dbj.save(obj,
function (err, data){
repo._rev = data._rev
repo.saving = null
@@ -74,49 +61,11 @@ function save (repo) { //pull this out also.
} else {
repo.changed = true
}
-}
-
-app.post('/', function (req, res) {
-
- //pull all of this out into a repo controller.
-
- if(!req.body)
- return res.send ({error: "Expected payload: property"})
- try{
- var payload = JSON.parse(req.body.payload)
- } catch (err){
- w.error('could not parse POST JSON')
- w.statusCode = 400
- return res.send ({
- error: "was not valid JSON",
- was: req.body.payload,
- exception: err.stack})
- }
- w.info('loading new repo')
- eyes.inspect(payload)
-
- repo = testbed.Repo(
- payload.repository.owner.name,
- payload.repository.name,
- config.basedir) // load that from config.
+}*/
- repo.post = payload
-
- repo.on('change',function (event){
- //log
- console.log(event, repo._id)
- save(repo)
- })
-
- repo.integrate(function (err,data){
- //log
- if(err){
- return res.send({error: err,
- reason: "could not connect to github or npm", data: data})
- }
- res.send(data)
- })
-})
+app.post('/', handler(controllers.github, function (obj,res){
+ res.send(obj,{'Content-Type': 'application/json'},200)
+}))
/*
controller gets req and a callback, through which it passes it's data to renderer
Oops, something went wrong.

0 comments on commit d590127

Please sign in to comment.