add swagger.yaml to ruby sinatra

This commit is contained in:
wing328
2015-12-13 17:39:52 +08:00
parent bef2dac022
commit 1dd05bb90f
5 changed files with 700 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
package io.swagger.codegen.languages;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenType;
@@ -8,10 +10,13 @@ import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
@@ -73,6 +78,7 @@ public class SinatraServerCodegen extends DefaultCodegen implements CodegenConfi
supportingFiles.add(new SupportingFile("config.ru", "", "config.ru"));
supportingFiles.add(new SupportingFile("Gemfile", "", "Gemfile"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("swagger.mustache","","swagger.yaml"));
}
public CodegenType getTag() {
@@ -213,5 +219,17 @@ public class SinatraServerCodegen extends DefaultCodegen implements CodegenConfi
return underscore(operationId);
}
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Swagger swagger = (Swagger)objs.get("swagger");
if(swagger != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(swagger));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return super.postProcessSupportingFileData(objs);
}
}

View File

@@ -26,6 +26,13 @@ class Swaggering < Sinatra::Base
cross_origin
Swaggering.to_resource_listing
}
# for swagger.yaml
get("/swagger.yaml") {
cross_origin
File.read("./swagger.yaml");
}
@@configuration ||= Configuration.new
yield(@@configuration) if block_given?
end

View File

@@ -0,0 +1 @@
{{{swagger-yaml}}}