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

Public Class Methods

instance() click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 10
def self.instance
  @instance ||= new(Dir.glob(Rails.root.join("public", "doc", "openapi*.json")))
end
new(glob) click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 14
def initialize(glob)
  @cache = {}
  glob.each { |f| load_file(f) }
end

Public Instance Methods

[](version) click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 38
def [](version)
  @cache[version]
end
load_file(file) click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 19
def load_file(file)
  openapi_spec = JSON.parse(File.read(file))
  store_doc(DocV3.new(openapi_spec))
end
routes() click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 42
def routes
  @routes ||= begin
    @cache.each_with_object([]) do |(version, doc), routes|
      next unless /\d+\.\d+/ =~ version # Skip unless major.minor
      routes.concat(doc.routes)
    end
  end
end
store_doc(doc) click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 24
def store_doc(doc)
  update_doc_for_version(doc, doc.version.segments[0..1].join("."))
  update_doc_for_version(doc, doc.version.segments.first.to_s)
end
update_doc_for_version(doc, version) click to toggle source
# File lib/insights/api/common/open_api/docs.rb, line 29
def update_doc_for_version(doc, version)
  if @cache[version].nil?
    @cache[version] = doc
  else
    existing_version = @cache[version].version
    @cache[version] = doc if doc.version > existing_version
  end
end