oas_contrib 0.2.5 → 0.2.8

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: b0a7cb442ad80a5069290a8ee4452c08aa454b9db4fe0e4c6a4a9e759754269f
4
- data.tar.gz: 41ee8a0a9d722e5ec0e14cc2db1eb177d646538b4e1c4bd52a12816d8325c089
3
+ metadata.gz: 7fa1d5623f4d09450ed267bb56e209700143a0aa1d80259164ace19ea0a2d6d9
4
+ data.tar.gz: 29b486addbd6c5ca256d668b489b258ada6b8a36e0c52d9265659f322c748684
5
5
  SHA512:
6
- metadata.gz: 30b549fe4d53f3281f3b498d898251a131fd20fd4d8f5e6b70fed504bcb6935a29de10237952895287cbf5d1f7f4fce294233541cb48ce798f4caa75ceec8a92
7
- data.tar.gz: 92fbd5c8820a6b30dde1e7b7ff431d7fac8b56cc14c1392ea36cfb836b364e1f6e7fb6919ff14022ffa6639a22a16680e7164b9005c4f250fced5a165955c706
6
+ metadata.gz: c925b3c0c21f3d9e0178a5ebb9deda4cc2b6b1c20b3a14a4d6cdb655aab0a3dd93c3f4e07ebaad6fe7e4a1ca2451a5268d0497cfaa2520c4247a4016fa6b83c6
7
+ data.tar.gz: 42aaac1a8b4aab20067d4e70bedc78b2080114610807d687f5d0949c62a4c264f6ce41d7996a18cfae2739286a0bb6d90ac54be3baf99fb444e4f4735ade761a
data/.rspec_status CHANGED
@@ -1,12 +1,14 @@
1
1
  example_id | status | run_time |
2
2
  ------------------------------------ | ------ | --------------- |
3
- ./spec/divide_command_spec.rb[1:1:1] | passed | 0.51656 seconds |
4
- ./spec/divide_command_spec.rb[1:2:1] | passed | 0.42067 seconds |
5
- ./spec/divide_command_spec.rb[1:3:1] | passed | 0.45216 seconds |
6
- ./spec/divide_command_spec.rb[1:4:1] | passed | 0.35493 seconds |
7
- ./spec/divide_command_spec.rb[1:5:1] | passed | 0.31474 seconds |
8
- ./spec/merge_command_spec.rb[1:1:1] | passed | 0.34898 seconds |
9
- ./spec/merge_command_spec.rb[1:2:1] | passed | 0.3249 seconds |
10
- ./spec/merge_command_spec.rb[1:3:1] | passed | 0.3212 seconds |
11
- ./spec/merge_command_spec.rb[1:4:1] | passed | 0.41895 seconds |
12
- ./spec/merge_command_spec.rb[1:5:1] | passed | 0.75778 seconds |
3
+ ./spec/divide_command_spec.rb[1:1:1] | passed | 0.38062 seconds |
4
+ ./spec/divide_command_spec.rb[1:2:1] | passed | 0.37096 seconds |
5
+ ./spec/divide_command_spec.rb[1:3:1] | passed | 0.37719 seconds |
6
+ ./spec/divide_command_spec.rb[1:4:1] | passed | 0.36952 seconds |
7
+ ./spec/divide_command_spec.rb[1:5:1] | passed | 0.37552 seconds |
8
+ ./spec/divide_command_spec.rb[1:6:1] | passed | 0.3675 seconds |
9
+ ./spec/merge_command_spec.rb[1:1:1] | passed | 0.36627 seconds |
10
+ ./spec/merge_command_spec.rb[1:2:1] | passed | 0.35855 seconds |
11
+ ./spec/merge_command_spec.rb[1:3:1] | failed | 0.4193 seconds |
12
+ ./spec/merge_command_spec.rb[1:4:1] | passed | 0.36323 seconds |
13
+ ./spec/merge_command_spec.rb[1:5:1] | passed | 0.3595 seconds |
14
+ ./spec/merge_command_spec.rb[1:6:1] | failed | 0.35928 seconds |
data/README.md CHANGED
@@ -4,6 +4,12 @@
4
4
 
5
5
  Libraries and Commands for Open API (2.0, 3.0) Specification.
6
6
 
7
+ * Divide OAS file.
8
+ * Merge OAS divided files.
9
+ * Preview OAS file with SwaggerUI.
10
+
11
+ ![divide](/divide.jpg)
12
+
7
13
  ## Installation
8
14
 
9
15
  Add this line to your application's Gemfile:
@@ -14,48 +20,103 @@ gem 'oas_contrib'
14
20
 
15
21
  And then execute:
16
22
 
17
- $ bundle
23
+ bundle
18
24
 
19
25
  Or install it yourself as:
20
26
 
21
- $ gem install oas_contrib
27
+ gem install oas_contrib
22
28
 
23
29
  ## Usage
24
30
 
25
- ### Common rule
31
+ ### sub commands
32
+ ```bash
33
+ $ oas_contrib
34
+ Commands:
35
+ oas_contrib divide <spec_file> <output_dir> (<options>) # Divide the spec_file into path units and schema units.
36
+ oas_contrib help [COMMAND] # Describe available commands or one specific command
37
+ oas_contrib merge <input_dir> <spec_file> (<options>) # Merge multiple divided files into an spec_file.
38
+ oas_contrib preview <spec_file> (<options>) # Preview the spec_file using Swagger-UI official Docker image.
39
+ ```
26
40
 
27
- * The file extension of `<OpenAPI Specification file>` must be `.json` or `.yml`.
28
- * `<OpenAPI Specification file>` must be has the section of `swagger: 2.0.X` or `openapi: 3.0.X`.
41
+ ### divide command
42
+ ```bash
43
+ $ oas_contrib help divide
44
+ Usage:
45
+ oas_contrib divide <spec_file> <output_dir> (<options>)
29
46
 
30
- ### Divide the OpenAPI Specification file
47
+ Options:
48
+ [--out-ext=OUT_EXT] # output file ext (.yml or .yaml or .json)
49
+ # Default: .yml
31
50
 
32
- ```bash
33
- $ oas_contrib divide <OpenAPI Specification file> <output_dir> (<options>...)
51
+ Divide the spec_file into path units and schema units.
34
52
  ```
35
53
 
36
- | option | description | value type | default |
37
- |------------|------------------------------|----------------------------|----------|
38
- |`--out_ext` | the extension of output file | String (`.json` or `.yml`) | `.yml` |
54
+ #### example
39
55
 
40
- ### Merge from divided files to an OpenAPI Specification file
56
+ ##### input
57
+ [v3.yml](/example/v3.yml)
41
58
 
42
- ```bash
43
- $ oas_contrib merge <input_dir> <OpenAPI Specification file> (<options>...)
59
+ ##### execution
60
+ ```
61
+ $ oas_contrib divide v3.yml sample
62
+ Load: v3.yml
63
+ Dist: sample/meta
64
+ Dist: sample/meta/001_openapi.yml
65
+ Dist: sample/meta/002_info.yml
66
+ Dist: sample/meta/003_servers.yml
67
+ Dist: sample/path
68
+ Dist: sample/path/001_pets.yml
69
+ Dist: sample/path/002_pets_{petId}.yml
70
+ Dist: sample/model
71
+ Dist: sample/model/001_Pet.yml
72
+ Dist: sample/model/002_Error.yml
73
+ Dist: sample/model/003_Pets.yml
74
+ complete!
44
75
  ```
45
76
 
46
- | option | description | value type | default |
47
- |------------|------------------------------|----------------------------|----------|
48
- |`--in_ext` | the extension of input file | String (`.json` or `.yml`) | `.yml` |
49
-
50
- ### Preview the OpenAPI Specification file using Swagger-UI docker image
77
+ ##### output
78
+ ```
79
+ $ tree sample/
80
+ sample/
81
+ ├── meta
82
+ │   ├── 001_openapi.yml
83
+ │   ├── 002_info.yml
84
+ │   └── 003_servers.yml
85
+ ├── model
86
+ │   ├── 001_Pet.yml
87
+ │   ├── 002_Error.yml
88
+ │   └── 003_Pets.yml
89
+ └── path
90
+ ├── 001_pets.yml
91
+ └── 002_pets_{petId}.yml
92
+ ```
93
+ [output sample dir](/example/dist/v3)
51
94
 
95
+ ### merge command
52
96
  ```bash
53
- $ oas_contrib preview <OpenAPI Specification file> (<options>...)
97
+ $ oas_contrib help merge
98
+ Usage:
99
+ oas_contrib merge <input_dir> <spec_file> (<options>)
100
+
101
+ Options:
102
+ [--in-ext=IN_EXT] # input file ext (.yml or .yaml or .json)
103
+ # Default: .yml
104
+
105
+ Merge multiple divided files into an spec_file.
54
106
  ```
55
107
 
56
- | option | description | value type | default |
57
- |------------|-------------------------|-----------------|----------|
58
- |`--port` | Swagger UI listen port | Integer | `50010` |
108
+ ### preview command
109
+ ```bash
110
+ $ oas_contrib help preview
111
+ Usage:
112
+ oas_contrib preview <spec_file> (<options>)
113
+
114
+ Options:
115
+ [--port=N] # Swagger UI listen port
116
+ # Default: 50010
117
+
118
+ Preview the spec_file using Swagger-UI official Docker image.
119
+ ```
59
120
 
