Created
October 10, 2013 14:27
-
-
Save jellea/6919269 to your computer and use it in GitHub Desktop.
Creates a API blueprint dynamically.
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
"use strict" | |
var fs = require('fs') | |
// Add Title, Sections and Endpoints first then generate | |
var Document = function(){ | |
this.data = "" | |
this.append = function(input) { | |
this.data = this.data + '\n' + input | |
console.log(input) | |
} | |
} | |
var doc = new Document | |
exports.addTitle = function (host, title, description) { | |
// Example output | |
// | |
// HOST: http://www.google.com/ | |
// | |
// --- Sample API v2 --- | |
// --- | |
// Welcome to our API. Comments support [Markdown](http://daringfireball.net/projects/markdown/syntax) syntax | |
// --- | |
doc.append('HOST: ' + host + '\n') | |
doc.append('--- ' + title + ' ---') | |
doc.append('---\n' + title + '\n---\n') | |
} | |
exports.addSection = function(title, description) { | |
// Example output | |
// -- | |
// Shopping Cart Resources | |
// The following is a section of resources related to the shopping cart | |
// -- | |
doc.append('--\n' + title + '\n' + description + '\n--\n') | |
} | |
exports.addEndpoint = function (httpmethod, endpoint, spec) { | |
console.log(spec) | |
// Example output | |
// This resource allows you to submit payment information to process your *shopping cart* items | |
// POST /payment | |
// { "cc": "12345678900", "cvc": "123", "expiry": "0112" } | |
// < 200 | |
// { "receipt": "/payment/receipt/1" } | |
// | |
// TODO if params transform from ':id' to '{id}' | |
if(spec.description) {doc.append(JSON.stringify(spec.description))} | |
doc.append(httpmethod.toUpperCase() + ' ' + endpoint) | |
if(spec.request) {doc.append(JSON.stringify(spec.request))} | |
doc.append('< 200') | |
if(spec.response) {doc.append(JSON.stringify(spec.response))} | |
doc.append('') // white space after endpoint | |
} | |
exports.generate = function (path) { | |
fs.writeFile(path, doc.data, function(err) { | |
if(err) { | |
console.log(err); | |
} else { | |
console.log("The file was saved!"); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment