Class: Meta::Metadata

Inherits:
Object
  • Object
show all
Includes:
ExecutionMethods
Defined in:
lib/meta/application/metadata.rb

Defined Under Namespace

Modules: ExecutionMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExecutionMethods

#parse_parameters, #parse_request_body, #render_entity, #set_response

Constructor Details

#initialize(title: nil, description: nil, tags: [], parameters: {}, request_body: nil, responses: nil, scope: nil) ⇒ Metadata

Returns a new instance of Metadata.



67
68
69
70
71
72
73
74
75
# File 'lib/meta/application/metadata.rb', line 67

def initialize(title: nil, description: nil, tags: [], parameters: {}, request_body: nil, responses: nil, scope: nil)
  @title = title
  @description = description
  @tags = tags
  @parameters = parameters.is_a?(Parameters) ? parameters : Parameters.new(parameters)
  @request_body = request_body
  @responses = responses || {} # || { 204 => nil }
  @scope = scope
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def description
  @description
end

#parametersObject (readonly)

Returns the value of attribute parameters.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def parameters
  @parameters
end

#request_bodyObject (readonly)

Returns the value of attribute request_body.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def request_body
  @request_body
end

#responsesObject (readonly)

Returns the value of attribute responses.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def responses
  @responses
end

#scopeObject (readonly)

Returns the value of attribute scope.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def scope
  @scope
end

#tagsObject (readonly)

Returns the value of attribute tags.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def tags
  @tags
end

#titleObject (readonly)

Returns the value of attribute title.



65
66
67
# File 'lib/meta/application/metadata.rb', line 65

def title
  @title
end

Class Method Details

.new(meta = {}) ⇒ Object



118
119
120
# File 'lib/meta/application/metadata.rb', line 118

def new(meta = {})
  meta.is_a?() ? meta : super(**meta)
end

Instance Method Details

#[](key) ⇒ Object



77
78
79
# File 'lib/meta/application/metadata.rb', line 77

def [](key)
  send(key)
end

#generate_operation_doc(schemas, scope: []) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/meta/application/metadata.rb', line 81

def generate_operation_doc(schemas, scope: [])
  operation_object = {}

  operation_object[:summary] = title if title
  operation_object[:tags] = tags unless tags.empty?
  operation_object[:description] = description if description

  operation_object[:parameters] = parameters.to_swagger_doc

  if request_body
    schema = request_body.to_schema_doc(stage: :param, scope: self.scope + scope, schemas: schemas)
    if schema || true
      operation_object[:requestBody] = {
        content: {
          'application/json' => {
            schema: schema
          }
        }
      }
    end
  end

  operation_object[:responses] = responses.transform_values do |schema|
    {
      description: '', # description 属性必须存在
      content: schema ? {
        'application/json' => {
          schema: schema.to_schema_doc(stage: :render, scope: self.scope + scope, schemas: schemas)
        }
      } : nil
    }.compact
  end unless responses.empty?

  operation_object.compact
end