60
121
  ## Contributing
61
122
 
data/divide.jpg ADDED
Binary file
data/example/v2.yaml ADDED
@@ -0,0 +1,103 @@
1
+ ---
2
+ swagger: '2.0'
3
+ info:
4
+ version: 1.0.0
5
+ title: Swagger Petstore
6
+ license:
7
+ name: MIT
8
+ host: petstore.swagger.io
9
+ basePath: "/v1"
10
+ schemes:
11
+ - http
12
+ consumes:
13
+ - application/json
14
+ produces:
15
+ - application/json
16
+ components: {}
17
+ paths:
18
+ "/pets":
19
+ get:
20
+ summary: List all pets
21
+ operationId: listPets
22
+ tags:
23
+ - pets
24
+ parameters:
25
+ - name: limit
26
+ in: query
27
+ description: How many items to return at one time (max 100)
28
+ required: false
29
+ type: integer
30
+ format: int32
31
+ responses:
32
+ '200':
33
+ description: A paged array of pets
34
+ headers:
35
+ x-next:
36
+ type: string
37
+ description: A link to the next page of responses
38
+ schema:
39
+ "$ref": "#/definitions/Pets"
40
+ default:
41
+ description: unexpected error
42
+ schema:
43
+ "$ref": "#/definitions/Error"
44
+ post:
45
+ summary: Create a pet
46
+ operationId: createPets
47
+ tags:
48
+ - pets
49
+ responses:
50
+ '201':
51
+ description: Null response
52
+ default:
53
+ description: unexpected error
54
+ schema:
55
+ "$ref": "#/definitions/Error"
56
+ "/pets/{petId}":
57
+ get:
58
+ summary: Info for a specific pet
59
+ operationId: showPetById
60
+ tags:
61
+ - pets
62
+ parameters:
63
+ - name: petId
64
+ in: path
65
+ required: true
66
+ description: The id of the pet to retrieve
67
+ type: string
68
+ responses:
69
+ '200':
70
+ description: Expected response to a valid request
71
+ schema:
72
+ "$ref": "#/definitions/Pets"
73
+ default:
74
+ description: unexpected error
75
+ schema:
76
+ "$ref": "#/definitions/Error"
77
+ definitions:
78
+ Pet:
79
+ required:
80
+ - id
81
+ - name
82
+ properties:
83
+ id:
84
+ type: integer
85
+ format: int64
86
+ name:
87
+ type: string
88
+ tag:
89
+ type: string
90
+ Error:
91
+ required:
92
+ - code
93
+ - message
94
+ properties:
95
+ code:
96
+ type: integer
97
+ format: int32
98
+ message:
99
+ type: string
100
+ Pets:
101
+ type: array
102
+ items:
103
+ "$ref": "#/definitions/Pet"
data/example/v3.yaml ADDED
@@ -0,0 +1,110 @@
1
+ ---
2
+ openapi: 3.0.0
3
+ info:
4
+ version: 1.0.0
5
+ title: Swagger Petstore
6
+ license:
7
+ name: MIT
8
+ servers:
9
+ - url: http://petstore.swagger.io/v1
10
+ components:
11
+ schemas:
12
+ Pet:
13
+ required:
14
+ - id
15
+ - name
16
+ properties:
17
+ id:
18
+ type: integer
19
+ format: int64
20
+ name:
21
+ type: string
22
+ tag:
23
+ type: string
24
+ Error:
25
+ required:
26
+ - code
27
+ - message
28
+ properties:
29
+ code:
30
+ type: integer
31
+ format: int32
32
+ message:
33
+ type: string
34
+ Pets:
35
+ type: array
36
+ items:
37
+ "$ref": "#/components/schemas/Pet"
38
+ paths:
39
+ "/pets":
40
+ get:
41
+ summary: List all pets
42
+ operationId: listPets
43
+ tags:
44
+ - pets
45
+ parameters:
46
+ - name: limit
47
+ in: query
48
+ description: How many items to return at one time (max 100)
49
+ required: false
50
+ schema:
51
+ type: integer
52
+ format: int32
53
+ responses:
54
+ '200':
55
+ description: A paged array of pets
56
+ headers:
57
+ x-next:
58
+ description: A link to the next page of responses
59
+ schema:
60
+ type: string
61
+ content:
62
+ application/json:
63
+ schema:
64
+ "$ref": "#/components/schemas/Pets"
65
+ default:
66
+ description: unexpected error
67
+ content:
68
+ application/json:
69
+ schema:
70
+ "$ref": "#/components/schemas/Error"
71
+ post:
72
+ summary: Create a pet
73
+ operationId: createPets
74
+ tags:
75
+ - pets
76
+ responses:
77
+ '201':
78
+ description: Null response
79
+ default:
80
+ description: unexpected error
81
+ content:
82
+ application/json:
83
+ schema:
84
+ "$ref": "#/components/schemas/Error"
85
+ "/pets/{petId}":
86
+ get:
87
+ summary: Info for a specific pet
88
+ operationId: showPetById
89
+ tags:
90
+ - pets
91
+ parameters:
92
+ - name: petId
93
+ in: path
94
+ required: true
95
+ description: The id of the pet to retrieve
96
+ schema:
97
+ type: string
98
+ responses:
99
+ '200':
100
+ description: Expected response to a valid request
101
+ content:
102
+ application/json:
103
+ schema:
104
+ "$ref": "#/components/schemas/Pets"
105
+ default:
106
+ description: unexpected error
107
+ content:
108
+ application/json:
109
+ schema:
110
+ "$ref": "#/components/schemas/Error"
@@ -4,12 +4,17 @@ require 'oas_contrib/resolver/merge'
4
4
  require 'oas_contrib/resolver/preview'
5
5
 
6
6
  module OasContrib
7
+ # Command class
7
8
  class Command < Thor
8
9
  include Thor::Actions
9
10
 
10
11
  desc 'divide <spec_file> <output_dir> (<options>)', 'Divide the spec_file into path units and schema units.'
11
- option :out_ext, type: :string, default: '.yml', desc: 'output file ext (.yml or .json)'
12
+ option :out_ext, type: :string, default: '.yml', desc: 'output file ext (.yml or .yaml or .json)'
12
13
 
14
+ # Divide the spec file command
15
+ # @param [String] spec_file spec file path
16
+ # @param [String] outdir output directory path
17
+ # @return [Integer] return code
13
18
  def divide(spec_file, outdir)
14
19
  resolver = OasContrib::Resolver::Divide.new(spec_file, outdir, options)
15
20
  resolver.setup
@@ -17,11 +22,16 @@ module OasContrib
17
22
  resolver.resolve
18
23
  resolver.distribute
19
24
  say 'complete!', :green
25
+ exit(0)
20
26
  end
21
27
 
22
28
  desc 'merge <input_dir> <spec_file> (<options>)', 'Merge multiple divided files into an spec_file.'
23
- option :in_ext, type: :string, default: '.yml', desc: 'input file ext (.yml or .json)'
29
+ option :in_ext, type: :string, default: '.yml', desc: 'input file ext (.yml or .yaml or .json)'
24
30
 
31
+ # Merge divided files to spec file command
32
+ # @param [String] indir input directory path
33
+ # @param [String] spec_file spec file path
34
+ # @return [Integer] return code
25
35
  def merge(indir, spec_file)
26
36
  resolver = OasContrib::Resolver::Merge.new(indir, spec_file, options)
27
37
  resolver.setup
@@ -29,16 +39,21 @@ module OasContrib
29
39
  resolver.resolve
30
40
  resolver.distribute
31
41
  say 'complete!', :green
42
+ exit(0)
32
43
  end
33
44
 
34
45
  desc 'preview <spec_file> (<options>)', 'Preview the spec_file using Swagger-UI official Docker image.'
35
46
  option :port, type: :numeric, default: 50_010, desc: 'Swagger UI listen port'
36
47
 
48
+ # Preview the spec file with Swagger UI
49
+ # @param [String] spec_file spec file path
50
+ # @return [Integer] return code
37
51
  def preview(spec_file)
38
52
  resolver = OasContrib::Resolver::Preview.new(spec_file, options)
39
53
  resolver.setup
40
54
  resolver.distribute
41
55
  say 'complete!', :green
56
+ exit(0)
42
57
  end
43
58
  end
44
59
  end
@@ -1,18 +1,27 @@
1
1
  module OasContrib
2
2
  module Interface
3
+ # Interface of Resolver class
3
4
  module Resolver
5
+ # Setup the resolver object.
6
+ # @return [nil]
4
7
  def setup
5
8
  raise NotImplementedError, 'You must be implement "setup" method.'
6
9
  end
7
10
 
11
+ # Load and parse the input file.
12
+ # @return [nil]
8
13
  def load
9
14
  raise NotImplementedError, 'You must be implement "load" method.'
10
15
  end
11
16
 
17
+ # Judge and generate OpenAPI specification object.
18
+ # @return [nil]
12
19
  def resolve
13
20
  raise NotImplementedError, 'You must be implement "resolve" method.'
14
21
  end
15
22
 
23
+ # Distribute the command artifacts.
24
+ # @return [nil]
16
25
  def distribute
17
26
  raise NotImplementedError, 'You must be implement"dist" method.'
18
27
  end
@@ -1,6 +1,9 @@
1
1
  module OasContrib
2
2
  module Interface
3
+ # Interface of Spec class
3
4
  module Spec
5
+ # Mapping the hash to the spec object.
6
+ # @return [nil]
4
7
  def mapping
5
8
  raise NotImplementedError, 'You must be implement "mapping" method.'
6
9
  end
@@ -2,11 +2,16 @@ require 'oas_contrib/interface/spec'
2
2
 
3
3
  module OasContrib
4
4
  module OpenAPI
5
+ # Basical spec class
5
6
  class Base
7
+ # @!attribute [r] data
8
+ # @return [Hash] mapped spec data hash
6
9
  attr_reader :data
7
10
 
8
11
  include OasContrib::Interface::Spec
9
12
 
13
+ # Initialize
14
+ # @param [Hash] data spec data hash
10
15
  def initialize(data)
11
16
  @data = data
12
17
  end
@@ -2,14 +2,30 @@ require 'oas_contrib/openapi/base'
2
2
 
3
3
  module OasContrib
4
4
  module OpenAPI
5
+ # OpenAPI V2 module
5
6
  module V2
7
+ # Spec class
6
8
  class Spec < OasContrib::OpenAPI::Base
7
- attr_reader :meta, :path, :model
9
+ # @!attribute [r] meta
10
+ # @return [Hash] meta part
11
+ attr_reader :meta
8
12
 
13
+ # @!attribute [r] path
14
+ # @return [Hash] path part
15
+ attr_reader :path
16
+
17
+ # @!attribute [r] model
18
+ # @return [Hash] model part
19
+ attr_reader :model
20
+
21
+ # Initialize
22
+ # @param [Hash] data spec data hash
9
23
  def initialize(data)
10
24
  super
11
25
  end
12
26
 
27
+ # Mapping the hash to the spec object.
28
+ # @return [OpenAPI::V2::Spec] mapped spec data object
13
29
  def mapping
14
30
  @meta = data.select { |v| v != 'paths' && v != 'definitions' } || nil
15
31
  @path = data['paths'] || nil
@@ -2,14 +2,30 @@ require 'oas_contrib/openapi/base'
2
2
 
3
3
  module OasContrib
4
4
  module OpenAPI
5
+ # OpenAPI V3 module
5
6
  module V3
7
+ # Spec class
6
8
  class Spec < OasContrib::OpenAPI::Base
7
- attr_reader :meta ,:path, :model
9
+ # @!attribute [r] meta
10
+ # @return [Hash] meta part
11
+ attr_reader :meta
8
12
 
13
+ # @!attribute [r] path
14
+ # @return [Hash] path part
15
+ attr_reader :path
16
+
17
+ # @!attribute [r] model
18
+ # @return [Hash] model part
19
+ attr_reader :model
20
+
21
+ # Initialize
22
+ # @param [Hash] data spec data hash
9
23
  def initialize(data)
10
24
  super
11
25
  end
12
26
 
27
+ # Mapping the hash to the spec object.
28
+ # @return [OpenAPI::V3::Spec] mapped spec data object
13
29
  def mapping
14
30
  @meta = data.select { |v| v != 'paths' && v != 'components' } || nil
15
31
  @path = data['paths'] || nil
@@ -6,13 +6,32 @@ require 'json'
6
6
 
7
7
  module OasContrib
8
8
  module Resolver
9
+ # Basical command resolver class
9
10
  class Base
10
- attr_reader :data, :spec
11
+ # @!attribute [r] data
12
+ # @return [Hash] parsed input data hash
13
+ attr_reader :data
14
+
15
+ # @!attribute [r] spec
16
+ # @return [OpenAPI::V3::Spec|OpenAPI::V2::Spec] mapped spec data object
17
+ attr_reader :spec
11
18
 
12
19
  include OasContrib::Interface::Resolver
13
20
 
14
- DEFINED_FILE_EXT = ['.json', '.yml'].freeze
21
+ # @return [Array] approval file extensions
22
+ DEFINED_FILE_EXT = ['.json', '.yml', '.yaml'].freeze
23
+
24
+ # @return [String] the directory name of meta part files
25
+ DIR_NAME_META = 'meta'.freeze
26
+
27
+ # @return [String] the directory name of path part files
28
+ DIR_NAME_PATH = 'path'.freeze
29
+
30
+ # @return [String] the directory name of model part files
31
+ DIR_NAME_MODEL = 'model'.freeze
15
32
 
