oas_contrib 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b2dd9f63fca90af7dfadb2acb5bbcf3298bcbe8124f058228ef2e039702e519
4
- data.tar.gz: 4077394dfc935cf038f283147840a9bb95600774bec1f0ccb2c21254edc1500a
3
+ metadata.gz: 87c1e2320567fdd04877c5764842ce980a18ff540aa809129133383ff2096373
4
+ data.tar.gz: 1f9d67c5afde0abc2895834e855c1f869511ea99a0d54d43688671dc2c8bc335
5
5
  SHA512:
6
- metadata.gz: 63164dd1c20142588d8453c87c4a9043107ae3ba7f9a6d9072dd4639b5d6886a4333d7de0acc9a39b85cc20870c27db9df623aebf318aee8d95464afd7c04b67
7
- data.tar.gz: 396ae10edea63c1a1198ae20054d631e2c4ade37d2e073965f6d6714014755a6bfb3e6c6ee57114b214aa1782cbdc83ca6a91a26382dee9751d7bcbb804d7823
6
+ metadata.gz: ba9d73d6cf2ea45aecba1a4fd31b36edcc6dd7a55dc4c77af2bff88afdf70a6a8686c0e683dc55790ef3f2a26d65acf2af04ef4e082bbb05ea34a81ab34b16ba
7
+ data.tar.gz: 8cd80655e4ebc7afe1208dde4957ced17ea8f5857a95819efb41f0653eb25c2c7ba523aac0bdb609198272a453aadfe128adf6ac4150b5cfa0dc279490d6f257
data/.rspec_status CHANGED
@@ -1,12 +1,12 @@
1
1
  example_id | status | run_time |
2
2
  ------------------------------------ | ------ | --------------- |
3
- ./spec/divide_command_spec.rb[1:1:1] | passed | 0.31599 seconds |
4
- ./spec/divide_command_spec.rb[1:2:1] | passed | 0.31835 seconds |
5
- ./spec/divide_command_spec.rb[1:3:1] | passed | 0.32395 seconds |
6
- ./spec/divide_command_spec.rb[1:4:1] | passed | 0.33436 seconds |
7
- ./spec/divide_command_spec.rb[1:5:1] | passed | 0.30711 seconds |
8
- ./spec/merge_command_spec.rb[1:1:1] | passed | 0.32602 seconds |
9
- ./spec/merge_command_spec.rb[1:2:1] | passed | 0.31555 seconds |
10
- ./spec/merge_command_spec.rb[1:3:1] | passed | 0.30952 seconds |
11
- ./spec/merge_command_spec.rb[1:4:1] | passed | 0.31631 seconds |
12
- ./spec/merge_command_spec.rb[1:5:1] | passed | 0.31812 seconds |
3
+ ./spec/divide_command_spec.rb[1:1:1] | passed | 0.36528 seconds |
4
+ ./spec/divide_command_spec.rb[1:2:1] | passed | 0.46455 seconds |
5
+ ./spec/divide_command_spec.rb[1:3:1] | passed | 0.34273 seconds |
6
+ ./spec/divide_command_spec.rb[1:4:1] | passed | 0.34539 seconds |
7
+ ./spec/divide_command_spec.rb[1:5:1] | passed | 0.32332 seconds |
8
+ ./spec/merge_command_spec.rb[1:1:1] | passed | 0.33312 seconds |
9
+ ./spec/merge_command_spec.rb[1:2:1] | passed | 0.31424 seconds |
10
+ ./spec/merge_command_spec.rb[1:3:1] | passed | 0.31142 seconds |
11
+ ./spec/merge_command_spec.rb[1:4:1] | passed | 0.31345 seconds |
12
+ ./spec/merge_command_spec.rb[1:5:1] | passed | 0.31093 seconds |
@@ -6,33 +6,40 @@ require 'oas_contrib/resolver/preview'
6
6
  module OasContrib
7
7
  # Commands
8
8
  class Command < Thor
9
- option :out_type, type: :string, aliases: '-ot', default: 'yaml', desc: 'output file type (yaml or json)'
9
+ option :out_type, type: :string, default: 'yaml', desc: 'output file type (yaml or json)'
10
10
  desc 'divide <input_file> <output_dir>', 'Divide the OAS file into path units and schema units.'
11
11
  # Command (Divide the OAS file into path units and schema units)
12
- # @param [String] in_file input file path
13
- # @param [String] out_dir output directory path
12
+ # @param [String] infile input file path
13
+ # @param [String] outdir output directory path
14
14
  # @return [Integer]
15
- def divide(in_file, out_dir)
16
- Resolver::Divide.new(in_file, out_dir, options['out_type']).run
15
+ def divide(infile, outdir)
16
+ OasContrib::Resolver::Divide.new(infile, outdir, options).run
17
17
  end
18
18
 
19
- option :in_type, type: :string, aliases: '-it', default: 'yaml', desc: 'input file type (yaml or json)'
19
+ option :in_type, type: :string, default: 'yaml', desc: 'input file type (yaml or json)'
20
20
  desc 'merge <input_dir> <output_file>', 'Merge multiple divided files into an OAS file.'
21
21
  # Command (Merge multiple divided files into an OAS file)
22
- # @param [String] in_dir input directory path
23
- # @param [String] out_file output file path
22
+ # @param [String] indir input directory path
23
+ # @param [String] outfile output file path
24
24
  # @return [Integer]
25
- def merge(in_dir, out_file)
26
- Resolver::Merge.new(in_dir, out_file, options['in_type']).run
25
+ def merge(indir, outfile)
26
+ OasContrib::Resolver::Merge.new(indir, outfile, options).run
27
27
  end
28
28
 
29
- option :port, type: :string, aliases: '-p', default: '50010', desc: 'Swagger UI listen port'
29
+ option :port, type: :string,default: '50010', desc: 'Swagger UI listen port'
30
30
  desc 'preview <input_file>', 'Preview OAS file using Swagger-UI official Docker image.'
31
31
  # Preview OAS file using Swagger-UI official Docker image.
32
- # @param [String] in_file input file path
32
+ # @param [String] infile input file path
33
33
  # @return [Integer]
34
- def preview(in_file)
35
- Resolver::Preview.new(in_file, options['port']).run
34
+ def preview(infile)
35
+ OasContrib::Resolver::Preview.new(infile, options).run
36
36
  end
37
+
38
+ # Preview OAS file using Swagger-UI official Docker image.
39
+ # @param [String] infile input file path
40
+ # @return [Integer]
41
+ # def preview(infile)
42
+ # OasContrib::Resolver::Generate.new(infile).run
43
+ # end
37
44
  end
38
45
  end
@@ -0,0 +1,41 @@
1
+ module OasContrib
2
+ module Interface
3
+ # Resolver Interface
4
+ module Resolver
5
+ # Run
6
+ # @raise [NotImplementedError]
7
+ # @return [nil]
8
+ def run
9
+ raise NotImplementedError, 'You must be implement "run" method.'
10
+ end
11
+
12
+ # Run
13
+ # @raise [NotImplementedError]
14
+ # @return [nil]
15
+ def setup
16
+ raise NotImplementedError, 'You must be implement "setup" method.'
17
+ end
18
+
19
+ # Load
20
+ # @raise [NotImplementedError]
21
+ # @return [nil]
22
+ def load
23
+ raise NotImplementedError, 'You must be implement "load" method.'
24
+ end
25
+
26
+ # Distribute
27
+ # @raise [NotImplementedError]
28
+ # @return [nil]
29
+ def dist
30
+ raise NotImplementedError, 'You must be implement"dist" method.'
31
+ end
32
+
33
+ # Resolve
34
+ # @raise [NotImplementedError]
35
+ # @return [nil]
36
+ def resolve
37
+ raise NotImplementedError, 'You must be implement "resolve" method.'
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module OasContrib
2
+ module Interface
3
+ # Spec Interface
4
+ module Spec
5
+ # Run
6
+ # @raise [NotImplementedError]
7
+ # @return [nil]
8
+ def mapping
9
+ raise NotImplementedError, 'You must be implement "mangppi" method.'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'oas_contrib/interface/spec'
2
+
3
+ module OasContrib
4
+ # OpenAPI module
5
+ module OpenAPI
6
+ # Spec Base
7
+ class Base
8
+ # @!attribute data
9
+ # @return [<Type>] <description>
10
+ attr_reader :data
11
+
12
+ include OasContrib::Interface::Spec
13
+
14
+ # Initialize
15
+ # @param [Hash] data mapping data
16
+ def initialize(data)
17
+ @data = data
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,18 +1,35 @@
1
+ require 'oas_contrib/openapi/base'
2
+
1
3
  module OasContrib
