Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 99eef601d9a49d055f44f09ddf7920e9b7bbc70f @angelini committed Jan 11, 2012
2 .gitignore
@@ -0,0 +1,2 @@
+node_modules
+*.swp
23 LICENSE
@@ -0,0 +1,23 @@
+Copyright 2011 Alex Angelini
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
0 README.md
No changes.
0 example/resources/songs/songs.js
No changes.
0 example/resources/users/contacts/contacts.js
No changes.
15 example/resources/users/users.js
@@ -0,0 +1,15 @@
+var Users = {
+ Resource: {
+ get: function(uid, callback) {
+ callback(null, {uid: uid, query: this.query});
+ }
+ },
+
+ Collection: {
+ get: function(callback) {
+ callback(null, {all: 'users'});
+ }
+ }
+}
+
+module.exports = Users;
9 example/server.js
@@ -0,0 +1,9 @@
+var connect = require('connect');
+var resty = require('../resty');
+
+var app = connect.createServer();
+app.use(connect.query());
+app.use(resty(__dirname + '/resources'));
+app.listen(3000);
+
+console.log('Server listening on port 3000');
96 lib/resty.js
@@ -0,0 +1,96 @@
+// Requires
+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) {
+ res.statusCode = code || 200;
+ res.write(JSON.stringify(response));
+ res.end();
+}
+
+function readResources(resource_dir) {
+ var i = 0;
+ var resources = {};
+ var level = fs.readdirSync(resource_dir);
+
+ for(; i < level.length; i++) {
+ var stat = fs.statSync(path.join(resource_dir, level[i]));
+ 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'));
+ }
+ }
+
+ return resources;
+}
+
+function writeResponse(err, obj, res) {
+ if(err) {
+ return send(res, err, 500);
+ }
+
+ send(res, obj);
+}
+
+// Middleware
+module.exports = function resty(resource_dir) {
+ var resources = readResources(resource_dir);
+
+ return function(req, res, next) {
+ var i = 0;
+ var collection = true;
+ var method = req.method.toLowerCase();
+ var components = req.url.split('?')[0].split('/');
+ components.shift();
+
+ var context = {
+ body: req.body,
+ cookies: req.cookies,
+ query: req.query
+ };
+
+ if(isInt(components[components.length - 1])) {
+ collection = false;
+ }
+
+ var args = [];
+ var resource = resources;
+ for(; i < components.length; i += 2) {
+ resource = resource[components[i]];
+
+ if(!resource) {
+ return send(res, {error: 'Resource Not Found'}, 404);
+ }
+
+ if((i + 2) >= components.length && collection) {
+ break;
+ }
+
+ args.push(components[i + 1]);
+ }
+
+ if(collection) {
+ resource = resource.main.Collection;
+ } else {
+ resource = resource.main.Resource;
+ }
+
+ if(!resource || !resource[method]) {
+ return send(res, {error: 'Method Not Found'}, 404);
+ }
+
+ args.push(function(err, obj) {
+ writeResponse(err, obj, res);
+ });
+ resource[method].apply(context, args);
+ }
+};
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "resty",
+ "description": "Build quick and simple RESTfull APIs using Node.js and connect",
+ "keywords": ["rest", "node", "connect", "simple"],
+ "version": "0.0.1",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/SoapyIllusions/resty"
+ },
+ "author": "Alex Angelini <alex.louis.angelini@gmail.com>",
+ "main": "resty",
+ "engines": {
+ "node": ">=0.4.10"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/SoapyIllusions/resty/raw/master/LICENSE"
+ }
+ ]
+}
1 resty.js
@@ -0,0 +1 @@
+module.exports = require('./lib/resty');

0 comments on commit 99eef60

Please sign in to comment.