Class: Insights::API::Common::OpenApi::Docs::DocV3
- Inherits:
-
Object
- Object
- Insights::API::Common::OpenApi::Docs::DocV3
- Defined in:
- lib/insights/api/common/open_api/docs/doc_v3.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Instance Method Summary collapse
- #base_path ⇒ Object
- #definitions ⇒ Object
- #example_attributes(key) ⇒ Object
-
#initialize(content) ⇒ DocV3
constructor
A new instance of DocV3.
- #parameters ⇒ Object
- #paths ⇒ Object
- #routes ⇒ Object
- #schemas ⇒ Object
- #to_json(options = nil) ⇒ Object
-
#validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json') ⇒ Object
Validates data types against OpenAPI schema.
- #validate_parameters!(http_method, request_path, api_version, params) ⇒ Object
- #version ⇒ Object
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
#content ⇒ Object (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
#base_path ⇒ Object
69 70 71 |
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 69 def base_path @base_path ||= @content.fetch_path("servers", 0, "variables", "basePath", "default") end |
#definitions ⇒ Object
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 |
#parameters ⇒ Object
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 |
#paths ⇒ Object
73 74 75 |
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 73 def paths @content["paths"] end |
#routes ⇒ Object
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(base_path, path).gsub(/{\w*}/, ":id") {:path => p, :verb => verb.upcase} end end end end |
#schemas ⇒ Object
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 |
#to_json(options = nil) ⇒ Object
77 78 79 |
# File 'lib/insights/api/common/open_api/docs/doc_v3.rb', line 77 def to_json( = nil) content.to_json() end |
#validate!(http_method, request_path, api_version, payload, payload_content_type = 'application/json') ⇒ Object
Validates data types against OpenAPI schema
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 |
#version ⇒ Object
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 |