class OpenapiBuilder::Core

Attributes

data[R]

Public Class Methods

new(path_to_spec) click to toggle source
# File lib/openapi_builder/core.rb, line 18
def initialize(path_to_spec)
  @dirname = File.dirname(path_to_spec)
  @data = load_file(path_to_spec)
  load_paths
  load_components
end

Public Instance Methods

to_json() click to toggle source
# File lib/openapi_builder/core.rb, line 12
def to_json
  @data.to_json
end
to_yaml() click to toggle source
# File lib/openapi_builder/core.rb, line 8
def to_yaml
  @data.to_yaml
end

Private Instance Methods

component_dirs() click to toggle source
# File lib/openapi_builder/core.rb, line 33
def component_dirs
  Dir["#{@dirname}/components/*"].select { |component_path| File.directory? component_path }
end
load_component(component_path) click to toggle source
# File lib/openapi_builder/core.rb, line 37
def load_component(component_path)
  Hash.new.tap do |block|
    Dir["#{component_path}/*"].each do |file|
      content = load_file(file)
      next unless content

      block[File.basename(file, '.*')] = content
    end
  end
end
load_components() click to toggle source
# File lib/openapi_builder/core.rb, line 25
def load_components
  @data["components"] = Hash.new.tap do |components|
    component_dirs.each do |component_path|
      components[File.basename(component_path)] = load_component(component_path)
    end
  end
end
load_file(path) click to toggle source
# File lib/openapi_builder/core.rb, line 60
def load_file(path)
  case File.extname(path)
  when ".yml", ".yaml"
    YAML.load_file(path)
  when ".json"
    JSON.parse(File.read(path))
  end
end
load_paths() click to toggle source
# File lib/openapi_builder/core.rb, line 48
def load_paths
  @data["paths"] = Hash.new.tap do |paths|
    Dir["#{@dirname}/paths/*"].each do |file|
      content = load_file(file)
      next unless content

      key = File.basename(file, '.*').gsub('@', '/')
      paths["/#{key}"] = content
    end
  end
end