class Insights::API::Common::OpenApi::Docs::DocV3
Attributes
content[R]
Public Class Methods
new(content)
click to toggle source
# 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
Public Instance Methods
definitions()
click to toggle source
# File lib/insights/api/common/open_api/docs/doc_v3.rb, line 59 def definitions schemas end
example_attributes(key)
click to toggle source
# 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
parameters()
click to toggle source
# 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
paths()
click to toggle source
# File lib/insights/api/common/open_api/docs/doc_v3.rb, line 73 def paths @content["paths"] end
routes()
click to toggle source
# 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
schemas()
click to toggle source
# 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_path()
click to toggle source
# 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)
click to toggle source
# 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')
click to toggle source
Validates data types against OpenAPI schema
@param http_method [String] POST/PATCH/… @param request_path [String] i.e. /api/sources/v1.0/sources @param api_version [String] i.e. “v1.0”, has to be part of request_path @param payload [String] JSON if payload_content_type == 'application/json' @param payload_content_type [String]
@raise OpenAPIParser::OpenAPIError
# 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)
click to toggle source
# 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
version()
click to toggle source
# 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
Private Instance Methods
validator_doc(opts = { :coerce_value => true, :datetime_coerce_class => DateTime })
click to toggle source
# File lib/insights/api/common/open_api/docs/doc_v3.rb, line 94 def validator_doc(opts = { :coerce_value => true, :datetime_coerce_class => DateTime }) @validator_doc ||= ::OpenAPIParser.parse(content, opts) end