Class: OasContrib::Resolver::Base
- Inherits:
-
Object
- Object
- OasContrib::Resolver::Base
- Includes:
- Interface::Resolver
- Defined in:
- lib/oas_contrib/resolver/base.rb
Overview
Basical command resolver class
Constant Summary collapse
- DEFINED_FILE_EXT =
Returns approval file extensions.
['.json', '.yml', '.yaml'].freeze
- DIR_NAME_META =
Returns the directory name of meta part files.
'meta'.freeze
- DIR_NAME_PATH =
Returns the directory name of path part files.
'path'.freeze
- DIR_NAME_MODEL =
Returns the directory name of model part files.
'model'.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
-
#file_ext_check ⇒ Boolean
Check the file extensions is approved or not.
-
#input(path) ⇒ Hash
Load and parse the input file.
-
#input_dir(dir) ⇒ Hash
Load and parse the files in target directory recursive.
-
#input_lambda ⇒ Proc
Load and parse the file proc.
-
#output(hash, path) ⇒ IO
Output a new file with mapped spec data hash.
-
#resolve ⇒ OpenAPI::V3::Spec|OpenAPI::V2::Spec
Judge and generate OpenAPI specification object.
-
#v2? ⇒ Boolean
Check the format of input file is OpenAPI v2 specificaion or not.
-
#v3? ⇒ Boolean
Check the format of input file is OpenAPI v3 specificaion or not.
Methods included from Interface::Resolver
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/oas_contrib/resolver/base.rb', line 13 def data @data end |
#spec ⇒ Object (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_check ⇒ Boolean
Check the file extensions is approved or not.
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.
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.
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_lambda ⇒ Proc
Load and parse the file 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.
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 |
#resolve ⇒ OpenAPI::V3::Spec|OpenAPI::V2::Spec
Judge and generate OpenAPI specification object.
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.
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.
48 49 50 |
# File 'lib/oas_contrib/resolver/base.rb', line 48 def v3? @data['openapi'] =~ /^3/ end |