Class: OpenapiRuby::Core::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_ruby/core/document.rb

Constant Summary collapse

OPENAPI_VERSION =
"3.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (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 set_tags(tags)
  @data["tags"] = tags if tags.any?
end

#to_hObject



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_yamlObject



44
45
46
47
# File 'lib/openapi_ruby/core/document.rb', line 44

def to_yaml
  require "yaml"
  @data.to_yaml
end

#valid?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/openapi_ruby/core/document.rb', line 49

def valid?
  validate.empty?
end

#validateObject



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.message}]
end