mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-05 05:36:09 +00:00
[Ruby] Rename files/folders (#534)
* Rename templateDir: rails5 -> ruby-on-rails-server * Rename script: rails5-petstore-server.sh -> ruby-on-rails-server-petstore.sh * Rename sample folder: rails5 -> ruby-on-rails * Rename script(openapi3): rails5-petstore-server.sh -> ruby-on-rails-server-petstore.sh * Update ruby-on-rails-server-petstore.sh * Rename templateDir: sinatra -> ruby-sinatra-server * Rename script: sinatra-petstore-server.sh -> ruby-sinatra-server-petstore.sh * Rename sample folder: sinatra -> ruby-sinatra * Update script * Rename templateDir: ruby -> ruby-client * Rename script: ruby-petstore.sh -> ruby-client-petstore.sh * Update scripts * Update samples - bin/ruby-sinatra-server-petstore.sh - bin/ruby-on-rails-server-petstore.sh - bin/ruby-client-petstore.sh - bin/security/ruby-client-petstore.sh
This commit is contained in:
committed by
William Cheng
parent
6c14b4271f
commit
26e70fe642
163
samples/server/petstore/ruby-sinatra/lib/swaggering.rb
Normal file
163
samples/server/petstore/ruby-sinatra/lib/swaggering.rb
Normal file
@@ -0,0 +1,163 @@
|
||||
require 'json'
|
||||
require 'sinatra/base'
|
||||
require 'sinatra/cross_origin'
|
||||
|
||||
class Configuration
|
||||
attr_accessor :base_path, :api_version, :swagger_version, :format_specifier
|
||||
|
||||
def initialize
|
||||
@api_version = '1.0'
|
||||
@base_path = 'http://localhost:4567'
|
||||
@swagger_version = '1.1'
|
||||
@format_specifier = ".json"
|
||||
end
|
||||
end
|
||||
|
||||
class Swaggering < Sinatra::Base
|
||||
register Sinatra::CrossOrigin
|
||||
|
||||
@@routes = {}
|
||||
@@configuration = Configuration.new
|
||||
|
||||
attr_accessor :configuration
|
||||
|
||||
def self.configure
|
||||
get("/resources" + @@configuration.format_specifier) {
|
||||
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
|
||||
|
||||
def self.add_route(method, path, swag={}, opts={}, &block)
|
||||
#fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path
|
||||
fullPath = path.gsub(/{(.*?)}/, ':\1')
|
||||
|
||||
accepted = case method.to_s.downcase
|
||||
when 'get'
|
||||
get(fullPath, opts, &block)
|
||||
true
|
||||
when 'post'
|
||||
post(fullPath, opts, &block)
|
||||
true
|
||||
when 'delete'
|
||||
delete(fullPath, opts, &block)
|
||||
true
|
||||
when 'put'
|
||||
put(fullPath, opts, &block)
|
||||
true
|
||||
else
|
||||
puts "Error adding route: #{method} #{fullPath}"
|
||||
false
|
||||
end
|
||||
|
||||
if accepted then
|
||||
resourcePath = swag["resourcePath"].to_s
|
||||
ops = @@routes[resourcePath]
|
||||
if ops.nil?
|
||||
ops = Array.new
|
||||
@@routes.merge!(resourcePath => ops)
|
||||
|
||||
get(resourcePath + @@configuration.format_specifier) do
|
||||
cross_origin
|
||||
Swaggering.to_api(resourcePath)
|
||||
end
|
||||
end
|
||||
|
||||
swag.merge!("httpMethod" => method.to_s.upcase)
|
||||
ops.push(swag)
|
||||
end
|
||||
end
|
||||
|
||||
def self.to_resource_listing
|
||||
apis = Array.new
|
||||
(@@routes.keys).each do |key|
|
||||
api = {
|
||||
"path" => (key + ".{format}"),
|
||||
"description" => "no description"
|
||||
}
|
||||
apis.push api
|
||||
end
|
||||
|
||||
resource = {
|
||||
"apiVersion" => @@configuration.api_version,
|
||||
"swaggerVersion" => @@configuration.swagger_version,
|
||||
"apis" => apis
|
||||
}
|
||||
|
||||
resource.to_json
|
||||
end
|
||||
|
||||
def self.to_api(resourcePath)
|
||||
apis = {}
|
||||
models = []
|
||||
|
||||
@@routes[resourcePath].each do |route|
|
||||
endpoint = route["endpoint"].gsub(/:(\w+)(\/?)/,'{\1}\2')
|
||||
path = (resourcePath + ".{format}" + endpoint)
|
||||
api = apis[path]
|
||||
if api.nil?
|
||||
api = {"path" => path, "description" => "description", "operations" => []}
|
||||
apis.merge!(path => api)
|
||||
end
|
||||
|
||||
parameters = route["parameters"]
|
||||
|
||||
unless parameters.nil? then
|
||||
parameters.each do |param|
|
||||
av_string = param["allowableValues"]
|
||||
unless av_string.nil?
|
||||
if av_string.count('[') > 0
|
||||
pattern = /^([A-Z]*)\[(.*)\]/
|
||||
match = pattern.match av_string
|
||||
case match.to_a[1]
|
||||
when "LIST"
|
||||
allowables = match.to_a[2].split(',')
|
||||
param["allowableValues"] = {
|
||||
"valueType" => "LIST",
|
||||
"values" => allowables
|
||||
}
|
||||
when "RANGE"
|
||||
allowables = match.to_a[2].split(',')
|
||||
param["allowableValues"] = {
|
||||
"valueType" => "RANGE",
|
||||
"min" => allowables[0],
|
||||
"max" => allowables[1]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
op = {
|
||||
"httpMethod" => route["httpMethod"],
|
||||
"description" => route["summary"],
|
||||
"responseClass" => route["responseClass"],
|
||||
"notes" => route["notes"],
|
||||
"nickname" => route["nickname"],
|
||||
"summary" => route["summary"],
|
||||
"parameters" => route["parameters"]
|
||||
}
|
||||
api["operations"].push(op)
|
||||
end
|
||||
|
||||
api_listing = {
|
||||
"apiVersion" => @@configuration.api_version,
|
||||
"swaggerVersion" => @@configuration.swagger_version,
|
||||
"basePath" => @@configuration.base_path,
|
||||
"resourcePath" => resourcePath,
|
||||
"apis" => apis.values,
|
||||
"models" => models
|
||||
}
|
||||
api_listing.to_json
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user