2
4
  # OpenAPI module
3
5
  module OpenAPI
4
6
  # Version 2 module
5
7
  module V2
6
8
  # Spec
7
- class Spec
8
- attr_accessor :data, :meta, :path, :model
9
+ class Spec < OasContrib::OpenAPI::Base
10
+ # @!attribute [r] meta
11
+ # @return [<Type>] <description>
12
+ attr_reader :meta
13
+ # @!attribute [r] path
14
+ # @return [<Type>] <description>
15
+ attr_reader :path
16
+ # @!attribute [r] model
17
+ # @return [<Type>] <description>
18
+ attr_reader :model
19
+
9
20
  # Initialize
10
21
  # @param [Hash] data mapping data
11
22
  def initialize(data)
12
- @data = data
23
+ super
24
+ end
25
+
26
+ # Mapping
27
+ # @return [OpenAPI::V2::Spec]
28
+ def mapping
13
29
  @meta = data.select { |v| v != 'paths' && v != 'definitions' } || nil
14
30
  @path = data['paths'] || nil
15
31
  @model = data['definitions'] || nil
32
+ self
16
33
  end
17
34
  end
18
35
  end
@@ -1,19 +1,35 @@
1
+ require 'oas_contrib/openapi/base'
2
+
1
3
  module OasContrib
2
4
  # OpenAPI
3
5
  module OpenAPI
4
6
  # Version 3 module
5
7
  module V3
6
8
  # Spec class
7
- class Spec
8
- attr_accessor :data, :meta, :path, :model
9
+ class Spec < OasContrib::OpenAPI::Base
10
+ # @!attribute [r] meta
11
+ # @return [<Type>] <description>
12
+ attr_reader :meta
13
+ # @!attribute [r] path
14
+ # @return [<Type>] <description>
15
+ attr_reader :path
16
+ # @!attribute [r] model
17
+ # @return [<Type>] <description>
18
+ attr_reader :model
9
19
 
10
20
  # Initialize
11
21
  # @param [Hash] data mapping data
12
22
  def initialize(data)
13
- @data = data
23
+ super
24
+ end
25
+
26
+ # Mapping
27
+ # @return [OpenAPI::V3::Spec]
28
+ def mapping
14
29
  @meta = data.select { |v| v != 'paths' && v != 'components' } || nil
15
30
  @path = data['paths'] || nil
16
31
  @model = data.dig('components', 'schemas') || nil
32
+ self
17
33
  end
18
34
  end
19
35
  end
@@ -1,3 +1,4 @@
1
+ require 'oas_contrib/interface/resolver'
1
2
  require 'oas_contrib/openapi/v2/spec'
2
3
  require 'oas_contrib/openapi/v3/spec'
3
4
  require 'yaml'
@@ -7,85 +8,43 @@ module OasContrib
7
8
  module Resolver
8
9
  # CommandResolver Base
9
10
  class Base
10
- # Initialze
11
- # @param [String] path input or output directory path
12
- def initialize(path)
13
- @meta_dir = path + '/meta'
14
- @path_dir = path + '/path'
15
- @model_dir = path + '/model'
16
- end
17
-
18
- # Run
19
- # @raise [NotImplementedError]
20
- # @return [nil]
21
- def run
22
- raise NotImplementedError, 'This class must be implemented "run" method.'
23
- end
11
+ # @!attribute [r] data
12
+ # @return [<Type>] <description>
13
+ attr_reader :data
24
14
 
