Last active
November 18, 2015 17:41
-
-
Save crgarridos/2860d45a53545b0cd055 to your computer and use it in GitHub Desktop.
Script to generate Retrofits Interfaces from Symfony API's doc site
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
var operations = $(".operation"); | |
var div = $("<div>"); | |
div.css({ | |
background: "whitesmoke", | |
/*position: "fixed", | |
top: 0, | |
//margin: "5%", | |
"overflow-y": "scroll",*/ | |
width : "100%", | |
height : "100%", | |
padding: "5%" | |
}); | |
var javaCode = "" | |
for (var i = 0 ; i < operations.length; i++) { | |
var api = operations.eq(i); | |
var method = api.find(".http_method i").text().trim(); | |
var description = api.find(".options li").text().trim(); | |
var path = api.find(".path").text().trim().replace("{_format}","json"); | |
var toReplace = "#replace_params#"; | |
var javaCode = "/** <br/>" | |
+ "* " + description + "<br/>" | |
+ toReplace //see below into params for cycle | |
+ "*/<br/>" | |
+ "@Deprecated<br/>" | |
+ "@" + method + "(\"" + path + "\")<br/>"; | |
javaCode += "void " + method.toLowerCase() + "(" | |
var vars = path.match(/{(.*?)}/g); | |
var paramsDescription = ""; | |
if (vars != null) { | |
for (var j = 0; j < vars.length; j++) { | |
var varName = vars[j].replace(/[{}]/g,""); | |
paramsDescription += "* @param " + varName + " the " + varName + "<br/>"; | |
javaCode += "@Path(\""+ varName + "\") int "+ varName + ", "; | |
} | |
}; | |
if (method != "GET" ) { | |
javaCode += "@Body JsonElement body, " | |
paramsDescription += "* @param body the object body to pass in the request<br/>"; | |
}; | |
paramsDescription += "* @param callback the retrofit callback with a list of objects<br/>" | |
var callback = "Callback<List<Object>> callback".replace(/</g,"<").replace(/>/g,">"); | |
javaCode += callback + ");<br/><br/>"; | |
div.append(javaCode.replace(toReplace,paramsDescription)) | |
}; | |
console.log(javaCode) | |
$("body").html("") | |
$("body").append(div) | |
//div[0].src = 'data:text/html;charset=utf-8,' + encodeURI(javaCode); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment