Skip to content

Instantly share code, notes, and snippets.

@ameliaikeda
Created April 10, 2014 22:58
Show Gist options
  • Save ameliaikeda/10430413 to your computer and use it in GitHub Desktop.
Save ameliaikeda/10430413 to your computer and use it in GitHub Desktop.
function GameAPI(locals) {
this.settings = $.extend(locals, {
base: "",
csrf: "",
laravel: true
});
this.method = "GET";
}
GameAPI.prototype.getMethod(method) {
if (this.settings.laravel && method.toUpperCase() != "GET") {
this.method = method;
return "POST";
}
this.method = "";
return method;
}
GameAPI.prototype.getURL(route) {
return this.settings.base + "/" + route;
}
GameAPI.prototype.call(method, route, params) {
var url = this.getURL(route),
method = this.getMethod(method),
params = $.extend(params, {
_method: this.method,
_token: this.csrf
});
return $.ajax({
url: url,
type: method,
dataType: "json",
data: params
});
}
GameAPI.prototype.execute(method, route, params, callback) {
this.call(method, route, params, callback).done(callback);
}
GameAPI.prototype.get(route, params, callback) {
this.execute("GET", route, params, callback);
}
GameAPI.prototype.post(route, params, callback) {
this.execute("POST", route, params, callback);
}
GameAPI.prototype.put(route, params, callback) {
this.execute("PUT", route, params, callback);
}
GameAPI.prototype.delete(route, params, callback) {
this.execute("DELETE", route, params, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment