class OasContrib::Resolver::Base

Basical command resolver class

Constants

DEFINED_FILE_EXT

@return [Array] approval file extensions

DIR_NAME_META

@return [String] the directory name of meta part files

DIR_NAME_MODEL

@return [String] the directory name of model part files

DIR_NAME_PATH

@return [String] the directory name of path part files

Attributes

data[R]

@!attribute [r] data

@return [Hash] parsed input data hash
spec[R]

@!attribute [r] spec

@return [OpenAPI::V3::Spec|OpenAPI::V2::Spec] mapped spec data object

Public Instance Methods

file_ext_check() click to toggle source

Check the file extensions is approved or not. @return [Boolean]

# File lib/oas_contrib/resolver/base.rb, line 35
def file_ext_check
  if @infile_ext && !DEFINED_FILE_EXT.include?(@infile_ext)
    raise "Undefined input file extension. #{@infile_ext}"
  end

  if @outfile_ext && !DEFINED_FILE_EXT.include?(@outfile_ext)
    raise "Undefined output file extension. #{@outfile_ext}"
  end
  true
end
input(path) click to toggle source

Load and parse the input file. @param [String] path input file path @return [Hash] parsed input data hash

# File lib/oas_contrib/resolver/base.rb, line 70
def input(path)
  @data = _input(path)
end
input_dir(dir) click to toggle source

Load and parse the files in target directory recursive. @param [String] dir input directory path @return [Hash] parsed input data hash

# File lib/oas_contrib/resolver/base.rb, line 85
def input_dir(dir)
  path = dir + '/**/*' + @infile_ext
  Dir.glob(path).sort.each_with_object({}, &input_lambda)
end
input_lambda() click to toggle source

Load and parse the file proc. @return [Proc]

# File lib/oas_contrib/resolver/base.rb, line 92
def input_lambda
  lambda do |file, result|
    hash = _input(file)
    key = hash.keys[0]
    result[key] = hash[key]
  end
end
output(hash, path) click to toggle source

Output a new file with mapped spec data hash. @param [Hash] hash mapped spec data hash @param [String] path output file path @return [IO]

# File lib/oas_contrib/resolver/base.rb, line 78
def output(hash, path)
  File.open(path, 'w') { |f| _output(hash, f) }
end
resolve() click to toggle source

Judge and generate OpenAPI specification object. @return [OpenAPI::V3::Spec|OpenAPI::V2::Spec] mapped spec data object

# File lib/oas_contrib/resolver/base.rb, line 60
def resolve
  @spec = OasContrib::OpenAPI::V2::Spec.new(@data) if v2?
  @spec = OasContrib::OpenAPI::V3::Spec.new(@data) if v3?
  raise 'Undefined OAS file.' unless @spec
  @spec.mapping
end
v2?() click to toggle source

Check the format of input file is OpenAPI v2 specificaion or not. @return [Boolean]

# File lib/oas_contrib/resolver/base.rb, line 54
def v2?
  @data['swagger'] =~ /^2/
end
v3?() click to toggle source

Check the format of input file is OpenAPI v3 specificaion or not. @return [Boolean]

# File lib/oas_contrib/resolver/base.rb, line 48
def v3?
  @data['openapi'] =~ /^3/
end

Private Instance Methods

_input(path) click to toggle source

Load and prase the file depending on the file extension. @param [String] path input file path @return [Hash] parsed input data hash

# File lib/oas_contrib/resolver/base.rb, line 105
def _input(path)
  puts "Load: #{path}"
  case @infile_ext
  when DEFINED_FILE_EXT[0] then JSON.parse(File.read(path))
  when DEFINED_FILE_EXT[1] then YAML.load_file(path)
  else raise ArgumentError, 'Undefined input file type'
  end
end
_output(hash, file) click to toggle source

Write the spec data hash depending on the file extension. @param [Hash] hash mapped spec data hash @param [String] file output file path @return [IO]

# File lib/oas_contrib/resolver/base.rb, line 118
def _output(hash, file)
  puts "Dist: #{file.path}"
  case @outfile_ext
  when DEFINED_FILE_EXT[0] then JSON.dump(hash, file)
  when DEFINED_FILE_EXT[1] then YAML.dump(hash, file)
  else raise ArgumentError, 'Undefined output file type'
  end
end