25
- # Load
26
- # @raise [NotImplementedError]
27
- # @return [nil]
28
- def load
29
- raise NotImplementedError, 'This class must be implemented "load" method.'
30
- end
31
-
32
- # Distribute
33
- # @raise [NotImplementedError]
34
- # @return [nil]
35
- def dist
36
- raise NotImplementedError, 'This class must be implemented "dist" method.'
37
- end
15
+ # @!attribute [r] spec
16
+ # @return [<Type>] <description>
17
+ attr_reader :spec
38
18
 
39
- # Determine which type of definition data.
40
- # @raise [StandardError]
41
- # @return [OasContrib::Swagger::V2::Spec|OasContrib::OpenAPI::V3::Spec] spec
42
- def resolve
43
- return @spec = OasContrib::OpenAPI::V2::Spec.new(@load_data) if has_v2_meta?
44
- return @spec = OasContrib::OpenAPI::V3::Spec.new(@load_data) if has_v3_meta?
45
- raise 'Undefined OAS file.'
46
- end
19
+ include OasContrib::Interface::Resolver
47
20
 
48
21
  # <Description>
49
22
  # @return [<Type>] <description>
50
- def has_v3_meta?
51
- @load_data['openapi'] =~ /^3/
23
+ def v3?
24
+ @data['openapi'] =~ /^3/
52
25
  end
53
26
 
54
27
  # <Description>
55
28
  # @return [<Type>] <description>
56
- def has_v2_meta?
57
- @load_data['swagger'] =~ /^2/
58
- end
59
-
60
- # Check the type of OpenAPI v3 definition or not
61
- # @return [Boolean]
62
- def v3_spec?
63
- @spec.is_a?(OasContrib::OpenAPI::V3::Spec)
29
+ def v2?
30
+ @data['swagger'] =~ /^2/
64
31
  end
65
32
 
66
- # Check the type of OpenAPI v2 definition or not
67
- # @return [Boolean]
68
- def v2_spec?
69
- @spec.is_a?(OasContrib::OpenAPI::V2::Spec)
70
- end
71
-
72
- # Convert file type string to file extention string.
73
- # @param [String] type file type string (yaml or json)
74
- # @raise [ArgumentError] invalid file type string
75
- # @return [String] file extension string (.yml or .json)
76
- def file_type_to_ext(type)
77
- case type
78
- when 'yaml' then '.yml'
79
- when 'json' then '.json'
80
- else raise ArgumentError, 'Undefined file type'
81
- end
33
+ # Resolver spec object
34
+ # @raise [StandardError]
35
+ # @return [OasContrib::Swagger::V2::Spec|OasContrib::OpenAPI::V3::Spec] spec
36
+ def resolve
37
+ @spec = OasContrib::OpenAPI::V2::Spec.new(@data) if v2?
38
+ @spec = OasContrib::OpenAPI::V3::Spec.new(@data) if v3?
39
+ raise 'Undefined OAS file.' unless @spec
40
+ @spec.mapping
82
41
  end
83
42
 
84
43
  # Load a file
85
44
  # @param [String] path input file path
86
45
  # @return [Hash]
87
46
  def input(path)
88
- @load_data = input_call(path)
47
+ @data = _input(path)
89
48
  end
90
49
 
91
50
  # Output a file
@@ -93,68 +52,48 @@ module OasContrib
93
52
  # @param [String] path output file path
94
53
  # @return [File]
95
54
  def output(hash, path)
96
- File.open(path, 'w') { |f| output_call(hash, f) }
97
- end
98
-
99
- # Load directory files
100
- # @param [String] path input directory
101
- # @return [Hash] merged input files data
102
- def input_dir(path)
103
- Dir.glob(path).sort.each_with_object({}, &input_call_lambda)
55
+ File.open(path, 'w') { |f| _output(hash, f) }
104
56
  end
105
57
 
