Skip to content

Instantly share code, notes, and snippets.

@dajester2013
Last active August 29, 2015 14:11
Show Gist options
  • Save dajester2013/9e281b66bc63491b97f0 to your computer and use it in GitHub Desktop.
Save dajester2013/9e281b66bc63491b97f0 to your computer and use it in GitHub Desktop.
RestAPI = (function () {
var GET='GET', PUT='PUT',POST='POST',DELETE='DELETE',PATCH='PATCH'
,allMethods = [GET,POST,PUT,PATCH,DELETE];
var doRequest = function(method, url, body, callback) {
console.log(arguments);
}
var p = {};
var RouteParser = new function() {
return {
parse: function(route) {
var parsed = []
,routeParts = route.replace(/^\/+|\s+/,"").split("/")
;
//console.log(routeParts);
var stack = [];
routeParts.map(function(part) {
if (/^:/.test(part)) {
stack.push(part);
} else {
if (stack.length) {
parsed.push(stack);
stack = [];
}
stack.push(part);
}
});
if (stack.length)
parsed.push(stack)
return parsed;
}
}
}
var APIStub = function(stubPath, methods, args) {
methods = methods || allMethods;
var stub = this
,currentArgs = []
,stubFn = function() {
currentArgs = Array.slice ? Array.slice(arguments) : Array.prototype.slice.call(arguments);
return stub;
}
stubFn.stub = this;
this.stubFn = stubFn;
this.stubpath = stubPath;
allMethods.map(function(m) {
if (~methods.indexOf(m)) {
stubFn[m.toLowerCase()] = (function() {return this[m.toLowerCase()].apply(this,arguments);}).bind(stub);
} else {
stub[m.toLowerCase()] = undefined;
}
});
this.getStubPath = function() {
return stubPath;
}
this.getCurrentArgs = function() {
return currentArgs;
}
this.getPath = function() {
var path = "";
var pathArgs = currentArgs.join("/")
if (this.parentStub) {
path = [this.parentStub.getPath(),this.getStubPath(), currentArgs.join("/")].join("/");
} else {
path = this.getStubPath();
}
this.resetArgs();
return path.replace(/\/+$/,"");
}
this.resetArgs = function() {
currentArgs = [];
}
return stubFn;
}
,HttpMethod = function(m) {
return function() {
var path = this.getPath();
doRequest(m,path,{},{});
}
}
;
allMethods.map(function(m) {
APIStub.prototype[m.toLowerCase()] = new HttpMethod(m);
});
APIStub.prototype.addChild = function(child) {
if (!(child instanceof APIStub)) {
throw "Invalid child - expected instance of APIStub";
}
child.parentStub = this;
this[child.getStubPath()] = child.stubFn;
this.stubFn[child.getStubPath()] = child.stubFn;
}
var RestAPI = function() {}
;
RestAPI.prototype.load = function(baseUrl, routes) {
var base = new APIStub(baseUrl)
;
if (routes && routes.map) {
routes.map(function(route) {
//console.log("parsing ",route)
parsed = RouteParser.parse(route);
var curNode = base;
var curPath = [];
parsed.map(function(stubpath) {
_stub = stubpath.shift();
if (curNode[_stub]) {
curNode = curNode[_stub];
} else {
var newNode = new APIStub(_stub,allMethods,stubpath);
curNode.stub.addChild(newNode.stub);
curNode = newNode;
}
});
});
}
return base;
}
return new RestAPI();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment