Class: Insights::API::Common::OpenApi::Docs::DocV3

Inherits:
Object
  • Object
show all
Defined in:
lib/insights/api/common/open_api/docs/doc_v3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ DocV3

Returns a new instance of DocV3.



13
14
15
16
17
18
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 13

def initialize(content)
  spec_version = content["openapi"]
  raise "Unsupported OpenAPI Specification version #{spec_version}" unless spec_version =~ /\A3\..*\z/

  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 11

def content
  @content
end

Instance Method Details

#definitionsObject



59
60
61
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 59

def definitions
  schemas
end

#example_attributes(key) ⇒ Object



63
64
65
66
67
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 63

def example_attributes(key)
  schemas[key]["properties"].each_with_object({}) do |(col, stuff), hash|
    hash[col] = stuff["example"] if stuff.key?("example")
  end
end

#parametersObject



51
52
53
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 51

def parameters
  @parameters ||= ::Insights::API::Common::OpenApi::Docs::ComponentCollection.new(self, "components/parameters")
end

#pathsObject



73
74
75
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 73

def paths
  @content["paths"]
end

#routesObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 81

def routes
  @routes ||= begin
    paths.flat_map do |path, hash|
      hash.collect do |verb, _details|
        p = File.join(server_base_path, path).gsub(/{\w*}/, ":id")
        {:path => p, :verb => verb.upcase}
      end
    end
  end
end

#schemasObject



55
56
57
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 55

def schemas
  @schemas ||= ::Insights::API::Common::OpenApi::Docs::ComponentCollection.new(self, "components/schemas")
end

#server_base_pathObject



69
70
71
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 69

def server_base_path
  @server_base_path ||= @content.fetch_path("servers", 0, "variables", "basePath", "default")
end

#to_json(options = nil) ⇒ Object



77
78
79
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 77

def to_json(options = nil)
  content.to_json(options)
end

#validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json') ⇒ Object

Validates data types against OpenAPI schema

Parameters:

  • http_method (String)

    POST/PATCH/…

  • request_path (String)

    i.e. /api/sources/v1.0/sources

  • api_version (String)

    i.e. “v1.0”, has to be part of request_path

  • payload (String)

    JSON if payload_content_type == ‘application/json’

  • payload_content_type (String) (defaults to: 'application/json')

Raises:

  • OpenAPIParser::OpenAPIError



29
30
31
32
33
34
35
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 29

def validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json')
  path = request_path.split(api_version)[1]
  raise "API version not found in request_path" if path.nil?

  request_operation = validator_doc.request_operation(http_method.to_s.downcase, path)
  request_operation.validate_request_body(payload_content_type, payload)
end

#validate_parameters!(http_method, request_path, api_version, params) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 37

def validate_parameters!(http_method, request_path, api_version, params)
  path = request_path.split(api_version)[1]
  raise "API version not found in request_path" if path.nil?

  request_operation = validator_doc.request_operation(http_method.to_s.downcase, path)
  return unless request_operation

  request_operation.validate_request_parameter(params, {})
end

#versionObject



47
48
49
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 47

def version
  @version ||= Gem::Version.new(content.fetch_path("info", "version"))
end