106
- # Proc of input a yaml or json file
107
- # @return [Proc]
108
- def input_call_lambda
109
- lambda do |file, result|
110
- hash = input_call(file)
111
- key = hash.keys[0]
112
- result[key] = hash[key]
58
+ # Convert file type string to file extention string.
59
+ # @param [String] type file type string (yaml or json)
60
+ # @raise [ArgumentError] invalid file type string
61
+ # @return [String] file extension string (.yml or .json)
62
+ def str2ext(type)
63
+ case type
64
+ when 'yaml' then '.yml'
65
+ when 'json' then '.json'
66
+ else raise ArgumentError, 'Undefined file type'
113
67
  end
114
68
  end
115
69
 
70
+ private
71
+
116
72
  # Load a file depending on file extension
117
73
  # @param [String] path file path
118
74
  # @raise [ArgumentError] invalid file type string
119
75
  # @return [Hash]
120
- def input_call(path)
76
+ def _input(path)
121
77
  puts "Load: #{path}"
122
- case @input_file_ext
78
+ case @infile_ext
123
79
  when '.yml' then YAML.load_file(path)
124
80
  when '.json' then JSON.parse(File.read(path))
125
- else raise ArgumentError, 'Undefined file type'
81
+ else raise ArgumentError, 'Undefined input file type'
126
82
  end
127
83
  end
128
84
 
129
- # Output a file depending on file extension
130
- # @param [String] path file path
85
+ # <Description>
86
+ # @param [Hash] hash data
87
+ # @param [File] file output file object
131
88
  # @raise [ArgumentError] invalid file type string
132
89
  # @return [IO]
133
- def output_call(hash, file)
90
+ def _output(hash, file)
134
91
  puts "Dist: #{file.path}"
135
- case @output_file_ext
92
+ case @outfile_ext
136
93
  when '.yml' then YAML.dump(hash, file)
137
94
  when '.json' then JSON.dump(hash, file)
138
- else raise ArgumentError, 'Undefined file type'
139
- end
140
- end
141
-
142
- # Output directory and files
143
- # @param [Hash] hash data
144
- # @param [String] path directory path
145
- # @return [nil]
146
- def output_dir(hash, path)
147
- puts "Dist: #{path}"
148
- FileUtils.mkdir_p(path)
149
-
150
- i = 1
151
- hash.each do |k, _v|
152
- key = k.tr('/', '_').gsub(/^_/, '')
153
- val = hash.select { |hash_key, _| hash_key == k }
154
- output(val, "#{path}/#{i.to_s.rjust(3, '0')}_#{key}#{@output_file_ext}")
155
- i += 1
95
+ else raise ArgumentError, 'Undefined output file type'
156
96
  end
157
- nil
158
97
  end
159
98
  end
160
99
  end
@@ -4,30 +4,39 @@ module OasContrib
4
4
  # Command Resolvers
5
5
  module Resolver
6
6
  # Divide command resolver
7
- class Divide < Resolver::Base
7
+ class Divide < OasContrib::Resolver::Base
8
8
  # Initialize
9
- # @param [String] input_file_path input file path
10
- # @param [String] output_dir_path output directory path
11
- # @param [String] output_file_type output file type (json or yaml)
12
- def initialize(input_file_path, output_dir_path, output_file_type)
13
- @input_file_path = input_file_path
14
- @input_file_ext = File.extname(input_file_path)
15
- @output_file_ext = file_type_to_ext(output_file_type)
16
- super(output_dir_path)
9
+ # @param [String] infile input file path
10
+ # @param [String] outdir output directory path
11
+ # @param [Array] options options
12
+ def initialize(infile, outdir, options)
13
+ @meta_dir = outdir + '/meta'
14
+ @path_dir = outdir + '/path'
15
+ @model_dir = outdir + '/model'
16
+ @infile = infile
17
+ @outfile_type = options['out_type']
17
18
  end
18
19
 
19
20
  # Run
20
21
  # @return [nil]
21
22
  def run
23
+ setup
22
24
  load
23
25
  resolve
24
26
  dist