33
+ # Check the file extensions is approved or not.
34
+ # @return [Boolean]
16
35
  def file_ext_check
17
36
  if @infile_ext && !DEFINED_FILE_EXT.include?(@infile_ext)
18
37
  raise "Undefined input file extension. #{@infile_ext}"
@@ -21,16 +40,23 @@ module OasContrib
21
40
  if @outfile_ext && !DEFINED_FILE_EXT.include?(@outfile_ext)
22
41
  raise "Undefined output file extension. #{@outfile_ext}"
23
42
  end
43
+ true
24
44
  end
25
45
 
46
+ # Check the format of input file is OpenAPI v3 specificaion or not.
47
+ # @return [Boolean]
26
48
  def v3?
27
49
  @data['openapi'] =~ /^3/
28
50
  end
29
51
 
52
+ # Check the format of input file is OpenAPI v2 specificaion or not.
53
+ # @return [Boolean]
30
54
  def v2?
31
55
  @data['swagger'] =~ /^2/
32
56
  end
33
57
 
58
+ # Judge and generate OpenAPI specification object.
59
+ # @return [OpenAPI::V3::Spec|OpenAPI::V2::Spec] mapped spec data object
34
60
  def resolve
35
61
  @spec = OasContrib::OpenAPI::V2::Spec.new(@data) if v2?
36
62
  @spec = OasContrib::OpenAPI::V3::Spec.new(@data) if v3?
@@ -38,19 +64,31 @@ module OasContrib
38
64
  @spec.mapping
39
65
  end
40
66
 
67
+ # Load and parse the input file.
68
+ # @param [String] path input file path
69
+ # @return [Hash] parsed input data hash
41
70
  def input(path)
42
71
  @data = _input(path)
43
72
  end
44
73
 
74
+ # Output a new file with mapped spec data hash.
75
+ # @param [Hash] hash mapped spec data hash
76
+ # @param [String] path output file path
77
+ # @return [IO]
45
78
  def output(hash, path)
46
79
  File.open(path, 'w') { |f| _output(hash, f) }
47
80
  end
48
81
 
82
+ # Load and parse the files in target directory recursive.
83
+ # @param [String] dir input directory path
84
+ # @return [Hash] parsed input data hash
49
85
  def input_dir(dir)
50
86
  path = dir + '/**/*' + @infile_ext
51
87
  Dir.glob(path).sort.each_with_object({}, &input_lambda)
52
88
  end
53
89
 
90
+ # Load and parse the file proc.
91
+ # @return [Proc]
54
92
  def input_lambda
55
93
  lambda do |file, result|
56
94
  hash = _input(file)
@@ -61,20 +99,30 @@ module OasContrib
61
99
 
62
100
  private
63
101
 
102
+ # Load and prase the file depending on the file extension.
103
+ # @param [String] path input file path
104
+ # @return [Hash] parsed input data hash
64
105
  def _input(path)
65
106
  puts "Load: #{path}"
66
107
  case @infile_ext
67
- when '.yml' then YAML.load_file(path)
68
- when '.json' then JSON.parse(File.read(path))
108
+ when DEFINED_FILE_EXT[0] then JSON.parse(File.read(path))
109
+ when DEFINED_FILE_EXT[1] then YAML.load_file(path)
110
+ when DEFINED_FILE_EXT[2] then YAML.load_file(path)
69
111
  else raise ArgumentError, 'Undefined input file type'
70
112
  end
71
113
  end
72
114
 
115
+ # Write the spec data hash depending on the file extension.
116
+ # @param [Hash] hash mapped spec data hash
117
+ # @param [String] file output file path
118
+ # @return [IO]
73
119
  def _output(hash, file)
74
120
  puts "Dist: #{file.path}"
121
+ p @outfile_ext
75
122
  case @outfile_ext
76
- when '.yml' then YAML.dump(hash, file)
77
- when '.json' then JSON.dump(hash, file)
123
+ when DEFINED_FILE_EXT[0] then JSON.dump(hash, file)
124
+ when DEFINED_FILE_EXT[1] then YAML.dump(hash, file)
125
+ when DEFINED_FILE_EXT[2] then YAML.dump(hash, file)
78
126
  else raise ArgumentError, 'Undefined output file type'
79
127
  end
80
128
  end
@@ -2,32 +2,50 @@ require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
4
  module Resolver
5
+ # Divide command resolver class
5
6
  class Divide < OasContrib::Resolver::Base
7
+ # Initialize
8
+ # @param [String] infile spec file path
9
+ # @param [String] outdir output directory path
10
+ # @param [Array] options command options
6
11
  def initialize(infile, outdir, options)
