Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby support #20

Open
bparanj opened this issue May 26, 2023 · 1 comment
Open

Add Ruby support #20

bparanj opened this issue May 26, 2023 · 1 comment

Comments

@bparanj
Copy link

bparanj commented May 26, 2023

Adding ruby to the targets array generates Ruby code snippets. Tested it on Mac and Ubuntu. Code:

'use strict';

const fs = require('fs');
const OpenAPISnippet = require('openapi-snippet');
const yaml = require('js-yaml');
const args = require('yargs').argv;

let targets = ['node_request','shell_curl', 'shell_httpie', 'python_python3', 'php_curl', 'php_http1', 'php_http2', 'ruby'];

if (args.targets) {
	targets = args.targets.split(',');
}

function enrichSchema(schema){
	for(var path in schema.paths){
		for(var method in schema.paths[path]){
			var generatedCode = OpenAPISnippet.getEndpointSnippets(schema, path, method, targets);
			schema.paths[path][method]["x-codeSamples"] = [];
			for(var snippetIdx in generatedCode.snippets){
				var snippet = generatedCode.snippets[snippetIdx];
				schema.paths[path][method]["x-codeSamples"][snippetIdx] = { "lang": snippet.title, "source": snippet.content };
			}
		}
	}
	return schema;
}

if(!args.input){
	throw new Error("Please pass the OpenAPI JSON schema as argument.");
}

// Try to interpret as YAML first, based on file extension
if(args.input.indexOf('yml') !== -1 || args.input.indexOf('yaml') !== -1){
	try {
		let schema = yaml.safeLoad(fs.readFileSync(args.input, 'utf8'));
		schema = enrichSchema(schema);
		console.log(JSON.stringify(schema));
	} catch (e) {
		console.log(e);
	}
} else {
	fs.readFile(args.input, (err, data) => {
		if (err) throw err;
		let schema = JSON.parse(data);
		schema = enrichSchema(schema);
		console.log(JSON.stringify(schema));
	});
}
@andrzejwp
Copy link
Contributor

@bparanj Thanks for this issue, but is it something you could formulate as a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants