Permalink
Browse files

Merge branch 'master' of github.com:mde/geddy into remove-model

  • Loading branch information...
2 parents 7016b13 + 6433d25 commit b4935c082d7f7c5379ce7771385e897f375b1319 mde committed Sep 16, 2012
Showing with 45 additions and 36 deletions.
  1. +36 −32 lib/app.js
  2. +5 −2 lib/sessions/stores/cookie.js
  3. +1 −1 package.json
  4. +3 −1 templates/base/router.js
View
@@ -97,7 +97,6 @@ var App = function () {
var controllerDir = 'app/controllers'
, dirList = _getDirList(controllerDir)
, item
- , ctors = {}
, ctor
, ctorActions;
@@ -296,8 +295,11 @@ var App = function () {
, actionName
, staticPath
, staticResp
+ , nonMethodRoutes
+ , acceptableMethods
, err
, errResp
+ , contentType
, body = ''
, bodyParams
, steps = {
@@ -401,18 +403,11 @@ var App = function () {
}
if (params) {
- if (params instanceof Error) {
- err = new errors.MethodNotAllowedError(method + ' method is not allowed.');
- errResp = new response.Response(resp);
- errResp.send(err.message, err.statusCode, {
- 'Content-Type': 'text/html'
- , 'Allow': params.allowed.join(', ')
- });
- }
- else {
- controllerName = params.controller;
+ controllerName = utils.string.camelize(params.controller, {initialCap: true});
+ // Shim -- router now returns snakeized name
+ params.controller = controllerName;
actionName = params.action;
- ctor = ctors[params.controller];
+ ctor = ctors[controllerName];
if (ctor) {
// Merge params from the URL and the query-string to produce a
@@ -421,16 +416,27 @@ var App = function () {
// If it's a plain form-post, save the request-body, and parse it into
// params as well
+ contentType = req.headers['content-type'];
if ((req.method == 'POST' || req.method == 'PUT') &&
- (req.headers['content-type'] &&
- (req.headers['content-type'].indexOf('form-urlencoded') > -1 ||
- req.headers['content-type'].indexOf('application/json') > -1))) {
+ (contentType &&
+ (contentType.indexOf('form-urlencoded') > -1 ||
+ contentType.indexOf('application/json') > -1))) {
+
+ bodyParams = {};
req.addListener('data', function (data) {
body += data.toString();
});
- // Handle the request once it's finished
+ // Parse the body into params once it's finished
req.addListener('end', function () {
- bodyParams = querystring.parse(body);
+ if (contentType.indexOf('form-urlencoded') > -1) {
+ bodyParams = querystring.parse(body);
+ }
+ else if (contentType.indexOf('application/json') > -1) {
+ try {
+ bodyParams = JSON.parse(body);
+ }
+ catch(e) {}
+ }
geddy.mixin(params, bodyParams);
req.body = body;
finish('parseBody');
@@ -512,13 +518,12 @@ var App = function () {
}
// no controller, 500 error
else {
- err = new errors.internalservererror('controller ' +
+ err = new errors.InternalServerError('controller ' +
params.controller + ' not found.');
- errresp = new response.response(resp);
- errresp.send(err.message, err.statuscode,
- {'content-type': 'text/html'});
+ errResp = new response.Response(resp);
+ errResp.send(err.message, err.statusCode,
+ {'Content-Type': 'text/html'});
}
- }
}
// Either static, 404, or 405
else {
@@ -534,28 +539,27 @@ var App = function () {
staticResp.sendFile(staticPath);
}
else {
-
// 405?
- var non_method_routes = router.all(reqUrl);
- if ( non_method_routes.length > 0 ){
-
+ nonMethodRoutes = router.all(reqUrl);
+ if (nonMethodRoutes.length){
// build a unique list of acceptable methods for this resource
- var acceptable_methods = {};
- non_method_routes.map(function(params){
- acceptable_methods[params.method] = true;
+ acceptableMethods = {};
+ nonMethodRoutes.map(function(params){
+ acceptableMethods[params.method] = true;
});
- acceptable_methods = Object.keys(acceptable_methods);
+ acceptableMethods = Object.keys(acceptableMethods);
// send a friendly error response
err = new errors.MethodNotAllowedError(
method + ' method not allowed. Please consider ' +
- acceptable_methods.join(', ').replace(/,\s(\w+)$/," or $1") +
+ acceptableMethods.join(', ').replace(/,\s(\w+)$/," or $1") +
' instead.');
errResp = new response.Response(resp);
errResp.send( err.message, err.statusCode, {'Content-Type': 'text/html'} );
// 404
- }else{
+ }
+ else{
err = new errors.NotFoundError(reqUrl + ' not found.');
errResp = new response.Response(resp);
errResp.send(err.message, err.statusCode,
@@ -42,9 +42,12 @@ Cookie.prototype = new (function () {
decipher = crypto.createDecipher('aes-256-cbc', secret +
this.id);
data = decipher.update(crypted, 'hex', 'utf8');
- data += decipher.final('utf8');
try {
- data = JSON.parse(data);
+ data += decipher.final('utf8');
+ try {
+ data = JSON.parse(data);
+ }
+ catch (e) {}
}
catch (e) {}
}
View
@@ -7,7 +7,7 @@
"REST",
"MVC"
],
- "version": "0.4.5",
+ "version": "0.4.6",
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"dependencies": {
"jake": "0.3.x",
View
@@ -17,7 +17,7 @@
*/
-var router = exports.router = new geddy.RegExpRouter();
+var router = new geddy.RegExpRouter();
router.get('/').to('Main.index');
@@ -38,3 +38,5 @@ router.get('/').to('Main.index');
// this.resource('countries');
// this.get('/print(.:format)').to('Hemispheres.print');
// });
+
+exports.router = router;

0 comments on commit b4935c0

Please sign in to comment.