Comparing version 1.1.6 to 1.2.0
@@ -47,12 +47,12 @@ var _ = require('underscore') | ||
Octopie.prototype.on = function(name, cb) { | ||
if (!_(this.allEvents).contains(name)) { | ||
return new Error('Not a valid event type. See `http://developer.github.com/v3/activity/events/types/` for a list of valid event types.'); | ||
if (_(this.allEvents).contains(name)) { | ||
// Update our internal list of events to listen to | ||
// and make sure that it's sorted and uniqued | ||
this.events.push(name) | ||
this.events = _.chain(this.events).concat([name]).sort().uniq().value() | ||
} | ||
// Update our internal list of events to listen to | ||
// and make sure that it's sorted and uniqued | ||
this.events.push(name) | ||
this.events = _.chain(this.events).concat([name]).sort().uniq().value() | ||
// Bind our CB to the server's GitHub event name | ||
@@ -64,2 +64,20 @@ this.server.on(name, cb) | ||
// Alias for Octopie#on | ||
Octopie.prototype.addListener = function() { | ||
var args = Array.prototype.slice.call(arguments) | ||
this.on.apply(this, args) | ||
} | ||
// Let's proxy the remainder of the event listener methods through to the | ||
// server, so we cheat and use it as our event bus | ||
var emitterMethods = ["removeListener", "removeAllListeners", | ||
"setMaxListeners", "listeners", "emit" ] | ||
emitterMethods.forEach(function(methodName) { | ||
Octopie.prototype[methodName] = function() { | ||
var args = Array.prototype.slice.call(arguments) | ||
return this.server[methodName].apply(this.server, arguments) | ||
} | ||
}) | ||
Octopie.prototype.listen = function(port, cb) { | ||
@@ -66,0 +84,0 @@ if (!port) { throw new Error("Can't start listening without a port!") } |
var express = require('express') | ||
, app = express() | ||
, Warning = require('./warning') | ||
@@ -8,4 +9,11 @@ app.use(express.bodyParser()); | ||
var event = req.header('x-github-event') | ||
app.emit(event, req.body) | ||
app.emit('*', event, req.body) // Synthetic event to catch all GH hooks | ||
try { | ||
app.emit(event, req.body) | ||
app.emit('*', event, req.body) // Synthetic event to catch all GH hooks | ||
} catch(e) { | ||
e.recoverable = true | ||
app.emit('error', e) | ||
} | ||
res.send('OK') | ||
@@ -12,0 +20,0 @@ }) |
{ | ||
"name": "octopie", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"description": "An easy way to add GitHub hooks to your project", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -92,2 +92,39 @@ var assert = require("assert") | ||
}) | ||
it("should bind events while tracking GitHub events", function() { | ||
var octopie = octoFactory() | ||
, fooSpy = sinon.spy() | ||
, pushSpy = sinon.spy() | ||
octopie.on('foo', fooSpy) | ||
octopie.on('push', pushSpy) | ||
assert.deepEqual(octopie.events, ["push"]) | ||
assert(octopie.server.on.calledWith('foo', fooSpy)) | ||
assert(octopie.server.on.calledWith('push', pushSpy)) | ||
}) | ||
it("should expose proxy all of eventEmitter's methods to the server", function() { | ||
var octopie = octoFactory() | ||
, onBarSpy = sinon.spy() | ||
, addListenerFooSpy = sinon.spy() | ||
, onFooSpy = sinon.spy() | ||
octopie.on('bar', onBarSpy) | ||
octopie.on('foo', onFooSpy) | ||
octopie.addListener('foo', addListenerFooSpy) | ||
assert.deepEqual(octopie.listeners(), octopie.server.listeners()) | ||
assert.deepEqual(octopie.listeners('foo'), octopie.server.listeners('foo')) | ||
octopie.emit('foo', 1, 2, 3) | ||
assert(onFooSpy.callCount === 1) | ||
assert(onFooSpy.calledWith(1, 2, 3)) | ||
assert(addListenerFooSpy.callCount === 1) | ||
assert(addListenerFooSpy.calledWith(1, 2, 3)) | ||
octopie.removeAllListeners() | ||
octopie.emit('bar') | ||
assert(onBarSpy.notCalled) | ||
}) | ||
}); |
12637
8
251