Skip to content

Instantly share code, notes, and snippets.

@mars
Forked from mtsmfm/foo.rb
Created June 27, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mars/0570e25c6bf49c1313ce9cd185b3ed29 to your computer and use it in GitHub Desktop.
Save mars/0570e25c6bf49c1313ce9cd185b3ed29 to your computer and use it in GitHub Desktop.
convert from json hyper schema to swagger spec
require 'yaml'
require 'pathname'
require 'active_support/all'
def convert(f)
filename = Pathname.new(f)
yaml = YAML.load_file(filename)
name = filename.basename('.yml').to_s
File.write(filename, {
name.camelize => {
'type' => yaml['type'],
'properties' => yaml['definitions'].except('identity')
}
}.to_yaml)
api_file = 'schemata/api.yml'
api_yaml = YAML.load_file(api_file)
yaml['links'].each do |link|
api_yaml['paths'] << {
link['href'] => {
link['method'].downcase => {
'description' => link['description'],
'parameters' => link.dig('schema', 'properties') || nil,
'responses' => {
200 => {
'description' => name,
'schema' => {'$ref' => './x.yml#/definitions/X'}
}
}
}
}
}
end
File.write(
api_file,
api_yaml.to_yaml
)
end
Dir['schemata/**/*.yml'].each do |f|
next if f == 'schemata/api.yml'
convert(f)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment