Class: Jsapi::Meta::Definitions

Inherits:
Object
  • Object
show all
Defined in:
lib/jsapi/meta/definitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner = nil) ⇒ Definitions

Returns a new instance of Definitions.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jsapi/meta/definitions.rb', line 9

def initialize(owner = nil)
  @owner = owner
  @examples = {}
  @operations = {}
  @parameters = {}
  @request_bodies = {}
  @responses = {}
  @schemas = {}
  @rescue_handlers = []
  @self_and_included = [self]
end

Instance Attribute Details

#examplesObject (readonly)

Returns the value of attribute examples.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def examples
  @examples
end

#openapi_rootObject

Returns the value of attribute openapi_root.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def openapi_root
  @openapi_root
end

#operationsObject (readonly)

Returns the value of attribute operations.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def operations
  @operations
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def parameters
  @parameters
end

#request_bodiesObject (readonly)

Returns the value of attribute request_bodies.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def request_bodies
  @request_bodies
end

#rescue_handlersObject (readonly)

Returns the value of attribute rescue_handlers.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def rescue_handlers
  @rescue_handlers
end

#responsesObject (readonly)

Returns the value of attribute responses.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def responses
  @responses
end

#schemasObject (readonly)

Returns the value of attribute schemas.



6
7
8
# File 'lib/jsapi/meta/definitions.rb', line 6

def schemas
  @schemas
end

Instance Method Details

#add_example(name, keywords = {}) ⇒ Object



21
22
23
# File 'lib/jsapi/meta/definitions.rb', line 21

def add_example(name, keywords = {})
  @examples[name.to_s] = Example.new(keywords)
end

#add_operation(name = nil, keywords = {}) ⇒ Object



25
26
27
28
# File 'lib/jsapi/meta/definitions.rb', line 25

def add_operation(name = nil, keywords = {})
  name = name.nil? ? default_operation_name : name.to_s
  @operations[name] = Operation.new(name, keywords.reverse_merge(path: default_path))
end

#add_parameter(name, keywords = {}) ⇒ Object



30
31
32
33
# File 'lib/jsapi/meta/definitions.rb', line 30

def add_parameter(name, keywords = {})
  name = name.to_s
  @parameters[name] = Parameter.new(name, keywords)
end

#add_request_body(name, keywords = {}) ⇒ Object



35
36
37
# File 'lib/jsapi/meta/definitions.rb', line 35

def add_request_body(name, keywords = {})
  @request_bodies[name.to_s] = RequestBody.new(keywords)
end

#add_rescue_handler(klass, status: nil) ⇒ Object



39
40
41
# File 'lib/jsapi/meta/definitions.rb', line 39

def add_rescue_handler(klass, status: nil)
  @rescue_handlers << RescueHandler.new(klass, status: status)
end

#add_response(name, keywords = {}) ⇒ Object



43
44
45
46
# File 'lib/jsapi/meta/definitions.rb', line 43

def add_response(name, keywords = {})
  name = name.to_s
  @responses[name] = Response.new(keywords)
end

#add_schema(name, keywords = {}) ⇒ Object



48
49
50
51
# File 'lib/jsapi/meta/definitions.rb', line 48

def add_schema(name, keywords = {})
  name = name.to_s
  @schemas[name] = Schema.new(keywords)
end

#example(name) ⇒ Object



53
54
55
56
57
58
# File 'lib/jsapi/meta/definitions.rb', line 53

def example(name)
  return unless (name = name.to_s).present?

  definitions = @self_and_included.find { |d| d.examples.key?(name) }
  definitions.examples[name] if definitions
end

#include(definitions) ⇒ Object



60
61
62
63
64
# File 'lib/jsapi/meta/definitions.rb', line 60

def include(definitions)
  return if @self_and_included.include?(definitions)

  @self_and_included << definitions
end

#inspectObject

:nodoc:



66
67
68
69
70
71
72
73
# File 'lib/jsapi/meta/definitions.rb', line 66

def inspect # :nodoc:
  "#<#{self.class.name} #{
    %i[owner operations parameters request_bodies responses schemas
       examples openapi_root rescue_handlers].map do |name|
      "#{name}: #{instance_variable_get("@#{name}").inspect}"
    end.join(', ')
  }>"
end

#json_schema_document(name) ⇒ Object

Returns the JSON Schema document for the given schema as a Hash.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jsapi/meta/definitions.rb', line 76

def json_schema_document(name)
  schema(name)&.to_json_schema&.tap do |hash|
    definitions =
      @self_and_included
      .map(&:schemas)
      .reduce(&:merge)
      .except(name.to_s)
      .transform_values(&:to_json_schema)

    hash[:definitions] = definitions if definitions.any?
  end
end

#openapi_document(version = nil) ⇒ Object

Returns a hash representing the OpenAPI document for version. Raises an ArgumentError if version is not supported.



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/jsapi/meta/definitions.rb', line 91

def openapi_document(version = nil)
  version = OpenAPI::Version.from(version)

  (openapi_root&.to_openapi(version, self) || {}).tap do |h|
    h[:paths] = openapi_paths(version)

    if version.major == 2
      h.merge!(
        definitions: openapi_schemas(version),
        parameters: openapi_parameters(version),
        responses: openapi_responses(version)
      )
    else
      h.deep_merge!(
        components: {
          schemas: openapi_schemas(version),
          parameters: openapi_parameters(version),
          requestBodies: openapi_request_bodies(version),
          responses: openapi_responses(version),
          examples: openapi_examples
        }.compact.presence
      )
    end
  end.compact
end

#operation(name = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/jsapi/meta/definitions.rb', line 121

def operation(name = nil)
  if (name = name.to_s).present?
    definitions = @self_and_included.find { |d| d.operations.key?(name) }
    definitions.operations[name] if definitions
  elsif @operations.one?
    # return the one and only operation
    @operations.values.first
  end
end

#parameter(name) ⇒ Object



131
132
133
134
135
136
# File 'lib/jsapi/meta/definitions.rb', line 131

def parameter(name)
  return unless (name = name.to_s).present?

  definitions = @self_and_included.find { |d| d.parameters.key?(name) }
  definitions.parameters[name] if definitions
end

#request_body(name) ⇒ Object



138
139
140
141
142
143
# File 'lib/jsapi/meta/definitions.rb', line 138

def request_body(name)
  return unless (name = name.to_s).present?

  definitions = @self_and_included.find { |d| d.request_bodies.key?(name) }
  definitions.request_bodies[name] if definitions
end

#rescue_handler_for(exception) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/jsapi/meta/definitions.rb', line 145

def rescue_handler_for(exception)
  @self_and_included.each do |definitions|
    definitions.rescue_handlers.each do |rescue_handler|
      return rescue_handler if rescue_handler.match?(exception)
    end
  end
  nil
end

#response(name) ⇒ Object



154
155
156
157
158
159
# File 'lib/jsapi/meta/definitions.rb', line 154

def response(name)
  return unless (name = name.to_s).present?

  definitions = @self_and_included.find { |d| d.responses.key?(name) }
  definitions.responses[name] if definitions
end

#schema(name) ⇒ Object



161
162
163
164
165
166
# File 'lib/jsapi/meta/definitions.rb', line 161

def schema(name)
  return unless (name = name.to_s).present?

  definitions = @self_and_included.find { |d| d.schemas.key?(name) }
  definitions.schemas[name] if definitions
end