oas_contrib 0.2.0 → 0.2.1

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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec_status +12 -0
  3. data/README.md +42 -42
  4. data/example/dist/{swagger_v2 → v2}/meta/001_swagger.yml +0 -0
  5. data/example/dist/{openapi_v3 → v2}/meta/002_info.yml +0 -0
  6. data/example/dist/{swagger_v2 → v2}/meta/003_host.yml +0 -0
  7. data/example/dist/{swagger_v2 → v2}/meta/004_basePath.yml +0 -0
  8. data/example/dist/{swagger_v2 → v2}/meta/005_schemes.yml +0 -0
  9. data/example/dist/{swagger_v2 → v2}/meta/006_consumes.yml +0 -0
  10. data/example/dist/{swagger_v2 → v2}/meta/007_produces.yml +0 -0
  11. data/example/dist/{swagger_v2 → v2}/meta/008_components.yml +0 -0
  12. data/example/dist/{openapi_v3 → v2}/model/001_Pet.yml +0 -0
  13. data/example/dist/{openapi_v3 → v2}/model/002_Error.yml +0 -0
  14. data/example/dist/{swagger_v2 → v2}/model/003_Pets.yml +0 -0
  15. data/example/dist/{swagger_v2 → v2}/path/001_pets.yml +0 -0
  16. data/example/dist/{swagger_v2 → v2}/path/002_pets_{petId}.yml +0 -0
  17. data/example/dist/{openapi_v3 → v3}/meta/001_openapi.yml +0 -0
  18. data/example/dist/{swagger_v2 → v3}/meta/002_info.yml +0 -0
  19. data/example/dist/{openapi_v3 → v3}/meta/003_servers.yml +0 -0
  20. data/example/dist/{swagger_v2 → v3}/model/001_Pet.yml +0 -0
  21. data/example/dist/{swagger_v2 → v3}/model/002_Error.yml +0 -0
  22. data/example/dist/{openapi_v3 → v3}/model/003_Pets.yml +0 -0
  23. data/example/dist/{openapi_v3 → v3}/path/001_pets.yml +0 -0
  24. data/example/dist/{openapi_v3 → v3}/path/002_pets_{petId}.yml +0 -0
  25. data/example/{sample_petstore_swagger_v2.yml → v2.yml} +0 -0
  26. data/example/{sample_petstore_openapi_v3.yml → v3.yml} +0 -0
  27. data/lib/oas_contrib/command.rb +6 -23
  28. data/lib/oas_contrib/{swagger → openapi}/v2/spec.rb +2 -2
  29. data/lib/oas_contrib/openapi/v3/spec.rb +1 -1
  30. data/lib/oas_contrib/resolver/base.rb +161 -0
  31. data/lib/oas_contrib/{command_resolvers/divide_command_resolver.rb → resolver/divide.rb} +11 -3
  32. data/lib/oas_contrib/{command_resolvers/merge_command_resolver.rb → resolver/merge.rb} +14 -5
  33. data/lib/oas_contrib/resolver/preview.rb +42 -0
  34. data/lib/oas_contrib/version.rb +1 -1
  35. data/oas_contrib.gemspec +2 -5
  36. metadata +33 -31
  37. data/lib/oas_contrib/command_resolver_base.rb +0 -140
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f74cdce165853e7f21d8a71c42d3c07665166c383eb14ae920f9f13b9179647
4
- data.tar.gz: 7a9676bc8f852f38175716222ba5dcc2cffbd89a4753621e1ee8b8c1d14cd6db
3
+ metadata.gz: 2b2dd9f63fca90af7dfadb2acb5bbcf3298bcbe8124f058228ef2e039702e519
4
+ data.tar.gz: 4077394dfc935cf038f283147840a9bb95600774bec1f0ccb2c21254edc1500a
5
5
  SHA512:
