Last active
January 14, 2016 17:21
-
-
Save arleslie/82618d2f6bc3d5834a79 to your computer and use it in GitHub Desktop.
Enom API Docs to PHP Functions
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
// This is a quick and dirty scraper for Enom's API docs to generate php functions for arleslie/enom-php | |
// And by dirty, I mean like a cheap hooker that hasn't bathed for days. | |
var request = require('request'), | |
cheerio = require('cheerio'); | |
var baseurl = 'http://www.enom.com/APICommandCatalog/API%20topics/'; | |
request(baseurl + 'api_Command_Categories.htm', function(err, resp, body) { | |
var $ = cheerio.load(body); | |
$('#contentBody > .row > ul > li > a').each(function(i, link) { | |
request(baseurl + $(link).attr('href'), parseAPIpage); | |
}); | |
}); | |
function parseAPIpage(err, resp, body) { | |
var $ = cheerio.load(body); | |
var params = {}; | |
var title = $('title').text(); | |
$('table').first().find('tbody > tr').each(function(i, row) { | |
var param = $(row).find('td:nth-child(1)').text().trim(); | |
var status = $(row).find('td:nth-child(2)').text().trim(); | |
var description = $(row).find('td:nth-child(3)').text().trim(); | |
if (param !== 'ResponseType' && param !== 'UID' && param !== 'PW') { | |
params[param] = { | |
'name' : param, | |
'status' : status, | |
'description' : description | |
}; | |
} | |
}); | |
var output = "public function " + title + "("; | |
Object.keys(params).forEach(function(key) { | |
var param = params[key]; | |
output += '$' + param.name; | |
if (param.status.indexOf('Optional') > -1) { | |
var def = 'false'; | |
if (param.status.indexOf('default value') > -1) { | |
def = param.status.replace(/Optional; default value is /g, ''); | |
} | |
output += ' = ' +def; | |
} | |
output += ','; | |
}); | |
output = output.slice(0, -1); | |
output += ")\n{\n\treturn $this->request([\n\t\t'command' => '"+title+"'"; | |
Object.keys(params).forEach(function(key) { | |
var param = params[key]; | |
output += ",\n\t\t'" + param.name + "' => $"+param.name; | |
}); | |
output += "\n\t]);\n}"; | |
console.log(output); | |
console.log("\n\n\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment