Skip to content

Instantly share code, notes, and snippets.

@TrevorJTClarke
Created May 31, 2014 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TrevorJTClarke/47c4081ded585c8250fc to your computer and use it in GitHub Desktop.
Save TrevorJTClarke/47c4081ded585c8250fc to your computer and use it in GitHub Desktop.
API Route Module
function API (){
this._apiString = "";
//TODO: add this to check if we already have a param or not, then finish route
this.addQueryParam = function (){
// console.log(this._apiString.toString().search("\?"));
}
this.v = function (){
this._apiString += "/v1";
return this;
}
this.route = function (params){
params = (params) ? "/" + params : "";
return this._apiString + params;
}
this.addList = function (){
return "/list";
}
this.list = function (length){
length = (length) ? this.addList() + "?length=" + length : "";
return this._apiString + length;
}
this.limit = function (length){
length = length || 0;
var _limit = this.addList() + "?limit=" + length;
return this._apiString + _limit;
}
this.offset = function (start, finish){
start = start || 0;
finish = finish || 0;
var _offset = this.addList() + "?start=" + start + "&finish=" + finish;
return this._apiString + _offset;
}
}
API.prototype.user = function (){
this._apiString += "/user";
return this;
};
API.prototype.users = function (){
this._apiString += "/users";
return this;
};
API.prototype.profile = function (){
this._apiString += "/profile";
return this;
};
API.prototype.friends = function (){
this._apiString += "/friends";
return this;
};
// could add many more methods for other routes, but those are some basics
API.prototype.userProfile = function (id){
id = (id) ? "/" + id : "";
return this.v().user().profile().route() + id;
};
var usersFriendsRoute = new API().v().users().friends().route();
var userProfileRoute = new API().userProfile(1234);
var usersFriendsListRoute = new API().v().users().friends().list(20);
var usersFriendsListLimitRoute = new API().v().users().friends().limit(20);
var usersFriendsListOffsetRoute = new API().v().users().friends().offset(20, 60);
console.log("Basic Routes: ", usersFriendsRoute, userProfileRoute, usersFriendsListRoute);
console.log("Pagination: ", usersFriendsListLimitRoute, usersFriendsListOffsetRoute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment