oas_divider 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: daefa07bc1b005749456c534967daf3c586886c32f6976cae06cd1da46553541
4
+ data.tar.gz: 2f815a695d9e5198f896197892c347b1209886856e2c57d16f1c0a6efd94373e
5
+ SHA512:
6
+ metadata.gz: 55b8728fa5947dd49e518d3278dc3cadca05ee754cf6053481a90498cffc3d04b33984fa32d0b9be7eec54aee13dc0faa4288b02003781172d62b28442b2b1c9
7
+ data.tar.gz: 6d834f629c96d5b416e9b76a3b892068c249745cac53a6c3503a67c743e68d77049620e3a78de27ae26fbc7255ad22425110be25f88bc45268f95f3be80791af
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hikalin8686@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in oas_divider.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 lighty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # OasDivider
2
+
3
+ Divides the OpenAPI format schema definition file described in one file into multiple files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'oas_divider'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install oas_divider
20
+
21
+ ## Usage
22
+
23
+ ```shell
24
+ bundle exec exe/oas_divider
25
+ ```
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/oas_divider. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/oas_divider/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the OasDivider project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/oas_divider/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "oas_divider"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/oas_divider ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'oas_divider'
4
+
5
+ OasDivider::Cli.new('swagger_original.yml').divide
@@ -0,0 +1,69 @@
1
+ require "oas_divider/version"
2
+ require 'yaml'
3
+ require 'fileutils'
4
+
5
+ require 'oas_divider/components_object_field_object'
6
+ require 'oas_divider/paths_object'
7
+ require 'oas_divider/relative_document_referencer'
8
+ require 'oas_divider/schema_object'
9
+
10
+ module OasDivider
11
+ class Cli
12
+
13
+ def initialize(file_name)
14
+ @file_name = file_name
15
+ end
16
+
17
+ def divide
18
+ load_file_as_open_api_object
19
+ save_divided_files
20
+ save_open_api_object_as_root_file
21
+ end
22
+
23
+ private
24
+
25
+ def save_divided_files
26
+ open_api_object.keys.each do |open_api_object_field|
27
+ if open_api_object_field === "paths"
28
+ FileUtils.mkdir_p('paths')
29
+ paths_objects = open_api_object[open_api_object_field]
30
+ paths_objects.keys.each do |path|
31
+ path_object = PathsObject.new(path, paths_objects[path]) # 一つのpathオブジェクト。postなどがキー
32
+ path_object.to_file
33
+ open_api_object[open_api_object_field][path] = { "$ref" => path_object.ref }
34
+ end
35
+ end
36
+
37
+ if open_api_object_field === "components"
38
+ FileUtils.mkdir_p('components')
39
+ components_objects = open_api_object[open_api_object_field]
40
+ components_objects.keys.each do |components_object_field|
41
+ if components_object_field === 'schemas'
42
+ FileUtils.mkdir_p('components/schemas')
43
+ schmas = components_objects[components_object_field]
44
+ schmas.keys.each do |schema_name|
45
+ schema_object = SchemaObject.new(schema_name, schmas[schema_name])
46
+ schema_object.to_file
47
+ open_api_object[open_api_object_field][components_object_field][schema_name] = { "$ref" => schema_object.ref }
48
+ end
49
+ else
50
+ field_object = ComponentsObjectFieldObject.new(components_object_field, components_objects[components_object_field])
51
+ field_object.to_file
52
+ open_api_object[open_api_object_field][components_object_field] = { "$ref" => field_object.ref }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def load_file_as_open_api_object
60
+ self.open_api_object = YAML.load_file(file_name)
61
+ end
62
+
63
+ def save_open_api_object_as_root_file
64
+ YAML.dump(open_api_object, File.open('swagger_root.yml', 'w'))
65
+ end
66
+
67
+ attr_accessor :file_name, :open_api_object
68
+ end
69
+ end
@@ -0,0 +1,31 @@
1
+ module OasDivider
2
+ class ComponentsObjectFieldObject
3
+ attr_accessor :field_name, :field_object
4
+
5
+ def initialize(field_name, field_object)
6
+ @field_name = field_name
7
+ @field_object = field_object
8
+ end
9
+
10
+ def to_file
11
+ convert_ref
12
+ YAML.dump(field_object, File.open( File.join(directory, file_name) , 'w') )
13
+ end
14
+
15
+ def directory
16
+ 'components'
17
+ end
18
+
19
+ def file_name
20
+ "#{field_name}.yml"
21
+ end
22
+
23
+ def ref
24
+ File.join(directory, file_name)
25
+ end
26
+
27
+ def convert_ref
28
+ RelativeDocumentReferencer.execute(field_object, 1)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ module OasDivider
2
+ class PathsObject
3
+ attr_accessor :path, :path_item_objects
4
+
5
+ def initialize(path, path_item_objects)
6
+ @path = path
7
+ @path_item_objects = path_item_objects
8
+ end
9
+
10
+ def to_file
11
+ make_directory
12
+ convert_ref
13
+ YAML.dump(path_item_objects, File.open( File.join(directory, file_name) , 'w') )
14
+ end
15
+
16
+ def make_directory
17
+ FileUtils.mkdir_p( directory )
18
+ rescue => e
19
+ puts "path: #{@path}"
20
+ throw e
21
+ end
22
+
23
+ def interlevel_directory
24
+ @path.split('/').length > 2 ? @path.split('/')[1..-2].map {|dir| dir.gsub(/[{}]/,"")} : ''
25
+ end
26
+
27
+ def directory
28
+ File.join( 'paths' , interlevel_directory)
29
+ end
30
+
31
+ def file_name
32
+ "#{@path.split('/').pop.gsub(/[{}]/,"")}.yml"
33
+ end
34
+
35
+ def ref
36
+ File.join(directory, file_name)
37
+ end
38
+
39
+ def convert_ref
40
+ RelativeDocumentReferencer.execute(path_item_objects, 1 + interlevel_directory.size)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,47 @@
1
+ module OasDivider
2
+ class RelativeDocumentReferencer
3
+ def self.execute(root, depth)
4
+ new(root, depth).execute
5
+ end
6
+
7
+ def initialize(root, depth)
8
+ @root = root
9
+ @depth = depth
10
+ end
11
+
12
+ def execute
13
+ lookup(root)
14
+ root
15
+ end
16
+
17
+ # hashで、keyが$refならconvert
18
+ # hashかarrayならlookup
19
+ # そうでないならなにもしない
20
+ def lookup(object)
21
+ if object.is_a? Array
22
+ object.each { |value| lookup(value) if value.is_a?(Hash) || value.is_a?(Array) }
23
+ else
24
+ object.each do |key, value|
25
+ set_converted_value(object, key, value) if key === "$ref"
26
+ lookup(value) if value.is_a?(Hash) || value.is_a?(Array)
27
+ end
28
+ end
29
+ end
30
+
31
+ def set_converted_value(object, key, value)
32
+ object[key] = convert(value)
33
+ end
34
+
35
+ def convert(ref)
36
+ # componentから取得することしか考えない
37
+ if ref.split('/')[2] === 'schemas'
38
+ "../" * depth + "#{ref.split('/')[1..3].join('/')}.yml"
39
+ else
40
+ "../" * depth + "#{ref.split('/')[1..2].join('/')}.yml#/#{ref.split('/')[3]}"
41
+ end
42
+ end
43
+
44
+ private
45
+ attr :root, :depth
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ module OasDivider
2
+ class SchemaObject
3
+ attr_accessor :schema_name, :schema_object
4
+
5
+ def initialize(schema_name, schema_object)
6
+ @schema_name = schema_name
7
+ @schema_object = schema_object
8
+ end
9
+
10
+ def to_file
11
+ convert_ref
12
+ YAML.dump(schema_object, File.open( File.join(directory, file_name) , 'w') )
13
+ end
14
+
15
+ def directory
16
+ 'components/schemas'
17
+ end
18
+
19
+ def file_name
20
+ "#{schema_name}.yml"
21
+ end
22
+
23
+ def ref
24
+ File.join(directory, file_name)
25
+ end
26
+
27
+ def convert_ref
28
+ RelativeDocumentReferencer.execute(schema_object, 2)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module OasDivider
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/oas_divider/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "oas_divider"
5
+ spec.version = OasDivider::VERSION
6
+ spec.authors = ["lighty"]
7
+ spec.email = ["hikalin8686@gmail.com"]
8
+
9
+ spec.summary = %q{Divides the OpenAPI format schema definition file described in one file into multiple files.}
10
+ spec.description = %q{Divides the OpenAPI format schema definition file described in one file into multiple files.}
11
+ spec.homepage = "https://twitter.com/hikalin8686"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/lighty/oas_divider"
17
+ spec.metadata["changelog_uri"] = "https://github.com/lighty/oas_divider"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oas_divider
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - lighty
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Divides the OpenAPI format schema definition file described in one file
14
+ into multiple files.
15
+ email:
16
+ - hikalin8686@gmail.com
17
+ executables:
18
+ - oas_divider
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - ".travis.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - exe/oas_divider
33
+ - lib/oas_divider.rb
34
+ - lib/oas_divider/components_object_field_object.rb
35
+ - lib/oas_divider/paths_object.rb
36
+ - lib/oas_divider/relative_document_referencer.rb
37
+ - lib/oas_divider/schema_object.rb
38
+ - lib/oas_divider/version.rb
39
+ - oas_divider.gemspec
40
+ homepage: https://twitter.com/hikalin8686
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://twitter.com/hikalin8686
45
+ source_code_uri: https://github.com/lighty/oas_divider
46
+ changelog_uri: https://github.com/lighty/oas_divider
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.0.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Divides the OpenAPI format schema definition file described in one file into
66
+ multiple files.
67
+ test_files: []