Class: OpenapiRuby::Core::Document
- Inherits:
-
Object
- Object
- OpenapiRuby::Core::Document
- Defined in:
- lib/openapi_ruby/core/document.rb
Constant Summary collapse
- OPENAPI_VERSION =
"3.1.0"
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #add_path(template, path_item) ⇒ Object
-
#initialize(info: {}, servers: []) ⇒ Document
constructor
A new instance of Document.
- #set_components(components) ⇒ Object
- #set_security(security) ⇒ Object
- #set_tags(tags) ⇒ Object
- #to_h ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_yaml ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(info: {}, servers: []) ⇒ Document
Returns a new instance of Document.
10 11 12 13 14 15 16 17 |
# File 'lib/openapi_ruby/core/document.rb', line 10 def initialize(info: {}, servers: []) @data = { "openapi" => OPENAPI_VERSION, "info" => normalize_info(info), "paths" => {} } @data["servers"] = servers.map { |s| s.transform_keys(&:to_s) } if servers.any? end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/openapi_ruby/core/document.rb', line 8 def data @data end |
Instance Method Details
#add_path(template, path_item) ⇒ Object
19 20 21 22 |
# File 'lib/openapi_ruby/core/document.rb', line 19 def add_path(template, path_item) @data["paths"][template] ||= {} @data["paths"][template].merge!(path_item) end |
#set_components(components) ⇒ Object
24 25 26 |
# File 'lib/openapi_ruby/core/document.rb', line 24 def set_components(components) @data["components"] = components if components.any? end |
#set_security(security) ⇒ Object
28 29 30 |
# File 'lib/openapi_ruby/core/document.rb', line 28 def set_security(security) @data["security"] = security if security.any? end |
#set_tags(tags) ⇒ Object
32 33 34 |
# File 'lib/openapi_ruby/core/document.rb', line 32 def () @data["tags"] = if .any? end |
#to_h ⇒ Object
36 37 38 |
# File 'lib/openapi_ruby/core/document.rb', line 36 def to_h @data end |
#to_json(*_args) ⇒ Object
40 41 42 |
# File 'lib/openapi_ruby/core/document.rb', line 40 def to_json(*_args) JSON.pretty_generate(@data) end |
#to_yaml ⇒ Object
44 45 46 47 |
# File 'lib/openapi_ruby/core/document.rb', line 44 def to_yaml require "yaml" @data.to_yaml end |
#valid? ⇒ Boolean
49 50 51 |
# File 'lib/openapi_ruby/core/document.rb', line 49 def valid? validate.empty? end |
#validate ⇒ Object
53 54 55 56 57 58 |
# File 'lib/openapi_ruby/core/document.rb', line 53 def validate schemer = JSONSchemer.openapi(@data) schemer.validate.to_a rescue => e [{"error" => e.}] end |