oas_contrib 0.2.3 → 0.2.4

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: 87c1e2320567fdd04877c5764842ce980a18ff540aa809129133383ff2096373
4
- data.tar.gz: 1f9d67c5afde0abc2895834e855c1f869511ea99a0d54d43688671dc2c8bc335
3
+ metadata.gz: 0b1a48c2b71fbc69225a8d565450bd2478ce8e5f2710196a06c05a3d60c93e68
4
+ data.tar.gz: 4bb4aea54a90ebdd8edc03252da09122625e411c17a31076fdd93c8457df76ba
5
5
  SHA512:
6
- metadata.gz: ba9d73d6cf2ea45aecba1a4fd31b36edcc6dd7a55dc4c77af2bff88afdf70a6a8686c0e683dc55790ef3f2a26d65acf2af04ef4e082bbb05ea34a81ab34b16ba
7
- data.tar.gz: 8cd80655e4ebc7afe1208dde4957ced17ea8f5857a95819efb41f0653eb25c2c7ba523aac0bdb609198272a453aadfe128adf6ac4150b5cfa0dc279490d6f257
6
+ metadata.gz: 723585fa66b02f6ff7201026234ea318f9f26e798a2ddf6fdc232c6b32505a7a3d9a6653c07820cd12f32fb38f74009727a34a8425073b9692e9ba14ed13d03a
7
+ data.tar.gz: a7d852a298beafaf2fbefec3c84278610d680f438ca3d21d9a02d36fd005e73e707709e165593fea66920696be8ccbbd5132512edaa4a1a3ec4313adf2bc561b
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.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 |
3
+ ./spec/divide_command_spec.rb[1:1:1] | passed | 0.34731 seconds |
4
+ ./spec/divide_command_spec.rb[1:2:1] | passed | 0.35834 seconds |
5
+ ./spec/divide_command_spec.rb[1:3:1] | passed | 0.50581 seconds |
6
+ ./spec/divide_command_spec.rb[1:4:1] | passed | 0.41085 seconds |
7
+ ./spec/divide_command_spec.rb[1:5:1] | passed | 0.52116 seconds |
8
+ ./spec/merge_command_spec.rb[1:1:1] | passed | 0.80512 seconds |
9
+ ./spec/merge_command_spec.rb[1:2:1] | passed | 0.54957 seconds |
10
+ ./spec/merge_command_spec.rb[1:3:1] | passed | 0.45048 seconds |
11
+ ./spec/merge_command_spec.rb[1:4:1] | passed | 0.53336 seconds |
12
+ ./spec/merge_command_spec.rb[1:5:1] | passed | 0.60615 seconds |
@@ -4,42 +4,41 @@ require 'oas_contrib/resolver/merge'
4
4
  require 'oas_contrib/resolver/preview'
5
5
 
6
6
  module OasContrib
7
- # Commands
8
7
  class Command < Thor
8
+ include Thor::Actions
9
+
9
10
  option :out_type, type: :string, default: 'yaml', desc: 'output file type (yaml or json)'
10
- desc 'divide <input_file> <output_dir>', 'Divide the OAS file into path units and schema units.'
11
- # Command (Divide the OAS file into path units and schema units)
12
- # @param [String] infile input file path
13
- # @param [String] outdir output directory path
14
- # @return [Integer]
15
- def divide(infile, outdir)
16
- OasContrib::Resolver::Divide.new(infile, outdir, options).run
11
+ desc 'divide <spec_file> <output_dir> (<options>)', 'Divide the spec_file into path units and schema units.'
12
+
13
+ def divide(spec_file, outdir)
14
+ resolver = OasContrib::Resolver::Divide.new(spec_file, outdir, options)
15
+ resolver.setup
16
+ resolver.load
17
+ resolver.resolve
18
+ resolver.distribute
19
+ say 'complete!', :green
17
20
  end
18
21
 
19
22
  option :in_type, type: :string, default: 'yaml', desc: 'input file type (yaml or json)'
20
- desc 'merge <input_dir> <output_file>', 'Merge multiple divided files into an OAS file.'
21
- # Command (Merge multiple divided files into an OAS file)
22
- # @param [String] indir input directory path
23
- # @param [String] outfile output file path
24
- # @return [Integer]
25
- def merge(indir, outfile)
26
- OasContrib::Resolver::Merge.new(indir, outfile, options).run
27
- end
23
+ desc 'merge <input_dir> <spec_file> (<options>)', 'Merge multiple divided files into an spec_file.'
28
24
 
