Skip to content

Commit

Permalink
Update for 2.0 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Lim committed Feb 4, 2020
1 parent c2d3890 commit b01b117
Show file tree
Hide file tree
Showing 2 changed files with 476 additions and 973 deletions.
78 changes: 54 additions & 24 deletions extract/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,37 @@ function createPostmanOutput(data){
description: "",
item: []
}

categoryData.forEach(function(item){
var code = item.code.replace(/^\#.*\n/m,"").replace(/^\$ /, "").replace(/-d'$/m, "-d '").replace(/:Bearer/gm, ": Bearer").replace(/:application/gm, ": application");
var curl = parse(code);
if (!item.code.match(/curl/)) return;
var code = item.code.replace(/^\#.*\n/m,"").replace(/^\$ /, "").replace(/-d'$/m, "-d '").replace(/:Bearer/gm, ": Bearer").replace(/:application/gm, ": application").replace(/curl \\/gm, "curl ");
if(code.match(/-d\s*$/m)){
code = code.replace(/-d\s*$/m, "-d '");
code += "'";
code = code.replace(/-d ''/m,"");
}
var curl = null;
var extra = "";
try
{
curl = parse(code);
}
catch(e){
}

if(!curl){
console.error("=======================================================================================\n" +
"ERROR: Can't parse curl for " + category + ": " + item.header + (item.subheader ? " (" + item.subheader + ")": "") +"\n" +
"---------------------------------------------------------------------------------------\n",
item.code);
"ERROR: Can't parse curl for " + category + ": " + item.header + (item.subheader ? " (" + item.subheader + ")": "") +"\n" +
"---------------------------------------------------------------------------------------\n",
);
console.error("original code\n", item.code);
console.error("sanitized code\name", code);
}
else{
postmanCategoryOutput.item.push(createPostmanEntry(item, curl));
}
});
postmanItems.push(postmanCategoryOutput);
if(postmanCategoryOutput.item.length > 0)
postmanItems.push(postmanCategoryOutput);
}
return {
"variables": [],
Expand All @@ -91,7 +107,10 @@ function createPostmanEntry(item, curl){
}
var name = item.header;
if(item.subheader) {
name += " (" + item.subheader + ")";
item.subheader = item.subheader.replace(/Request & Response\s*/g, "");
if(item.subheader){
name += " (" + item.subheader + ")";
}
}
var entry = {
"name": name,
Expand Down Expand Up @@ -173,6 +192,11 @@ function convertDeveloperWebsiteHTMLToJson(body){
return entries;
}

function createPostmanCollection(input, output){
var postmanOutput = createPostmanOutput(readJsonFromFile(input));
writeJSONToFile(postmanOutput,output);
}


////////////////////////////////////////////////////////////////////
// look for all code blocks in this section/item
Expand Down Expand Up @@ -257,21 +281,27 @@ function findBlock(text, type, start_text, end_text, start_position)

// Do the download and extraction
console.log(`Downloading dev docs: ${URL}....`);
request.get(URL, function (error, response, body) {
error = false;
if(error){
console.error("ERROR: Could not download page", error);
}
else {
// body = readFile(outputTempDevDocsHTML)
writeToFile(body, outputTempDevDocsHTML);
if (response.statusCode != 200){
console.warn("WARNING: Expected response code of 200 but got " + response.statusCode + ". Data may not be properly returned but will try parsing anyway");

const do_download = false;
if(do_download){
request.get(URL, function (error, response, body) {
error = false;
if(error){
console.error("ERROR: Could not download page", error);
}
var entries = convertDeveloperWebsiteHTMLToJson(body);
writeJSONToFile(entries, outputTempDevDocsToJsonFile);
var postmanOutput = createPostmanOutput(readJsonFromFile(outputTempDevDocsToJsonFile));
writeJSONToFile(postmanOutput,outputFinalPostmanFile);
}
});
else {
writeToFile(body, outputTempDevDocsHTML);
if (response.statusCode != 200){
console.warn("WARNING: Expected response code of 200 but got " + response.statusCode + ". Data may not be properly returned but will try parsing anyway");
}
var entries = convertDeveloperWebsiteHTMLToJson(body);
writeJSONToFile(entries, outputTempDevDocsToJsonFile);
createPostmanCollection(outputTempDevDocsToJsonFile, outputFinalPostmanFile);
}
});
}
else{
createPostmanCollection(outputTempDevDocsToJsonFile, outputFinalPostmanFile);
}


Loading

0 comments on commit b01b117

Please sign in to comment.