7
- @meta_dir = outdir + '/meta'
8
- @path_dir = outdir + '/path'
9
- @model_dir = outdir + '/model'
12
+ @meta_dir = outdir + '/' + DIR_NAME_META
13
+ @path_dir = outdir + '/' + DIR_NAME_PATH
14
+ @model_dir = outdir + '/' + DIR_NAME_MODEL
10
15
  @infile = infile
11
16
  @outfile_ext = options['out_ext']
12
17
  end
13
18
 
19
+ # Setup the resolver object.
20
+ # @return [Boolean]
14
21
  def setup
15
22
  @infile_ext = File.extname(@infile)
16
23
  file_ext_check
24
+ true
17
25
  end
18
26
 
27
+ # Load and parse the input file.
28
+ # @return [Boolean]
19
29
  def load
20
30
  input(@infile)
31
+ true
21
32
  end
22
33
 
34
+ # Distribute the command artifacts.
35
+ # @return [Boolean]
23
36
  def distribute
24
37
  output_dir(@spec.meta, @meta_dir)
25
38
  output_dir(@spec.path, @path_dir)
26
39
  output_dir(@spec.model, @model_dir)
40
+ true
27
41
  end
28
42
 
29
43
  private
30
44
 
45
+ # Generate directory and output files.
46
+ # @param [Hash] hash mapped spec data hash
47
+ # @param [String] path output directory path
48
+ # @return [Boolean]
31
49
  def output_dir(hash, path)
32
50
  puts "Dist: #{path}"
33
51
  FileUtils.mkdir_p(path)
@@ -36,15 +54,24 @@ module OasContrib
36
54
  outfile_data = _output_dir_file_data_filter(hash, k)
37
55
  output(outfile_data, outfile_path)
38
56
  end
39
- nil
57
+ true
40
58
  end
41
59
 
60
+ # Modify the output file path.
61
+ # @param [String] dir output directory path
62
+ # @param [String] hash_key hash key
63
+ # @param [Integer] num count of file
64
+ # @return [String] modified file path
42
65
  def _output_dir_file_path_modify(dir, hash_key, num)
43
66
  prefix = num.to_s.rjust(3, '0')
44
67
  file_name = hash_key.tr('/', '_').gsub(/^_/, '')
45
68
  dir + '/' + prefix + '_' + file_name + @outfile_ext
46
69
  end
47
70
 
71
+ # Filter the output file data.
72
+ # @param [Hash] hash mapped spec data hash
73
+ # @param [String] filter_key filtering hash key
74
+ # @return [Hash] filterd hash
48
75
  def _output_dir_file_data_filter(hash, filter_key)
49
76
  hash.select { |key, _| key == filter_key }
50
77
  end
@@ -2,30 +2,44 @@ require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
4
  module Resolver
5
+ # Merge command resolver class
5
6
  class Merge < OasContrib::Resolver::Base
7
+ # Initialize
8
+ # @param [String] indir input directory path
9
+ # @param [String] outfile output spec file path
10
+ # @param [Array] options command options
6
11
  def initialize(indir, outfile, options)
7
- @meta_dir = indir + '/meta'
8
- @path_dir = indir + '/path'
9
- @model_dir = indir + '/model'
12
+ @meta_dir = indir + '/' + DIR_NAME_META
13
+ @path_dir = indir + '/' + DIR_NAME_PATH
14
+ @model_dir = indir + '/' + DIR_NAME_MODEL
10
15
  @outfile = outfile
11
16
  @infile_ext = options['in_ext']
12
17
  end
13
18
 
19
+ # Setup the resolver object.
20
+ # @return [Boolean]
14
21
  def setup
15
22
  @outfile_ext = File.extname(@outfile)
16
23
  file_ext_check
24
+ true
17
25
  end
18
26
 
27
+ # Load and parse the input files.
28
+ # @return [Boolean]
19
29
  def load
20
30
  @data = input_dir(@meta_dir)
21
31
  resolve
22
32
  @data['paths'] = input_dir(@path_dir)
23
33
  @data['definitions'] = input_dir(@model_dir) if v2?
24
34
  @data['components'] = { 'schemas' => input_dir(@model_dir) } if v3?
35
+ true
25
36
  end
26
37
 
38
+ # Distribute the command artifacts.
39
+ # @return [Boolean]
27
40
  def distribute
28
41
  output(@data, @outfile)
42
+ true
29
43
  end
30
44
  end
31
45
  end
@@ -2,25 +2,35 @@ require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
4
  module Resolver
5
+ # Preview command resolver class
5
6
  class Preview < OasContrib::Resolver::Base
7
+ # Initialize
8
+ # @param [String] infile spec file path
9
+ # @param [Array] options command options
6
10
  def initialize(infile, options)
7
11
  @infile = infile
