Created
December 7, 2015 14:42
-
-
Save Canop/c71d606dbe363969b7b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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