json and gist


// 2011 Till Bovermann

// http://tangibleauditoryinterfaces.de 

// http://tai-studio.org 

// http://LFSaw.de 


// working:

// + list all gists for a specific user (well, currently only the first 30...)

// + get content of a gist by its id

// + edit/push gist (no file renaming, no file removal for now)

// + fork a gist (as long as it's not yours: It's a "feature" by github.)

// + delete a gist


// I purposfully did not add the username and password to the class. If that feels annoying to you, please tell me why it is wise to do so and not to tackle security issues. Also, please tell me how I should handle auth strings in curl... I'm unsure about that currently.


// if the following fails for you, please check if you have to update wslib's curl method by the one in 

// json/extString-curl.scd



q = ();


// set your username / password here if you wanna create personalized gists, but its optional.

q.username = nil;

q.password = nil;



////////////////////////////// get existing gists /////////////



q.list = Gist.allGistsFor("LFSaw", q.username, q.password);


// pretty print

q.list.do(_.prettyprint)


// get content for all and print them out (this micht take a long time since it does an HTTP GET for every gist...)

q.list.do{|gist| gist.pull.prettyprint(true)}



{|e| "% // %\n".postf(e, e.description); e.files.do{|f| "\t%\n".postf(f.filename)};"".postln}


// the gist dict for the latest gist of the user "LFSaw"

q.gist = q.list.first.pull

q.gist.filenames;


// get content

q.gist[q.gist.filenames.asArray.first];



// the gist dict for an example gist for the OpenSynthDef world

q.gist = Gist(1113618).pull;

q.gist.files.asArray.first["content"];


// open in default Browser (requires wslib & osx)

q.gist.html_url.openInFinder;


// You might want to evaluate it but do so on your own risk... someone could've put a bad unixCmd in there...

q.gist.files.asArray.first["content"].interpret.play; // start the server first...


// Open Documents for each file in the gist

q.gist.files.keysValuesDo{|key, value|

Document(key.asString, value["content"])

}


// fork that gist but mind you, you cannot fork your own gists for now!

q.forked = q.gist.fork(q.username, q.password)


q.forked.id == q.gist.id // should be different


////////////////////////////// push new gists /////////////


( // push a new gist

var descr = "My very-first SuperCollider gist";

var isPublic = true;

var content = Dictionary[

"SynthDef.sc" -> "SynthDef(\"out\", {Out.ar(0, FSinOsc.ar(247))})"

];


q.gist = Gist.createAndPush(descr, content, isPublic, q.username, q.password);

)


q.gist.delete(q.username, q.password)


// not there anymore...

q.gist.pull


// but you still have its last content:

q.gist.prettyprint


// alter the string

q.gist["SynthDef.sc"] = "SynthDef(\"out\", {Out.ar(0, LFPulse.ar(247))})";



// if you pushed this gist with a valid uid, you're able to push back your changes: 

q.gist.push(q.username, q.password)




( // post new anonymous gist

var descr = "My anonymous GIst gist";

var isPublic = true;

var content = Dictionary[

"SynthDef.sc" -> "SynthDef(\"out\", {Out.ar(0, FSinOsc.ar(247); \\haha)})"

];


q.gist = Gist.createAndPush(descr, content, isPublic);


q.gist.id;

)