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

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

Defined Under Namespace

Classes: ComponentCollection, DocV3, ObjectDefinition

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(glob) ⇒ Docs

Returns a new instance of Docs.



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

def initialize(glob)
  @cache = {}
  glob.each { |f| load_file(f) }
end

Class Method Details

.instanceObject



10
11
12
# 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

Instance Method Details

#[](version) ⇒ Object



38
39
40
# File 'lib/insights/api/common/open_api/docs.rb', line 38

def [](version)
  @cache[version]
end

#load_file(file) ⇒ Object



19
20
21
22
# 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

#routesObject



42
43
44
45
46
47
48
49
# 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) ⇒ Object



24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
34
35
36
# 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