class SwaggerYard::Swagger
Attributes
specification[R]
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
# File lib/swagger_yard/swagger.rb, line 15 def self.new(*args) return OpenAPI.object_new(*args) if SwaggerYard.config.swagger_version.start_with?("3.0") super end
Also aliased as: object_new
new(spec = Specification.new)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 22 def initialize(spec = Specification.new) @specification = spec end
Public Instance Methods
to_h()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 26 def to_h metadata.merge(definitions).merge(model_definitions) end
Private Instance Methods
definitions()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 35 def definitions { "paths" => paths(specification.path_objects), "tags" => tags(specification.tag_objects), "securityDefinitions" => security_defs(specification.security_objects) } end
metadata()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 45 def metadata { "swagger" => "2.0", "info" => Info.new.to_h }.merge(uri_info) end
model(mod)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 133 def model(mod) h = {} if !mod.properties.empty? || mod.inherits.empty? h["type"] = "object" h["properties"] = Hash[mod.properties.map {|p| [p.name, property(p)]}] h["required"] = mod.properties.select(&:required?).map(&:name) if mod.properties.detect(&:required?) end h["discriminator"] = mod.discriminator if mod.discriminator # Polymorphism unless mod.inherits.empty? all_of = mod.inherits.map { |name| Type.new(name).schema_with(model_path: model_path) } all_of << h unless h.empty? if all_of.length == 1 && mod.description.empty? h.update(all_of.first) else h = { "allOf" => all_of } end end # Description h["description"] = mod.description unless mod.description.empty? h["example"] = mod.example if mod.example h["additionalProperties"] = mod.additional_properties if !mod.additional_properties.nil? h end
model_definitions()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 41 def model_definitions { "definitions" => models(specification.model_objects) } end
model_path()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 31 def model_path Type::MODEL_PATH end
models(model_objects)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 129 def models(model_objects) Hash[model_objects.map { |name, mod| [name, model(mod)] }] end
operation(op)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 76 def operation(op) op_hash = { "tags" => op.tags, "operationId" => op.operation_id, "parameters" => parameters(op.parameters), "responses" => responses(op.responses_by_status, op), } op_hash["description"] = op.description unless op.description.empty? op_hash["summary"] = op.summary unless op.summary.empty? authorizations = op.api_group.authorizations unless authorizations.empty? op_hash["security"] = authorizations.map {|k,v| { k => v} } end op_hash.update(op.extended_attributes) end
operations(ops)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 68 def operations(ops) expanded_ops = ops.map do |meth, op| [meth, operation(op)] end Hash[expanded_ops] end
parameters(params)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 95 def parameters(params) params.map do |param| { "name" => param.name, "description" => param.description, "required" => param.required, "in" => param.param_type }.tap do |h| schema = param.type.schema_with(model_path: model_path) if h["in"] == "body" h["schema"] = schema else h.update(schema) end h["collectionFormat"] = 'multi' if !Array(param.allow_multiple).empty? && h["items"] end end end
paths(paths)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 64 def paths(paths) Hash[paths.path_items.map {|path,pi| [path, operations(pi.operations)] }] end
property(prop)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 165 def property(prop) prop.type.schema_with(model_path: model_path).tap do |h| unless h['$ref'] h["description"] = prop.description if prop.description && !prop.description.strip.empty? if prop.nullable h["x-nullable"] = true if h["type"] h["type"] = [h["type"], "null"] end end h["example"] = prop.example if prop.example end end end
response(resp, op)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 117 def response(resp, op) {}.tap do |h| h['description'] = resp && resp.description || op.summary || '' h['schema'] = resp.type.schema_with(model_path: model_path) if resp && resp.type if resp && resp.example h['examples'] = { 'application/json' => resp.example } end end end
responses(responses_by_status, op)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 113 def responses(responses_by_status, op) Hash[responses_by_status.map { |status, resp| [status, response(resp, op)] }] end
security(obj)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 191 def security(obj) case obj.type when /api_?key/i { 'type' => 'apiKey', 'name' => obj.key, 'in' => obj.name } else { 'type' => 'basic' } end.tap do |result| result['description'] = obj.description if obj.description && !obj.description.empty? end end
security_defs(security_objects)
click to toggle source
# File lib/swagger_yard/swagger.rb, line 186 def security_defs(security_objects) config_defs = SwaggerYard.config.security_definitions config_defs.merge(Hash[security_objects.map { |name, obj| [name, security(obj)] }]) end
uri_info()
click to toggle source
# File lib/swagger_yard/swagger.rb, line 52 def uri_info uri = URI(SwaggerYard.config.api_base_path) host = uri.host host = "#{uri.host}:#{uri.port}" unless uri.port == uri.default_port { 'host' => host, 'basePath' => uri.request_uri, 'schemes' => [uri.scheme] } end