8
12
  @port = options['port']
9
13
  end
10
14
 
15
+ # Setup the resolver object.
16
+ # @return [Boolean]
11
17
  def setup
12
18
  @expand_path = File.expand_path(@infile)
13
19
  @basename = File.basename(@expand_path)
14
20
  @infile_ext = File.extname(@infile)
15
21
  file_ext_check
22
+ true
16
23
  end
17
24
 
25
+ # Distribute the command artifacts.
26
+ # @return [Boolean]
18
27
  def distribute
19
28
  puts "SwaggerUI listen: http://localhost:#{@port} with: #{@expand_path}"
20
29
  `docker run --rm --name oas_contrib_preview_swagger_ui \
21
30
  -p #{@port}:8080 -e API_URL=#{@basename} \
22
31
  -v #{@expand_path}:/usr/share/nginx/html/#{@basename} swaggerapi/swagger-ui`
23
32
  raise 'Preview command needs docker.' unless $?.exitstatus.zero?
33
+ true
24
34
  end
25
35
  end
26
36
  end
@@ -1,3 +1,4 @@
1
1
  module OasContrib
2
- VERSION = '0.2.5'.freeze
2
+ # @return [String] semantic version
3
+ VERSION = '0.2.8'.freeze
3
4
  end
data/lib/oas_contrib.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'oas_contrib/version'
2
2
 
3
+ # Main module
3
4
  module OasContrib
4
5
  end
data/oas_contrib.gemspec CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |spec|
7
7
  spec.version = OasContrib::VERSION
8
8
  spec.authors = ['Michinao Shimizu']
9
9
  spec.email = ['shimizu.michinao@gmail.com']
10
- spec.summary = 'Libraries and Commands for Open API (2.0, 3.0) Specification.'
11
- spec.description = 'Libraries and Commands for Open API (2.0, 3.0) Specification.'
10
+ spec.summary = 'Libraries and Commands for Open API (2.X, 3.X) Specification.'
11
+ spec.description = 'Divide OAS file (2.X, 3.X). Merge divided files to a OAS file. Preview OAS file with SwaggerUI Docker Container.'
12
12
  spec.homepage = 'https://github.com/MichinaoShimizu/oas_contrib'
13
13
  spec.license = 'MIT'
14
14
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -17,8 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.bindir = 'exe'
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
- spec.add_dependency 'thor', '~> 0.20'
21
- spec.add_development_dependency 'bundler', '~> 1.16'
22
- spec.add_development_dependency 'rake', '~> 10.0'
23
- spec.add_development_dependency 'rspec', '~> 3.0'
20
+ spec.add_dependency 'thor', '> 0.20'
21
+ spec.add_development_dependency 'bundler', '> 2.0.1'
22
+ spec.add_development_dependency 'rake', '> 10.0'
23
+ spec.add_development_dependency 'rspec', '> 3.0'
24
24
  end
metadata CHANGED
@@ -1,72 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michinao Shimizu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-08 00:00:00.000000000 Z
11
+ date: 2022-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.20'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.20'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.16'
33
+ version: 2.0.1
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.16'
40
+ version: 2.0.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: Libraries and Commands for Open API (2.0, 3.0) Specification.
69
+ description: Divide OAS file (2.X, 3.X). Merge divided files to a OAS file. Preview
70
+ OAS file with SwaggerUI Docker Container.
70
71
  email:
71
72
  - shimizu.michinao@gmail.com
72
73
  executables:
@@ -83,6 +84,7 @@ files:
83
84
  - Rakefile
84
85
  - bin/console
85
86
  - bin/setup
87
+ - divide.jpg
86
88
  - example/dist/v2/meta/001_swagger.yml
87
89
  - example/dist/v2/meta/002_info.yml
88
90
  - example/dist/v2/meta/003_host.yml
@@ -104,7 +106,9 @@ files:
104
106
  - example/dist/v3/model/003_Pets.yml
105
107
  - example/dist/v3/path/001_pets.yml
106
108
  - example/dist/v3/path/002_pets_{petId}.yml
109
+ - example/v2.yaml
107
110
  - example/v2.yml
111
+ - example/v3.yaml
108
112
  - example/v3.yml
109
113
  - exe/oas_contrib
110
114
  - lib/oas_contrib.rb
@@ -139,9 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
143
  - !ruby/object:Gem::Version
140
144
  version: '0'
141
145
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.7.6
146
+ rubygems_version: 3.0.3
144
147
  signing_key:
145
148
  specification_version: 4
146
- summary: Libraries and Commands for Open API (2.0, 3.0) Specification.
149
+ summary: Libraries and Commands for Open API (2.X, 3.X) Specification.
147
150
  test_files: []