25
27
  end
26
28
 
29
+ # <Description>
30
+ # @return [<Type>] <description>
31
+ def setup
32
+ @infile_ext = File.extname(@infile)
33
+ @outfile_ext = str2ext(@outfile_type)
34
+ end
35
+
27
36
  # Load the OAS file
28
37
  # @return [Hash] loaded data
29
38
  def load
30
- input(@input_file_path)
39
+ input(@infile)
31
40
  end
32
41
 
33
42
  # Output divided files
@@ -37,6 +46,42 @@ module OasContrib
37
46
  output_dir(@spec.path, @path_dir)
38
47
  output_dir(@spec.model, @model_dir)
39
48
  end
49
+
50
+ private
51
+
52
+ # Output directory and files
53
+ # @param [Hash] hash data
54
+ # @param [String] path directory path
55
+ # @return [nil]
56
+ def output_dir(hash, path)
57
+ puts "Dist: #{path}"
58
+ FileUtils.mkdir_p(path)
59
+ hash.each.with_index(1) do |(k, _v), i|
60
+ outfile_path = _output_dir_file_path_modify(path, k, i)
61
+ outfile_data = _output_dir_file_data_filter(hash, k)
62
+ output(outfile_data, outfile_path)
63
+ end
64
+ nil
65
+ end
66
+
67
+ # <Description>
68
+ # @param [<Type>] dir <description>
69
+ # @param [<Type>] hash_key <description>
70
+ # @param [<Type>] num <description>
71
+ # @return [<Type>] <description>
72
+ def _output_dir_file_path_modify(dir, hash_key, num)
73
+ prefix = num.to_s.rjust(3, '0')
74
+ file_name = hash_key.tr('/', '_').gsub(/^_/, '')
75
+ dir + '/' + prefix + '_' + file_name + @outfile_ext
76
+ end
77
+
78
+ # <Description>
79
+ # @param [<Type>] hash <description>
80
+ # @param [<Type>] filter_key <description>
81
+ # @return [<Type>] <description>
82
+ def _output_dir_file_data_filter(hash, filter_key)
83
+ hash.select { |key, _| key == filter_key }
84
+ end
40
85
  end
41
86
  end
42
87
  end
@@ -4,26 +4,35 @@ module OasContrib
4
4
  # Command Resolvers
5
5
  module Resolver
6
6
  # Merge command resolver
7
- class Merge < Resolver::Base
7
+ class Merge < OasContrib::Resolver::Base
8
8
  # Initialize
9
- # @param [String] input_dir_path input directory path
10
- # @param [String] output_file_path output file path
11
- # @param [String] input_file_type input file type (json or yaml)
12
- def initialize(input_dir_path, output_file_path, input_file_type)
13
- @input_file_ext = file_type_to_ext(input_file_type)
14
- @output_file_path = output_file_path
15
- @output_file_ext = File.extname(output_file_path)
16
- super(input_dir_path)
9
+ # @param [String] indir input directory path
10
+ # @param [String] outfile output file path
11
+ # @param [Array] options options
12
+ def initialize(indir, outfile, options)
13
+ @meta_dir = indir + '/meta'
14
+ @path_dir = indir + '/path'
15
+ @model_dir = indir + '/model'
16
+ @outfile = outfile
17
+ @infile_type = options['in_type']
17
18
  end
18
19
 
19
20
  # Run
20
21
  # @return [nil]
21
22
  def run
23
+ setup
22
24
  load
23
25
  resolve
24
26
  dist
25
27
  end
26
28
 
29
+ # <Description>
30
+ # @return [<Type>] <description>
31
+ def setup
32
+ @infile_ext = str2ext(@infile_type)
33
+ @outfile_ext = File.extname(@outfile)
34
+ end
35
+
27
36
  # Load divided files
28
37
  # @return [Hash] load and merged data
29
38
  def load
@@ -36,7 +45,7 @@ module OasContrib
36
45
  # Output the OAS file
37
46
  # @return [nil]
38
47
  def dist
