Class: OasContrib::Resolver::Base

Inherits:
Object
  • Object
show all
Includes:
Interface::Resolver
Defined in:
lib/oas_contrib/resolver/base.rb

Overview

Basical command resolver class

Direct Known Subclasses

Divide, Merge, Preview

Constant Summary collapse

DEFINED_FILE_EXT =

Returns approval file extensions.

Returns:

  • (Array)

    approval file extensions

['.json', '.yml', '.yaml'].freeze
DIR_NAME_META =

Returns the directory name of meta part files.

Returns:

  • (String)

    the directory name of meta part files

'meta'.freeze
DIR_NAME_PATH =

Returns the directory name of path part files.

Returns:

  • (String)

    the directory name of path part files

'path'.freeze
DIR_NAME_MODEL =

Returns the directory name of model part files.

Returns:

  • (String)

    the directory name of model part files

'model'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interface::Resolver

#distribute, #load, #setup

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/oas_contrib/resolver/base.rb', line 13

def data
  @data
end

#specObject (readonly)

Returns the value of attribute spec.



17
18
19
# File 'lib/oas_contrib/resolver/base.rb', line 17

def spec
  @spec
end

Instance Method Details

#file_ext_checkBoolean

Check the file extensions is approved or not.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Hash

Load and parse the input file.

Parameters:

  • path (String)

    input file path

Returns:

  • (Hash)

    parsed input data hash



70
71
72
# File 'lib/oas_contrib/resolver/base.rb', line 70

def input(path)
  @data = _input(path)
end

#input_dir(dir) ⇒ Hash

Load and parse the files in target directory recursive.

Parameters:

  • dir (String)

    input directory path

Returns:

  • (Hash)

    parsed input data hash



85
86
87
88
# 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_lambdaProc

Load and parse the file proc.

Returns:

  • (Proc)


92
93
94
95
96
97
98
# 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) ⇒ IO

Output a new file with mapped spec data hash.

Parameters:

  • hash (Hash)

    mapped spec data hash

  • path (String)

    output file path

Returns:

  • (IO)


78
79
80
# File 'lib/oas_contrib/resolver/base.rb', line 78

def output(hash, path)
  File.open(path, 'w') { |f| _output(hash, f) }
end

#resolveOpenAPI::V3::Spec|OpenAPI::V2::Spec

Judge and generate OpenAPI specification object.

Returns:



60
61
62
63
64
65
# 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?Boolean

Check the format of input file is OpenAPI v2 specificaion or not.

Returns:

  • (Boolean)


54
55
56
# File 'lib/oas_contrib/resolver/base.rb', line 54

def v2?
  @data['swagger'] =~ /^2/
end

#v3?Boolean

Check the format of input file is OpenAPI v3 specificaion or not.

Returns:

  • (Boolean)


48
49
50
# File 'lib/oas_contrib/resolver/base.rb', line 48

def v3?
  @data['openapi'] =~ /^3/
end