Class: Steppe::OpenAPIVisitor

Inherits:
Plumb::JSONSchemaVisitor
  • Object
show all
Defined in:
lib/steppe/openapi_visitor.rb

Constant Summary collapse

ENVELOPE =
{
  'openapi' => '3.0.0'
}.freeze
PARAMETERS_IN =
%i[query path].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(node, root: true) ⇒ Object



9
10
11
12
13
14
# File 'lib/steppe/openapi_visitor.rb', line 9

def self.call(node, root: true)
  data = new.visit(node)
  return data unless root

  ENVELOPE.merge(data)
end

.from_request(service, request) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/steppe/openapi_visitor.rb', line 16

def self.from_request(service, request)
  data = call(service)
  url = request.base_url.to_s
  return data if data['servers'].any? { |s| s['url'] == url }

  data['servers'] << { 'url' => url, 'description' => 'Current server' }
  data
end

Instance Method Details

#visit_endpoint_security(schemes) ⇒ Object



105
106
107
# File 'lib/steppe/openapi_visitor.rb', line 105

def visit_endpoint_security(schemes)
  schemes.map { |name, scopes| { name => scopes } }
end

#visit_parameters(query_schema, header_schema) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/steppe/openapi_visitor.rb', line 109

def visit_parameters(query_schema, header_schema)
  specs = query_schema._schema.each.with_object({}) do |(name, type), h|
    h[name.to_s] = type if PARAMETERS_IN.include?(type.[:in])
  end
  params = specs.map do |name, type|
    spec = visit(type)

    ins = spec.delete('in')&.to_s

    {
      'name' => name,
      'in' => ins,
      'description' => spec.delete('description'),
      'example' => spec.delete('example'),
      'required' => (ins == 'path'),
      'schema' => spec.except('in', 'desc', 'options')
    }.compact
  end

  header_schema._schema.each.with_object(params) do |(key, type), list|
    spec = visit(type)
    list << { 
      'name' => key.to_s, 
      'in' => 'header', 
      'description' => spec.delete('description'),
      'example' => spec.delete('example'),
      'required' => !key.optional?,
      'schema' => spec.except('in', 'desc', 'options')
    }.compact
  end
end

#visit_request_body(schemas) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/steppe/openapi_visitor.rb', line 141

def visit_request_body(schemas)
  return {} if schemas.empty?

  content = schemas.each.with_object({}) do |(content_type, schema), h|
    h[content_type] = { 'schema' => visit(schema) }
  end

  { 'required' => true, 'content' => content }
end

#visit_security_schemes(schemes) ⇒ Object



151
152
153
# File 'lib/steppe/openapi_visitor.rb', line 151

def visit_security_schemes(schemes)
  schemes.transform_values(&:to_openapi)
end