29
- option :port, type: :string,default: '50010', desc: 'Swagger UI listen port'
30
- desc 'preview <input_file>', 'Preview OAS file using Swagger-UI official Docker image.'
31
- # Preview OAS file using Swagger-UI official Docker image.
32
- # @param [String] infile input file path
33
- # @return [Integer]
34
- def preview(infile)
35
- OasContrib::Resolver::Preview.new(infile, options).run
25
+ def merge(indir, spec_file)
26
+ resolver = OasContrib::Resolver::Merge.new(indir, spec_file, options)
27
+ resolver.setup
28
+ resolver.load
29
+ resolver.resolve
30
+ resolver.distribute
31
+ say 'complete!', :green
36
32
  end
37
33
 
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
34
+ option :port, type: :numeric, default: 50010, desc: 'Swagger UI listen port'
35
+ desc 'preview <spec_file> (<options>)', 'Preview the spec_file using Swagger-UI official Docker image.'
36
+
37
+ def preview(spec_file)
38
+ resolver = OasContrib::Resolver::Preview.new(spec_file, options)
39
+ resolver.setup
40
+ resolver.distribute
41
+ say 'complete!', :green
42
+ end
44
43
  end
45
44
  end
@@ -1,41 +1,21 @@
1
1
  module OasContrib
2
2
  module Interface
3
- # Resolver Interface
4
3
  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
4
  def setup
16
5
  raise NotImplementedError, 'You must be implement "setup" method.'
17
6
  end
18
7
 
19
- # Load
20
- # @raise [NotImplementedError]
21
- # @return [nil]
22
8
  def load
23
9
  raise NotImplementedError, 'You must be implement "load" method.'
24
10
  end
25
11
 
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
12
  def resolve
37
13
  raise NotImplementedError, 'You must be implement "resolve" method.'
38
14
  end
15
+
16
+ def distribute
17
+ raise NotImplementedError, 'You must be implement"dist" method.'
18
+ end
39
19
  end
40
20
  end
41
21
  end
@@ -1,10 +1,6 @@
1
1
  module OasContrib
2
2
  module Interface
3
- # Spec Interface
4
3
  module Spec
5
- # Run
6
- # @raise [NotImplementedError]
7
- # @return [nil]
8
4
  def mapping
9
5
  raise NotImplementedError, 'You must be implement "mangppi" method.'
10
6
  end
@@ -1,18 +1,12 @@
1
1
  require 'oas_contrib/interface/spec'
2
2
 
3
3
  module OasContrib
4
- # OpenAPI module
5
4
  module OpenAPI
6
- # Spec Base
7
5
  class Base
8
- # @!attribute data
9
- # @return [<Type>] <description>
10
6
  attr_reader :data
11
7
 
12
8
  include OasContrib::Interface::Spec
13
9
 
14
- # Initialize
15
- # @param [Hash] data mapping data
16
10
  def initialize(data)
17
11
  @data = data
18
12
  end
@@ -1,30 +1,15 @@
1
1
  require 'oas_contrib/openapi/base'
2
2
 
3
3
  module OasContrib
4
- # OpenAPI module
5
4
  module OpenAPI
6
- # Version 2 module
7
5
  module V2
8
- # Spec
9
6
  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
7
+ attr_reader :meta, :path, :model
19
8
 
20
- # Initialize
21
- # @param [Hash] data mapping data
22
9
  def initialize(data)
23
10
  super
24
11
  end
25
12
 
26
- # Mapping
27
- # @return [OpenAPI::V2::Spec]
28
13
  def mapping
29
14
  @meta = data.select { |v| v != 'paths' && v != 'definitions' } || nil
30
15
  @path = data['paths'] || nil
@@ -1,30 +1,15 @@
1
1
  require 'oas_contrib/openapi/base'
2
2
 
3
3
  module OasContrib
4
- # OpenAPI
5
4
  module OpenAPI
6
- # Version 3 module
7
5
  module V3
8
- # Spec class
9
6
  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
7
+ attr_reader :meta ,:path, :model
19
8
 
20
- # Initialize
21
- # @param [Hash] data mapping data
22
9
  def initialize(data)
23
10
  super
24
11
  end
25
12
 
26
- # Mapping
27
- # @return [OpenAPI::V3::Spec]
28
13
  def mapping
29
14
  @meta = data.select { |v| v != 'paths' && v != 'components' } || nil
30
15
  @path = data['paths'] || nil
@@ -6,33 +6,19 @@ require 'json'
6
6
 
7
7
  module OasContrib
8
8
  module Resolver
9
- # CommandResolver Base
10
9
  class Base
11
- # @!attribute [r] data
12
- # @return [<Type>] <description>
13
- attr_reader :data
14
-
15
- # @!attribute [r] spec
16
- # @return [<Type>] <description>
17
- attr_reader :spec
10
+ attr_reader :data, :spec
18
11
 