39
- output(@load_data, @output_file_path)
48
+ output(@data, @outfile)
40
49
  end
41
50
 
42
51
  private
@@ -44,24 +53,37 @@ module OasContrib
44
53
  # Load meta part files
45
54
  # @return [Hash] loaded data
46
55
  def load_meta
47
- @load_data = input_dir(@meta_dir + '/**/*' + @input_file_ext)
56
+ @data = input_dir(@meta_dir)
48
57
  end
49
58
 
50
59
  # Load path part files
51
60
  # @return [Hash] load and merged data
52
61
  def load_path
53
- @load_data['paths'] = input_dir(@path_dir + '/**/*' + @input_file_ext)
62
+ @data['paths'] = input_dir(@path_dir)
54
63
  end
55
64
 
56
65
  # Load model part files
57
66
  # @return [Hash] load and merged data
58
67
  def load_model
59
- path = @model_dir + '/**/*' + @input_file_ext
60
- if v3_spec?
61
- @load_data['components'] = {}
62
- @load_data['components']['schemas'] = input_dir(path)
63
- elsif v2_spec?
64
- @load_data['definitions'] = input_dir(path)
68
+ @data['definitions'] = input_dir(@model_dir) if v2?
69
+ @data['components'] = { 'schemas' => input_dir(@model_dir) } if v3?
70
+ end
71
+
72
+ # Load directory files
73
+ # @param [String] path input directory
74
+ # @return [Hash] merged input files data
75
+ def input_dir(dir)
76
+ path = dir + '/**/*' + @infile_ext
77
+ Dir.glob(path).sort.each_with_object({}, &input_lambda)
78
+ end
79
+
80
+ # Proc of input a yaml or json file
81
+ # @return [Proc]
82
+ def input_lambda
83
+ lambda do |file, result|
84
+ hash = _input(file)
85
+ key = hash.keys[0]
86
+ result[key] = hash[key]
65
87
  end
66
88
  end
67
89
  end
@@ -4,27 +4,32 @@ module OasContrib
4
4
  # Command Resolvers
5
5
  module Resolver
6
6
  # Preview command resolver
7
- class Preview < Resolver::Base
7
+ class Preview < OasContrib::Resolver::Base
8
8
  # Initialize
9
- # @param [String] input_file_path input file path
10
- # @param [String] input_file_type input file type (json or yaml)
11
- def initialize(input_file_path, port)
12
- @input_file_path = input_file_path
13
- @port = port
9
+ # @param [String] infile input file path
10
+ # @param [Integer] port listen port
11
+ def initialize(infile, options)
12
+ @infile = infile
13
+ @port = options['port']
14
14
  end
15
15
 
16
16
  # Run
17
17
  # @return [nil]
18
18
  def run
19
- load
19
+ setup
20
+ # load
21
+ # resolve
20
22
  dist
21
23
  end
22
24
 
25
+ def setup
26
+ @expand_path = File.expand_path(@infile)
27
+ @basename = File.basename(@expand_path)
28
+ end
29
+
23
30
  # Load
24
31
  # @return [nil]
25
32
  def load
26
- @expand_path = File.expand_path(@input_file_path)
27
- @basename = File.basename(@expand_path)
28
33
  nil
29
34
  end
30
35
 
@@ -1,4 +1,4 @@
1
1
  module OasContrib
2
2
  # @return [String] version
3
- VERSION = '0.2.1'.freeze
3
+ VERSION = '0.2.3'.freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michinao Shimizu
@@ -109,6 +109,9 @@ files:
109
109
  - exe/oas_contrib
110
110
  - lib/oas_contrib.rb
111
111
  - lib/oas_contrib/command.rb
112
+ - lib/oas_contrib/interface/resolver.rb
113
+ - lib/oas_contrib/interface/spec.rb
114
+ - lib/oas_contrib/openapi/base.rb
112
115
  - lib/oas_contrib/openapi/v2/spec.rb
113
116
  - lib/oas_contrib/openapi/v3/spec.rb
114
117
  - lib/oas_contrib/resolver/base.rb