6
- metadata.gz: f1cbb48a8848b56dc402f86329c673883ec8e1a8eb86a9363edea17903041d03f7c0a9e0a11586ccacfd330093d2754fcb7bfd46f24e0cf671acf792c6be4a4c
7
- data.tar.gz: 18a35b2c39802c1e7e5902203b5f40ce6f891a149d8527637da0e12a54dc850798333c4b80151ef867613dd577439df91ef4df59ce50e477344bcf6879537c87
6
+ metadata.gz: 63164dd1c20142588d8453c87c4a9043107ae3ba7f9a6d9072dd4639b5d6886a4333d7de0acc9a39b85cc20870c27db9df623aebf318aee8d95464afd7c04b67
7
+ data.tar.gz: 396ae10edea63c1a1198ae20054d631e2c4ade37d2e073965f6d6714014755a6bfb3e6c6ee57114b214aa1782cbdc83ca6a91a26382dee9751d7bcbb804d7823
data/.rspec_status ADDED
@@ -0,0 +1,12 @@
1
+ example_id | status | run_time |
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 |
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/oas_contrib.svg)](https://badge.fury.io/rb/oas_contrib)
4
4
 
5
- Libraries and Commands for OpenAPI Specification.
5
+ Libraries and Commands for Open API (2.0, 3.0) Specification.
6
6
 
7
7
  ## Installation
8
8
 
@@ -27,10 +27,10 @@ Or install it yourself as:
27
27
  Divide the OAS file into path units and schema units.
28
28
 
29
29
  ```bash
30
- $ oas_contrib divide <input_file> <output_dir> (<options>...)
30
+ $ oas_contrib divide <OAS file> <output_dir> (<options>...)
31
31
  ```
32
32
 
33
- `<input_file>` must be `.json` or `.yml`
33
+ `<OAS file>` must be `.json` or `.yml`
34
34
 
35
35
  #### Options
36
36
 
@@ -42,24 +42,24 @@ $ oas_contrib divide <input_file> <output_dir> (<options>...)
42
42
 
43
43
  ##### Open API 3.0
44
44
 
45
- [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/sample_petstore_openapi_v3.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/openapi_v3)
45
+ [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/v3.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/v3)
46
46
 
47
47
  ```
48
- $ oas_contrib divide example/sample_petstore_openapi_v3.yml example/dist/openapi_v3
49
- Load: example/sample_petstore_openapi_v3.yml
50
- Dist: example/dist/openapi_v3/meta
51
- Dist: example/dist/openapi_v3/meta/001_openapi.yml
52
- Dist: example/dist/openapi_v3/meta/002_info.yml
53
- Dist: example/dist/openapi_v3/meta/003_servers.yml
54
- Dist: example/dist/openapi_v3/path
55
- Dist: example/dist/openapi_v3/path/001_pets.yml
56
- Dist: example/dist/openapi_v3/path/002_pets_{petId}.yml
57
- Dist: example/dist/openapi_v3/model
58
- Dist: example/dist/openapi_v3/model/001_Pet.yml
59
- Dist: example/dist/openapi_v3/model/002_Error.yml
60
-
61
- $ tree example/dist/openapi_v3/
62
- example/dist/openapi_v3/
48
+ $ oas_contrib divide example/v3.yml example/dist/v3
49
+ Load: example/v3.yml
50
+ Dist: example/dist/v3/meta
51
+ Dist: example/dist/v3/meta/001_openapi.yml
52
+ Dist: example/dist/v3/meta/002_info.yml
53
+ Dist: example/dist/v3/meta/003_servers.yml
54
+ Dist: example/dist/v3/path
55
+ Dist: example/dist/v3/path/001_pets.yml
56
+ Dist: example/dist/v3/path/002_pets_{petId}.yml
57
+ Dist: example/dist/v3/model
58
+ Dist: example/dist/v3/model/001_Pet.yml
59
+ Dist: example/dist/v3/model/002_Error.yml
60
+
61
+ $ tree example/dist/v3/
62
+ example/dist/v3/
63
63
  ├── meta
64
64
  │ ├── 001_openapi.yml
65
65
  │ ├── 002_info.yml
@@ -73,33 +73,33 @@ example/dist/openapi_v3/
73
73
  └── 002_pets_{petId}.yml
74
74
  ```
75
75
 
76
- ##### Swagger v2
76
+ ##### Open API 2.0
77
77
 
78
- [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/sample_petstore_swagger_v2.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/swagger_v2)
78
+ [BEFORE](https://github.com/MichinaoShimizu/oas_contrib/blob/master/example/v2.yml) -> [AFTER](https://github.com/MichinaoShimizu/oas_contrib/tree/master/example/dist/v2)
79
79
 
80
80
  ```
81
- $ oas_contrib divide example/sample_petstore_swagger_v2.yml example/dist/swagger_v2
82
- Load: example/sample_petstore_swagger_v2.yml
83
- Dist: example/dist/swagger_v2/meta
84
- Dist: example/dist/swagger_v2/meta/001_swagger.yml
85
- Dist: example/dist/swagger_v2/meta/002_info.yml
86
- Dist: example/dist/swagger_v2/meta/003_host.yml
87
- Dist: example/dist/swagger_v2/meta/004_basePath.yml
88
- Dist: example/dist/swagger_v2/meta/005_schemes.yml
89
- Dist: example/dist/swagger_v2/meta/006_consumes.yml
90
- Dist: example/dist/swagger_v2/meta/007_produces.yml
91
- Dist: example/dist/swagger_v2/meta/008_components.yml
92
- Dist: example/dist/swagger_v2/path
93
- Dist: example/dist/swagger_v2/path/001_pets.yml
94
- Dist: example/dist/swagger_v2/path/002_pets_{petId}.yml
95
- Dist: example/dist/swagger_v2/model
96
- Dist: example/dist/swagger_v2/model/001_Pet.yml
97
- Dist: example/dist/swagger_v2/model/002_Error.yml
98
- Dist: example/dist/swagger_v2/model/003_Pets.yml
81
+ $ oas_contrib divide example/v2.yml example/dist/v2
82
+ Load: example/v2.yml
83
+ Dist: example/dist/v2/meta
84
+ Dist: example/dist/v2/meta/001_swagger.yml
85
+ Dist: example/dist/v2/meta/002_info.yml
86
+ Dist: example/dist/v2/meta/003_host.yml
87
+ Dist: example/dist/v2/meta/004_basePath.yml
88
+ Dist: example/dist/v2/meta/005_schemes.yml
89
+ Dist: example/dist/v2/meta/006_consumes.yml
90
+ Dist: example/dist/v2/meta/007_produces.yml
91
+ Dist: example/dist/v2/meta/008_components.yml
92
+ Dist: example/dist/v2/path
93
+ Dist: example/dist/v2/path/001_pets.yml
94
+ Dist: example/dist/v2/path/002_pets_{petId}.yml
95
+ Dist: example/dist/v2/model
96
+ Dist: example/dist/v2/model/001_Pet.yml
97
+ Dist: example/dist/v2/model/002_Error.yml
98
+ Dist: example/dist/v2/model/003_Pets.yml
99
99
 
100
100
  $ tree example/dist/
101
101
  example/dist/
102
- └── swagger_v2
102
+ └── v2
103
103
  ├── meta
104
104
  │ ├── 001_swagger.yml
105
105
  │ ├── 002_info.yml
@@ -123,10 +123,10 @@ example/dist/
123
123
  Merge multiple divided files into an OAS file.
124
124
 
125
125
  ```bash
126
- $ oas_contrib merge <input_dir> <output_file> (<options>...)
126
+ $ oas_contrib merge <input_dir> <OAS file> (<options>...)
127
127
  ```
128
128
 
129
- `<output_file>` must be `.json` or `.yml`
129
+ `<OAS file>` must be `.json` or `.yml`
130
130
 
131
131
  #### Options
132
132
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -1,55 +1,38 @@
1
1
  require 'thor'
2
- require 'oas_contrib/command_resolvers/divide_command_resolver'
3
- require 'oas_contrib/command_resolvers/merge_command_resolver'
2
+ require 'oas_contrib/resolver/divide'
3
+ require 'oas_contrib/resolver/merge'
4
+ require 'oas_contrib/resolver/preview'
4
5
 
5
6
  module OasContrib
6
7
  # Commands
7
8
  class Command < Thor
8
9
  option :out_type, type: :string, aliases: '-ot', default: 'yaml', desc: 'output file type (yaml or json)'
9
10
  desc 'divide <input_file> <output_dir>', 'Divide the OAS file into path units and schema units.'
10
-
11
11
  # Command (Divide the OAS file into path units and schema units)
12
12
  # @param [String] in_file input file path
13
13
  # @param [String] out_dir output directory path
14
14
  # @return [Integer]
15
15
  def divide(in_file, out_dir)
16
- resolver = CommandResolvers::DivideCommandResolver.new(in_file, out_dir, options['out_type'])
17
- resolver.load
18
- resolver.resolve
19
- resolver.dist
20
- exit(0)
16
+ Resolver::Divide.new(in_file, out_dir, options['out_type']).run
21
17
  end
22
18
 
23
19
  option :in_type, type: :string, aliases: '-it', default: 'yaml', desc: 'input file type (yaml or json)'
24
20
  desc 'merge <input_dir> <output_file>', 'Merge multiple divided files into an OAS file.'
25
-
26
21
  # Command (Merge multiple divided files into an OAS file)
27
22
  # @param [String] in_dir input directory path
28
23
  # @param [String] out_file output file path
29
24
  # @return [Integer]
30
25
  def merge(in_dir, out_file)
31
- resolver = CommandResolvers::MergeCommandResolver.new(in_dir, out_file, options['in_type'])
32
- resolver.load
33
- resolver.resolve
34
- resolver.dist
35
- exit(0)
26
+ Resolver::Merge.new(in_dir, out_file, options['in_type']).run
36
27
  end
37
28
 
38
29
  option :port, type: :string, aliases: '-p', default: '50010', desc: 'Swagger UI listen port'
39
30
  desc 'preview <input_file>', 'Preview OAS file using Swagger-UI official Docker image.'
40
-
41
31
  # Preview OAS file using Swagger-UI official Docker image.
42
32
  # @param [String] in_file input file path
43
33
  # @return [Integer]
44
34
  def preview(in_file)
45
- port = options['port']
46
- path = File.expand_path(in_file)
47
- basename = File.basename(path)
48
- puts "SwaggerUI listen: http://localhost:#{port} with: #{in_file}"
49
- Kernel.exec "docker run --rm --name oas_contrib_preview_swagger_ui \
50
- -p #{port}:8080 -e API_URL=#{basename} \
51
- -v #{path}:/usr/share/nginx/html/#{basename} swaggerapi/swagger-ui"
52
- exit(0)
35
+ Resolver::Preview.new(in_file, options['port']).run
53
36
  end
54
37
  end
55
38
  end
@@ -1,6 +1,6 @@
1
1
  module OasContrib
2
- # Swagger module
3
- module Swagger
2
+ # OpenAPI module
3
+ module OpenAPI
4
4
  # Version 2 module
5
5
  module V2
6
6
  # Spec
@@ -1,5 +1,5 @@
1
1
  module OasContrib
2
- # Open API definition module
2
+ # OpenAPI
3
3
  module OpenAPI
4
4
  # Version 3 module
5
5
  module V3
@@ -0,0 +1,161 @@
1
+ require 'oas_contrib/openapi/v2/spec'
2
+ require 'oas_contrib/openapi/v3/spec'
3
+ require 'yaml'
4
+ require 'json'
5
+
6
+ module OasContrib
7
+ module Resolver
8
+ # CommandResolver Base
9
+ 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
24
+
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
38
+
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
47
+
48
+ # <Description>
49
+ # @return [<Type>] <description>
50
+ def has_v3_meta?
51
+ @load_data['openapi'] =~ /^3/
52
+ end
53
+
54
+ # <Description>
55
+ # @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)
64
+ end
65
+
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
82
+ end
83
+
84
+ # Load a file
85
+ # @param [String] path input file path
86
+ # @return [Hash]
87
+ def input(path)
88
+ @load_data = input_call(path)
89
+ end
90
+
91
+ # Output a file
92
+ # @param [Hash] hash data
93
+ # @param [String] path output file path
94
+ # @return [File]
95
+ 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)
104
+ end
105
+
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]
113
+ end
114
+ end
115
+
116
+ # Load a file depending on file extension
117
+ # @param [String] path file path
118
+ # @raise [ArgumentError] invalid file type string
119
+ # @return [Hash]
120
+ def input_call(path)
121
+ puts "Load: #{path}"
122
+ case @input_file_ext
123
+ when '.yml' then YAML.load_file(path)
124
+ when '.json' then JSON.parse(File.read(path))
125
+ else raise ArgumentError, 'Undefined file type'
126
+ end
127
+ end
128
+
129
+ # Output a file depending on file extension
130
+ # @param [String] path file path
131
+ # @raise [ArgumentError] invalid file type string
132
+ # @return [IO]
133
+ def output_call(hash, file)
134
+ puts "Dist: #{file.path}"
135
+ case @output_file_ext
136
+ when '.yml' then YAML.dump(hash, file)
137
+ 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
156
+ end
157
+ nil
158
+ end
159
+ end
160
+ end
161
+ end
@@ -1,10 +1,10 @@
1
- require 'oas_contrib/command_resolver_base'
1
+ require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
4
  # Command Resolvers
5
- module CommandResolvers
5
+ module Resolver
6
6
  # Divide command resolver
7
- class DivideCommandResolver < CommandResolverBase
7
+ class Divide < Resolver::Base
8
8
  # Initialize
9
9
  # @param [String] input_file_path input file path
10
10
  # @param [String] output_dir_path output directory path
@@ -16,6 +16,14 @@ module OasContrib
16
16
  super(output_dir_path)
17
17
  end
18
18
 
19
+ # Run
20
+ # @return [nil]
21
+ def run
22
+ load
23
+ resolve
24
+ dist
25
+ end
26
+
19
27
  # Load the OAS file
20
28
  # @return [Hash] loaded data
21
29
  def load
@@ -1,10 +1,10 @@
1
- require 'oas_contrib/command_resolver_base'
1
+ require 'oas_contrib/resolver/base'
2
2
 
3
3
  module OasContrib
4
4
  # Command Resolvers
5
- module CommandResolvers
5
+ module Resolver
6
6
  # Merge command resolver
7
- class MergeCommandResolver < CommandResolverBase
7
+ class Merge < Resolver::Base
8
8
  # Initialize
9
9
  # @param [String] input_dir_path input directory path
10
10
  # @param [String] output_file_path output file path
@@ -16,6 +16,14 @@ module OasContrib
16
16
  super(input_dir_path)
17
17
  end
18
18
 
19
+ # Run
20
+ # @return [nil]
21
+ def run
22
+ load
23
+ resolve
24
+ dist
25
+ end
26
+
19
27
  # Load divided files
20
28
  # @return [Hash] load and merged data
21
29
  def load
@@ -49,10 +57,11 @@ module OasContrib
49
57
  # @return [Hash] load and merged data
50
58
  def load_model
51
59
  path = @model_dir + '/**/*' + @input_file_ext
52
- @load_data['definitions'] = input_dir(path) if swagger_v2?
53
- if openapi_v3?
60
+ if v3_spec?
54
61
  @load_data['components'] = {}
55
62
  @load_data['components']['schemas'] = input_dir(path)
63
+ elsif v2_spec?
64
+ @load_data['definitions'] = input_dir(path)
56
65
  end
57
66
  end
58
67
  end
@@ -0,0 +1,42 @@
1
+ require 'oas_contrib/resolver/base'
2
+
3
+ module OasContrib
4
+ # Command Resolvers
5
+ module Resolver
6
+ # Preview command resolver
7
+ class Preview < Resolver::Base
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
14
+ end
15
+
16
+ # Run
17
+ # @return [nil]
18
+ def run
19
+ load
20
+ dist
21
+ end
22
+
23
+ # Load
24
+ # @return [nil]
25
+ def load
26
+ @expand_path = File.expand_path(@input_file_path)
27
+ @basename = File.basename(@expand_path)
28
+ nil
29
+ end
30
+
31
+ # Distribute
32
+ # @return [nil]
33
+ def dist
34
+ puts "SwaggerUI listen: http://localhost:#{@port} with: #{@expand_path}"
35
+ `docker run --rm --name oas_contrib_preview_swagger_ui \
36
+ -p #{@port}:8080 -e API_URL=#{@basename} \
37
+ -v #{@expand_path}:/usr/share/nginx/html/#{@basename} swaggerapi/swagger-ui`
38
+ raise 'Preview command needs docker.' unless $?.exitstatus.zero?
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,4 @@
1
1
  module OasContrib
2
2
  # @return [String] version
3
- VERSION = '0.2.0'.freeze
3
+ VERSION = '0.2.1'.freeze
4
4
  end
data/oas_contrib.gemspec CHANGED
@@ -7,19 +7,16 @@ 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
-
11
- spec.summary = 'Open API Specification related commands and libraries.'
12
- spec.description = 'Open API Specification related commands and libraries.'
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.'
13
12
  spec.homepage = 'https://github.com/MichinaoShimizu/oas_contrib'
14
13
  spec.license = 'MIT'
15
-
16
14
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
15
  f.match(%r{^(test|spec|features)/})
18
16
  end
19
17
  spec.bindir = 'exe'
20
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
19
  spec.require_paths = ['lib']
22
-
23
20
  spec.add_dependency 'thor', '~> 0.20'
24
21
  spec.add_development_dependency 'bundler', '~> 1.16'
25
22
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oas_contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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-06 00:00:00.000000000 Z
11
+ date: 2018-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -66,7 +66,7 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
- description: Open API Specification related commands and libraries.
69
+ description: Libraries and Commands for Open API (2.0, 3.0) Specification.
70
70
  email:
71
71
  - shimizu.michinao@gmail.com
72
72
  executables:
@@ -75,6 +75,7 @@ extensions: []
75
75
  extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
+ - ".rspec_status"
78
79
  - CODE_OF_CONDUCT.md
79
80
  - Gemfile
80
81
  - LICENSE.txt
@@ -82,37 +83,38 @@ files:
82
83
  - Rakefile
83
84
  - bin/console
84
85
  - bin/setup
85
- - example/dist/openapi_v3/meta/001_openapi.yml
86
- - example/dist/openapi_v3/meta/002_info.yml
87
- - example/dist/openapi_v3/meta/003_servers.yml
88
- - example/dist/openapi_v3/model/001_Pet.yml
89
- - example/dist/openapi_v3/model/002_Error.yml
90
- - example/dist/openapi_v3/model/003_Pets.yml
91
- - example/dist/openapi_v3/path/001_pets.yml
92
- - example/dist/openapi_v3/path/002_pets_{petId}.yml
93
- - example/dist/swagger_v2/meta/001_swagger.yml
94
- - example/dist/swagger_v2/meta/002_info.yml
95
- - example/dist/swagger_v2/meta/003_host.yml
96
- - example/dist/swagger_v2/meta/004_basePath.yml
97
- - example/dist/swagger_v2/meta/005_schemes.yml
98
- - example/dist/swagger_v2/meta/006_consumes.yml
99
- - example/dist/swagger_v2/meta/007_produces.yml
100
- - example/dist/swagger_v2/meta/008_components.yml
101
- - example/dist/swagger_v2/model/001_Pet.yml
102
- - example/dist/swagger_v2/model/002_Error.yml
103
- - example/dist/swagger_v2/model/003_Pets.yml
104
- - example/dist/swagger_v2/path/001_pets.yml
105
- - example/dist/swagger_v2/path/002_pets_{petId}.yml
106
- - example/sample_petstore_openapi_v3.yml
107
- - example/sample_petstore_swagger_v2.yml
86
+ - example/dist/v2/meta/001_swagger.yml
87
+ - example/dist/v2/meta/002_info.yml
88
+ - example/dist/v2/meta/003_host.yml
89
+ - example/dist/v2/meta/004_basePath.yml
90
+ - example/dist/v2/meta/005_schemes.yml
91
+ - example/dist/v2/meta/006_consumes.yml
92
+ - example/dist/v2/meta/007_produces.yml
93
+ - example/dist/v2/meta/008_components.yml
94
+ - example/dist/v2/model/001_Pet.yml
95
+ - example/dist/v2/model/002_Error.yml
96
+ - example/dist/v2/model/003_Pets.yml
97
+ - example/dist/v2/path/001_pets.yml
98
+ - example/dist/v2/path/002_pets_{petId}.yml
99
+ - example/dist/v3/meta/001_openapi.yml
100
+ - example/dist/v3/meta/002_info.yml
101
+ - example/dist/v3/meta/003_servers.yml
102
+ - example/dist/v3/model/001_Pet.yml
103
+ - example/dist/v3/model/002_Error.yml
104
+ - example/dist/v3/model/003_Pets.yml
105
+ - example/dist/v3/path/001_pets.yml
106
+ - example/dist/v3/path/002_pets_{petId}.yml
107
+ - example/v2.yml
108
+ - example/v3.yml
108
109
  - exe/oas_contrib
109
110
  - lib/oas_contrib.rb
110
111
  - lib/oas_contrib/command.rb
111
- - lib/oas_contrib/command_resolver_base.rb
112
- - lib/oas_contrib/command_resolvers/divide_command_resolver.rb
113
- - lib/oas_contrib/command_resolvers/merge_command_resolver.rb
112
+ - lib/oas_contrib/openapi/v2/spec.rb
114
113
  - lib/oas_contrib/openapi/v3/spec.rb
115
- - lib/oas_contrib/swagger/v2/spec.rb
114
+ - lib/oas_contrib/resolver/base.rb
115
+ - lib/oas_contrib/resolver/divide.rb
116
+ - lib/oas_contrib/resolver/merge.rb
117
+ - lib/oas_contrib/resolver/preview.rb
116
118
  - lib/oas_contrib/version.rb
117
119
  - oas_contrib.gemspec
118
120
  homepage: https://github.com/MichinaoShimizu/oas_contrib
@@ -138,5 +140,5 @@ rubyforge_project:
138
140
  rubygems_version: 2.7.6
139
141
  signing_key:
140
142
  specification_version: 4
141
- summary: Open API Specification related commands and libraries.
143
+ summary: Libraries and Commands for Open API (2.0, 3.0) Specification.
142
144
  test_files: []
@@ -1,140 +0,0 @@
1
- require 'oas_contrib/swagger/v2/spec'
2
- require 'oas_contrib/openapi/v3/spec'
3
- require 'yaml'
4
- require 'json'
5
-
6
- module OasContrib
7
- # CommandResolver Basic
8
- class CommandResolverBase
9
- # Initialze
10
- # @param [String] path input or output directory path
11
- def initialize(path)
12
- @meta_dir = path + '/meta'
13
- @path_dir = path + '/path'
14
- @model_dir = path + '/model'
15
- end
16
-
17
- # Load input files or directories
18
- # @raise [NotImplementedError]
19
- # @return [nil]
20
- def load
21
- raise NotImplementedError, 'This class must be implemented "load" method.'
22
- end
23
-
24
- # Distribute output file
25
- # @raise [NotImplementedError]
26
- # @return [nil]
27
- def dist
28
- raise NotImplementedError, 'This class must be implemented "dist" method.'
29
- end
30
-
31
- # Determine which type of definition data.
32
- # @raise [StandardError]
33
- # @return [OasContrib::Swagger::V2::Spec|OasContrib::OpenAPI::V3::Spec] spec
34
- def resolve
35
- return @spec = OasContrib::Swagger::V2::Spec.new(@load_data) if @load_data['swagger'] =~ /^2/
36
- return @spec = OasContrib::OpenAPI::V3::Spec.new(@load_data) if @load_data['openapi'] =~ /^3/
37
- raise 'Undefined OAS file.'
38
- end
39
-
40
- # Check the type of OpenAPI v3 definition or not
41
- # @return [Boolean]
42
- def openapi_v3?
43
- @spec.is_a?(OasContrib::OpenAPI::V3::Spec)
44
- end
45
-
46
- # Check the type of Swagger v2 definition or not
47
- # @return [Boolean]
48
- def swagger_v2?
49
- @spec.is_a?(OasContrib::Swagger::V2::Spec)
50
- end
51
-
52
- # Convert file type string to file extention string.
53
- # @param [String] type file type string (yaml or json)
54
- # @raise [ArgumentError] invalid file type string
55
- # @return [String] file extension string (.yml or .json)
56
- def file_type_to_ext(type)
57
- case type
58
- when 'yaml' then '.yml'
59
- when 'json' then '.json'
60
- else raise ArgumentError, 'Undefined file type'
61
- end
62
- end
63
-
64
- # Load a file
65
- # @param [String] path input file path
66
- # @return [Hash]
67
- def input(path)
68
- @load_data = input_call(path)
69
- end
70
-
71
- # Output a file
72
- # @param [Hash] hash data
73
- # @param [String] path output file path
74
- # @return [File]
75
- def output(hash, path)
76
- File.open(path, 'w') { |f| output_call(hash, f) }
77
- end
78
-
79
- # Load directory files
80
- # @param [String] path input directory
81
- # @return [Hash] merged input files data
82
- def input_dir(path)
83
- Dir.glob(path).sort.each_with_object({}, &input_call_lambda)
84
- end
85
-
86
- # Proc of input a yaml or json file
87
- # @return [Proc]
88
- def input_call_lambda
89
- lambda do |file, result|
90
- hash = input_call(file)
91
- key = hash.keys[0]
92
- result[key] = hash[key]
93
- end
94
- end
95
-
96
- # Load a file depending on file extension
97
- # @param [String] path file path
98
- # @raise [ArgumentError] invalid file type string
99
- # @return [Hash]
100
- def input_call(path)
101
- puts "Load: #{path}"
102
- case @input_file_ext
103
- when '.yml' then YAML.load_file(path)
104
- when '.json' then JSON.parse(File.read(path))
105
- else raise ArgumentError, 'Undefined file type'
106
- end
107
- end
108
-
109
- # Output a file depending on file extension
110
- # @param [String] path file path
111
- # @raise [ArgumentError] invalid file type string
112
- # @return [IO]
113
- def output_call(hash, file)
114
- puts "Dist: #{file.path}"
115
- case @output_file_ext
116
- when '.yml' then YAML.dump(hash, file)
117
- when '.json' then JSON.dump(hash, file)
118
- else raise ArgumentError, 'Undefined file type'
119
- end
120
- end
121
-
122
- # Output directory and files
123
- # @param [Hash] hash data
124
- # @param [String] path directory path
125
- # @return [nil]
126
- def output_dir(hash, path)
127
- puts "Dist: #{path}"
128
- FileUtils.mkdir_p(path)
129
-
130
- i = 1
131
- hash.each do |k, _v|
132
- key = k.tr('/', '_').gsub(/^_/, '')
133
- val = hash.select { |hash_key, _| hash_key == k }
134
- output(val, "#{path}/#{i.to_s.rjust(3, '0')}_#{key}#{@output_file_ext}")
135
- i += 1
136
- end
137
- nil
138
- end
139
- end
140
- end