Skip to content

Instantly share code, notes, and snippets.

@Canop
Created December 7, 2015 14:42
Show Gist options
  • Save Canop/c71d606dbe363969b7b2 to your computer and use it in GitHub Desktop.
Save Canop/c71d606dbe363969b7b2 to your computer and use it in GitHub Desktop.
import world from './world.js';
class Query {
// Generic Stuff
constructor(){
this._path = 'rest';
this._body = null;
}
clone(){
let q = new Query;
q._path = this._path;
q._body = this._body;
return q;
}
path(...token){
let q = this.clone();
q._path += token.map(t -> '/'+t);
return q;
}
body(obj){
let q = this.clone();
q._body = JSON.stringify(obj);
return q;
}
get(){
return fetch(this._path).then(function(response){
return response.json();
});
}
post(){
return fetch(this._path, {method:"POST", body:this._body}).then(function(response){
return response.json();
});
}
// Stuff specific to Superpos
line(id){
if (typeof id === "number") return this.path("lines", id);
for (let i=0; i<world.lines.length; i++) {
if (world.lines[i] == id) {
return this.path("lines", i);
}
}
// throw new Error("unknow line:" +id);
}
}
export default new Query;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment