class Insights::API::Common::OpenApi::Docs::ComponentCollection

Attributes

doc[R]

Public Class Methods

new(doc, category) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 9
def initialize(doc, category)
  @doc = doc
  @category = category
end

Public Instance Methods

[](name) click to toggle source
Calls superclass method
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 14
def [](name)
  super || load_definition(name)
end
load_definition(name) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 18
def load_definition(name)
  raw_definition = @doc.content.fetch_path(*@category.split("/"), name)
  raise ArgumentError, "Failed to find definition for #{name}" unless raw_definition.kind_of?(Hash)

  definition = substitute_regexes(raw_definition)
  definition = substitute_references(definition)
  self[name] = ::Insights::API::Common::OpenApi::Docs::ObjectDefinition.new.replace(definition)
end

Private Instance Methods

fetch_ref_value(ref_path) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 40
def fetch_ref_value(ref_path)
  ref_paths = ref_path.split("/")
  property = ref_paths.last
  section  = ref_paths[1..-2]
  public_send(:[], property)
end
regexp_from_pattern(pattern) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 59
def regexp_from_pattern(pattern)
  Regexp.new(pattern)
end
substitute_references(object) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 29
def substitute_references(object)
  if object.kind_of?(Array)
    object.collect { |i| substitute_references(i) }
  elsif object.kind_of?(Hash)
    return fetch_ref_value(object["$ref"]) if object.keys == ["$ref"]
    object.each { |k, v| object[k] = substitute_references(v) }
  else
    object
  end
end
substitute_regexes(object) click to toggle source
# File lib/insights/api/common/open_api/docs/component_collection.rb, line 47
def substitute_regexes(object)
  if object.kind_of?(Array)
    object.collect { |i| substitute_regexes(i) }
  elsif object.kind_of?(Hash)
    object.each_with_object({}) do |(k, v), o|
      o[k] = k == "pattern" ? regexp_from_pattern(v) : substitute_regexes(v)
    end
  else
    object
  end
end