19
12
  include OasContrib::Interface::Resolver
20
13
 
21
- # <Description>
22
- # @return [<Type>] <description>
23
14
  def v3?
24
15
  @data['openapi'] =~ /^3/
25
16
  end
26
17
 
27
- # <Description>
28
- # @return [<Type>] <description>
29
18
  def v2?
30
19
  @data['swagger'] =~ /^2/
31
20
  end
32
21
 
33
- # Resolver spec object
34
- # @raise [StandardError]
35
- # @return [OasContrib::Swagger::V2::Spec|OasContrib::OpenAPI::V3::Spec] spec
36
22
  def resolve
37
23
  @spec = OasContrib::OpenAPI::V2::Spec.new(@data) if v2?
38
24
  @spec = OasContrib::OpenAPI::V3::Spec.new(@data) if v3?
@@ -40,25 +26,14 @@ module OasContrib
40
26
  @spec.mapping
41
27
  end
42
28
 
43
- # Load a file
44
- # @param [String] path input file path
45
- # @return [Hash]
46
29
  def input(path)
47
30
  @data = _input(path)
48
31
  end
49
32
 
50
- # Output a file
51
- # @param [Hash] hash data
52
- # @param [String] path output file path
53
- # @return [File]
54
33
  def output(hash, path)
55
34
  File.open(path, 'w') { |f| _output(hash, f) }
56
35
  end
57
36
 
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
37
  def str2ext(type)
63
38
  case type
64
39
  when 'yaml' then '.yml'
@@ -67,12 +42,21 @@ module OasContrib
67
42
  end
68
43
  end
69
44
 
45
+ def input_dir(dir)
46
+ path = dir + '/**/*' + @infile_ext
47
+ Dir.glob(path).sort.each_with_object({}, &input_lambda)
48
+ end
49
+
50
+ def input_lambda
51
+ lambda do |file, result|
52
+ hash = _input(file)
53
+ key = hash.keys[0]
54
+ result[key] = hash[key]
55
+ end
56
+ end
57
+
70
58
  private
71
59
 
72
- # Load a file depending on file extension
73
- # @param [String] path file path
74
- # @raise [ArgumentError] invalid file type string
75
- # @return [Hash]
76
60
  def _input(path)
77
61
  puts "Load: #{path}"
78
62
  case @infile_ext
@@ -82,11 +66,6 @@ module OasContrib
82
66
  end
83
67
  end
84
68
 
85
- # <Description>
86
- # @param [Hash] hash data
87
- # @param [File] file output file object
88
- # @raise [ArgumentError] invalid file type string
89
- # @return [IO]
90
69
  def _output(hash, file)
91
70
  puts "Dist: #{file.path}"
92
71
  case @outfile_ext
@@ -1,14 +1,8 @@
1
1
  require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
- # Command Resolvers
5
4
  module Resolver
6
- # Divide command resolver
7
5
  class Divide < OasContrib::Resolver::Base
8
- # Initialize
9
- # @param [String] infile input file path
10
- # @param [String] outdir output directory path
11
- # @param [Array] options options
12
6
  def initialize(infile, outdir, options)
13
7
  @meta_dir = outdir + '/meta'
14
8
  @path_dir = outdir + '/path'
@@ -17,31 +11,16 @@ module OasContrib
17
11
  @outfile_type = options['out_type']
18
12
  end
19
13
 
20
- # Run
21
- # @return [nil]
22
- def run
23
- setup
24
- load
25
- resolve
26
- dist
27
- end
28
-
29
- # <Description>
30
- # @return [<Type>] <description>
31
14
  def setup
32
15
  @infile_ext = File.extname(@infile)
33
16
  @outfile_ext = str2ext(@outfile_type)
34
17
  end
35
18
 
36
- # Load the OAS file
37
- # @return [Hash] loaded data
38
19
  def load
39
20
  input(@infile)
40
21
  end
41
22
 
42
- # Output divided files
43
- # @return [nil]
44
- def dist
23
+ def distribute
45
24
  output_dir(@spec.meta, @meta_dir)
46
25
  output_dir(@spec.path, @path_dir)
47
26
  output_dir(@spec.model, @model_dir)
@@ -49,10 +28,6 @@ module OasContrib
49
28
 
50
29
  private
51
30
 
52
- # Output directory and files
53
- # @param [Hash] hash data
54
- # @param [String] path directory path
55
- # @return [nil]
56
31
  def output_dir(hash, path)
57
32
  puts "Dist: #{path}"
58
33
  FileUtils.mkdir_p(path)
@@ -64,21 +39,12 @@ module OasContrib
64
39
  nil
65
40
  end
66
41
 
