Permalink
Browse files

restructure waldo api server so it can be a pleasant fab neighbor

  • Loading branch information...
technoweenie committed Feb 3, 2010
1 parent 986c96e commit 29fdbebad961df8176d591a24a3c520f30ae149d
Showing with 80 additions and 14 deletions.
  1. +31 −14 lib/api.js
  2. +49 −0 test/custom_api_test.js
View
@@ -13,8 +13,8 @@ exports.create = function(port, host) {
return this.createServer(waldo)
}
-exports.createServer = function(waldo) {
- return new ApiServer(waldo)
+exports.createServer = function(waldo, callback) {
+ return new ApiServer(waldo, callback)
}
// wrapper around an HTTP Server that has its own events:
@@ -23,17 +23,37 @@ exports.createServer = function(waldo) {
// * list(location, users)
// * track()
//
-function ApiServer(waldo, prefix) {
- if(!prefix)
- prefix = '/waldo'
- this.waldo = waldo
+// Pass a callback to modify the prefix or the fab directly.
+//
+// var server = new ApiServer(waldo, function(srv) {
+// srv.prefix = '/foo'
+// // don't finish the fab
+// srv.fab = ( fab )
+// ('/foo', 'foo!')
+// })
+function ApiServer(waldo, callback) {
+ this.waldo = waldo
+ this.prefix = '/waldo'
+ this.fab = null
+ if(callback)
+ callback(this)
+ if(this.fab)
+ this.fab = this.mountToFab(this.prefix, this.fab)
+}
- app = ( fab )
- app = this.mountToFab(prefix, app)
- this.server = Http.createServer(app(fab));
+ApiServer.prototype.listen = function(port, callback) {
+ this.server = Http.createServer((this.fab || this.createFab())(fab))
+ if(callback) {
+ callback(this.server)
+ }
+ this.server.listen(port)
}
ApiServer.headers = {'Content-Type': 'application/json'}
+ApiServer.prototype.createFab = function(prefix) {
+ return this.mountToFab(prefix || this.prefix, fab)
+}
+
ApiServer.prototype.mountToFab = function(prefix, fab) {
var app = this
return ( fab )
@@ -108,12 +128,9 @@ ApiServer.prototype.listeners = function(event) {
return this.waldo.listeners(event)
}
-ApiServer.prototype.listen = function(port) {
- return this.server.listen(port)
-}
-
ApiServer.prototype.close = function() {
- return this.server.close()
+ if(this.server)
+ return this.server.close()
}
ApiServer.prototype.buildJSON = function(body, fab) {
View
@@ -0,0 +1,49 @@
+var redisclient = require('redisclient'),
+ whereswaldo = require('../lib'),
+ assert = require('assert'),
+ testHttp = require('./helpers/test_client'),
+ promises = require('./lib/promise-group')
+ listeners = require('./helpers/temp_listeners')
+ sys = require('sys'),
+ api = require('../lib/api');
+
+process.mixin(GLOBAL, require('ntest'));
+
+var PORT = 8889,
+ redis = new redisclient.Client(),
+ waldo = whereswaldo.create(redis),
+ client = testHttp.createClient(PORT),
+waldoServer = api.createServer(waldo, function(srv) {
+ srv.prefix = '/custom-waldo'
+ srv.fab = ( fab )
+ ('/hello', 'hello world')
+});
+waldoServer.listen(PORT, function(srv) {
+ srv.addListener('close', function(e) {
+ process.exit();
+ })
+});
+
+before(function() { redis.flushdb().wait() })
+
+it("hits custom fab action", function() {
+ var resp = client.request("/hello").wait()
+ assert.equal("hello world", resp.body)
+})
+
+it("tracks user's location", function() {
+ client.request("/custom-waldo/track?name=bob&location=gym").wait()
+ var resp = client.request("/custom-waldo/locate?name=bob").wait();
+ assert.equal('"gym"', resp.body);
+
+ var resp = client.request("/custom-waldo/list?location=gym").wait();
+ assert.equal('["bob"]', resp.body);
+})
+
+it("tracks a user and lists users for that location", function() {
+ client.request("/custom-waldo/track?name=thing1&location=hat").wait()
+ var resp = client.request("/custom-waldo/track_and_list?name=thing2&location=hat").wait()
+ assert.equal('["thing1","thing2"]', resp.body)
+})
+
+waldoServer.close();

0 comments on commit 29fdbeb

Please sign in to comment.