Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Export utility functions for testing

  • Loading branch information...
commit c03df8be693e64fbcda097064a3e9682c9743965 1 parent fd46ea3
@angelini authored
Showing with 37 additions and 30 deletions.
  1. +1 −1  example/server.js
  2. +36 −29 lib/resty.js
View
2  example/server.js
@@ -3,7 +3,7 @@ var resty = require('../resty');
var app = connect.createServer();
app.use(connect.query());
-app.use(resty(__dirname + '/resources'));
+app.use(resty.middleware(__dirname + '/resources'));
app.listen(3000);
console.log('Server listening on port 3000');
View
65 lib/resty.js
@@ -3,46 +3,46 @@ var fs = require('fs');
var path = require('path');
// Utility Functions
-function isInt(value){
- if((parseFloat(value, 10) == parseInt(value, 10)) && !isNaN(value)){
- return true;
- } else {
- return false;
- }
-}
-
-function send(res, response, code) {
+var isInt = exports._isInt = function(value) {
+ if ((parseFloat(value, 10) == parseInt(value, 10)) && ! isNaN(value)) {
+ return true;
+ } else {
+ return false;
+ }
+};
+
+var send = exports._send = function(res, response, code) {
res.statusCode = code || 200;
res.write(JSON.stringify(response));
res.end();
-}
+};
-function readResources(resource_dir) {
+var readResources = exports._readResources = function(resource_dir) {
var i = 0;
var resources = {};
var level = fs.readdirSync(resource_dir);
- for(; i < level.length; i++) {
+ for (; i < level.length; i++) {
var stat = fs.statSync(path.join(resource_dir, level[i]));
- if(stat.isDirectory()) {
+ if (stat.isDirectory()) {
resources[level[i]] = readResources(path.join(resource_dir, level[i]));
- resources[level[i]].main = require(path.join(resource_dir, level[i], level[i] + '.js'));
+ resources[level[i]]._main = require(path.join(resource_dir, level[i], level[i] + '.js'));
}
}
return resources;
-}
+};
-function writeResponse(err, obj, res) {
- if(err) {
+var writeResponse = exports._writeResponse = function(err, obj, res) {
+ if (err) {
return send(res, err, err.statusCode || 500);
}
send(res, obj, obj.statusCode || 200);
-}
+};
// Middleware
-module.exports = function resty(resource_dir) {
+exports.middleware = function resty(resource_dir) {
var resources = readResources(resource_dir);
return function(req, res, next) {
@@ -58,34 +58,40 @@ module.exports = function resty(resource_dir) {
query: req.query
};
- if(isInt(components[components.length - 1])) {
+ if (isInt(components[components.length - 1])) {
collection = false;
}
var args = [];
var resource = resources;
- for(; i < components.length; i += 2) {
+ for (; i < components.length; i += 2) {
resource = resource[components[i]];
- if(!resource) {
- return send(res, {error: 'Resource Not Found'}, 404);
+ if (!resource) {
+ return send(res, {
+ error: 'Resource Not Found'
+ },
+ 404);
}
- if((i + 2) >= components.length && collection) {
+ if ((i + 2) >= components.length && collection) {
break;
}
args.push(components[i + 1]);
}
- if(collection) {
- resource = resource.main.Collection;
+ if (collection) {
+ resource = resource._main.Collection;
} else {
- resource = resource.main.Resource;
+ resource = resource._main.Resource;
}
- if(!resource || !resource[method]) {
- return send(res, {error: 'Method Not Found'}, 404);
+ if (!resource || ! resource[method]) {
+ return send(res, {
+ error: 'Method Not Found'
+ },
+ 404);
}
args.push(function(err, obj) {
@@ -94,3 +100,4 @@ module.exports = function resty(resource_dir) {
resource[method].apply(context, args);
}
};
+
Please sign in to comment.
Something went wrong with that request. Please try again.