67
- # <Description>
68
- # @param [<Type>] dir <description>
69
- # @param [<Type>] hash_key <description>
70
- # @param [<Type>] num <description>
71
- # @return [<Type>] <description>
72
42
  def _output_dir_file_path_modify(dir, hash_key, num)
73
43
  prefix = num.to_s.rjust(3, '0')
74
44
  file_name = hash_key.tr('/', '_').gsub(/^_/, '')
75
45
  dir + '/' + prefix + '_' + file_name + @outfile_ext
76
46
  end
77
47
 
78
- # <Description>
79
- # @param [<Type>] hash <description>
80
- # @param [<Type>] filter_key <description>
81
- # @return [<Type>] <description>
82
48
  def _output_dir_file_data_filter(hash, filter_key)
83
49
  hash.select { |key, _| key == filter_key }
84
50
  end
@@ -1,14 +1,8 @@
1
1
  require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
- # Command Resolvers
5
4
  module Resolver
6
- # Merge command resolver
7
5
  class Merge < OasContrib::Resolver::Base
8
- # Initialize
9
- # @param [String] indir input directory path
10
- # @param [String] outfile output file path
11
- # @param [Array] options options
12
6
  def initialize(indir, outfile, options)
13
7
  @meta_dir = indir + '/meta'
14
8
  @path_dir = indir + '/path'
@@ -17,74 +11,21 @@ module OasContrib
17
11
  @infile_type = options['in_type']
18
12
  end
19
13
 
20
- # Run
21
- # @return [nil]
22
- def run
23
- setup
24
- load
25
- resolve
26
- dist
27
- end
28
-
29
- # <Description>
30
- # @return [<Type>] <description>
31
14
  def setup
32
15
  @infile_ext = str2ext(@infile_type)
33
16
  @outfile_ext = File.extname(@outfile)
34
17
  end
35
18
 
36
- # Load divided files
37
- # @return [Hash] load and merged data
38
19
  def load
39
- load_meta
40
- resolve
41
- load_path
42
- load_model
43
- end
44
-
45
- # Output the OAS file
46
- # @return [nil]
47
- def dist
48
- output(@data, @outfile)
49
- end
50
-
51
- private
52
-
53
- # Load meta part files
54
- # @return [Hash] loaded data
55
- def load_meta
56
20
  @data = input_dir(@meta_dir)
57
- end
58
-
59
- # Load path part files
60
- # @return [Hash] load and merged data
61
- def load_path
21
+ resolve
62
22
  @data['paths'] = input_dir(@path_dir)
63
- end
64
-
65
- # Load model part files
66
- # @return [Hash] load and merged data
67
- def load_model
68
23
  @data['definitions'] = input_dir(@model_dir) if v2?
69
24
  @data['components'] = { 'schemas' => input_dir(@model_dir) } if v3?
70
25
  end
71
26
 
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]
87
- end
27
+ def distribute
28
+ output(@data, @outfile)
88
29
  end
89
30
  end
90
31
  end
@@ -1,41 +1,19 @@
1
1
  require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
- # Command Resolvers
5
4
  module Resolver
6
- # Preview command resolver
7
5
  class Preview < OasContrib::Resolver::Base
8
- # Initialize
9
- # @param [String] infile input file path
10
- # @param [Integer] port listen port
11
6
  def initialize(infile, options)
12
7
  @infile = infile
13
- @port = options['port']
14
- end
15
-
16
- # Run
17
- # @return [nil]
18
- def run
19
- setup
20
- # load
21
- # resolve
22
- dist
8
+ @port = options['port']
23
9
  end
24
10
 
25
11
  def setup
26
12
  @expand_path = File.expand_path(@infile)
27
- @basename = File.basename(@expand_path)
28
- end
29
-
30
- # Load
31
- # @return [nil]
32
- def load
33
- nil
13
+ @basename = File.basename(@expand_path)
34
14
  end
35
15
 
36
- # Distribute
37
- # @return [nil]
38
- def dist
16
+ def distribute
39
17
  puts "SwaggerUI listen: http://localhost:#{@port} with: #{@expand_path}"
40
18
  `docker run --rm --name oas_contrib_preview_swagger_ui \
41
19
  -p #{@port}:8080 -e API_URL=#{@basename} \
@@ -1,4 +1,3 @@
1
1
  module OasContrib
2
- # @return [String] version
3
- VERSION = '0.2.3'.freeze
2
+ VERSION = '0.2.4'.freeze
4
3
  end
data/lib/oas_contrib.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  require 'oas_contrib/version'
2
2
 
3
- # OAS Contribute module
4
- # @author Michinao Shimizu
5
3
  module OasContrib
6
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.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michinao Shimizu