Created
May 26, 2010 07:15
-
-
Save hugowetterberg/414168 to your computer and use it in GitHub Desktop.
Test to sketch out how the trickle router could work
This file contains hidden or 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
var sys = require('sys'), | |
trickle = require('../lib/trickle'), | |
router = new trickle.Router(), | |
number = /^\d+$/, | |
tests, path, route; | |
router.addRoute('node/:node', 'view_node'); | |
router.addRoute('node/:node/edit', 'edit_node'); | |
router.addRoute('node/:node/view', { | |
'sample': 'The route object can be anything!', | |
'data': '...but would probably be standardized at app-level' | |
}); | |
router.addRoute('admin', 'admin_page'); | |
router.addRoute('admin/*', 'admin_page_catchall'); | |
router.addRoute('admin/settings/content', 'content_settings'); | |
router.addRoute('admin/settings/build', 'content_settings'); | |
router.addRoute('node/:node=regex/devel', 'node_devel', { | |
'node': { | |
'regex': /^\d+$/, | |
'callback': function(value, match, callback) { | |
callback(false, { | |
'nid': parseInt(match[0], 10), | |
'title': 'Some node' | |
}); | |
} | |
} | |
}); | |
router.addRoute('/images/:path=regex*', 'image_path', { | |
'path': { | |
'regex': /(.*)\/(.*)\.(.*)/, | |
'callback': function(value, match, callback) { | |
switch (match[3]) { | |
case 'png': | |
case 'jpg': | |
case 'jpeg': | |
callback(false, { | |
'directory': match[1], | |
'name': match[2], | |
'extension': match[3], | |
'path': match[0] | |
}); | |
break; | |
default: | |
callback('Unknown image type'); | |
break; | |
} | |
} | |
} | |
}); | |
// Define a sample variable type for regexes. | |
router.addVariableType('regex', function(value, context, callback) { | |
var match; | |
try { | |
if ((match = context.regex.exec(value))) { | |
if (typeof(context.callback) == 'undefined') { | |
switch (match.length) { | |
case 1: | |
callback(false, match[0]); | |
break; | |
case 2: | |
callback(false, match[1]); | |
break; | |
default: | |
callback(false, match); | |
break; | |
} | |
} | |
else { | |
context.callback(value, match, callback); | |
} | |
} | |
else { | |
callback(true); | |
} | |
} | |
catch (e) { | |
callback(e); | |
} | |
}); | |
// Define a sample variable type for nodes. | |
router.addVariableType('node', function(value, context, callback) { | |
if (number.exec(value)) { | |
callback(false, { | |
'nid': parseInt(value, 10), | |
'title': 'Some node' | |
}); | |
} | |
else { | |
callback(true); | |
} | |
}); | |
tests = [ | |
'node/25', | |
'node', | |
'node/34/edit', | |
'node/34/delete', | |
'admin', | |
'admin/random/path', | |
'admin/settings/content', | |
'admin/settings/build/modules' | |
'images/assets/stationary/logo.png', | |
]; | |
for (path in tests) { | |
sys.puts('Resolving path ' . path); | |
route = router.route(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment