zero-rails_openapi 1.0.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +245 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/examples/examples_controller.rb +36 -0
- data/lib/examples/open_api.rb +87 -0
- data/lib/oas_objs/helpers.rb +41 -0
- data/lib/oas_objs/media_type_obj.rb +86 -0
- data/lib/oas_objs/param_obj.rb +83 -0
- data/lib/oas_objs/ref_obj.rb +29 -0
- data/lib/oas_objs/request_body_obj.rb +54 -0
- data/lib/oas_objs/response_obj.rb +44 -0
- data/lib/oas_objs/schema_obj.rb +187 -0
- data/lib/open_api.rb +9 -0
- data/lib/open_api/config.rb +34 -0
- data/lib/open_api/dsl.rb +58 -0
- data/lib/open_api/dsl_inside_block.rb +175 -0
- data/lib/open_api/generator.rb +75 -0
- data/lib/open_api/version.rb +3 -0
- data/lib/takes/open_api.rake +6 -0
- data/zero-rails_openapi.gemspec +37 -0
- metadata +116 -0
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'active_support/hash_with_indifferent_access'
|
2
|
+
|
3
|
+
module OpenApi
|
4
|
+
module Generator
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def generate_docs(api_name = nil)
|
11
|
+
Rails.application.eager_load!
|
12
|
+
if api_name.present?
|
13
|
+
{ api_name => generate_doc(api_name) }
|
14
|
+
else
|
15
|
+
OpenApi.apis.keys.map { |api_key| { api_key => generate_doc(api_key)} }.reduce({ }, :merge)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def generate_doc(api_name)
|
20
|
+
settings = OpenApi.apis[api_name]
|
21
|
+
doc = { openapi: '3.0.0' }.merge(settings.slice :info, :servers).merge({
|
22
|
+
security: settings[:global_security], tags: [ ], paths: { },
|
23
|
+
components: {
|
24
|
+
securitySchemes: settings[:global_security_schemes],
|
25
|
+
schemas: { }
|
26
|
+
}
|
27
|
+
})
|
28
|
+
|
29
|
+
settings[:root_controller].descendants.each do |ctrl|
|
30
|
+
doc[:paths].merge! ctrl.instance_variable_get('@_api_infos')
|
31
|
+
doc[:tags] << ctrl.instance_variable_get('@_ctrl_infos')[:tag]
|
32
|
+
doc[:components].merge! ctrl.instance_variable_get('@_ctrl_infos')[:components]
|
33
|
+
end
|
34
|
+
doc[:components].delete_if { |_,v| v.blank? }
|
35
|
+
($open_apis ||= { })[api_name] ||= HashWithIndifferentAccess.new doc.delete_if { |_,v| v.blank? }
|
36
|
+
end
|
37
|
+
|
38
|
+
def write_docs
|
39
|
+
docs = generate_docs
|
40
|
+
# puts docs
|
41
|
+
output_path = OpenApi.config.file_output_path
|
42
|
+
FileUtils.mkdir_p output_path
|
43
|
+
docs.each do |api_name, doc|
|
44
|
+
File.open("#{output_path}/#{api_name}.json", 'w') { |file| file.write JSON.pretty_generate doc }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.generate_routes_list
|
50
|
+
# ref https://github.com/rails/rails/blob/master/railties/lib/rails/tasks/routes.rake
|
51
|
+
require './config/routes'
|
52
|
+
all_routes = Rails.application.routes.routes
|
53
|
+
require 'action_dispatch/routing/inspector'
|
54
|
+
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
|
55
|
+
|
56
|
+
inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, nil).split("\n").drop(1).map do |line|
|
57
|
+
infos = line.match(/[A-Z].*/).to_s.split(' ') # => [GET, /api/v1/examples/:id, api/v1/examples#index]
|
58
|
+
{
|
59
|
+
http_verb: infos[0].downcase, # => "get"
|
60
|
+
path: infos[1][0..-11].split('/').map do |item|
|
61
|
+
item.match?(/:/) ? "{#{item[1..-1]}}" : item
|
62
|
+
end.join('/'), # => "/api/v1/examples/{id}"
|
63
|
+
action_path: infos[2] # => "api/v1/examples#index"
|
64
|
+
}
|
65
|
+
end.group_by {|api| api[:action_path].split('#').first } # => { "api/v1/examples" => [..] }, group by paths
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.get_actions_by_ctrl_path(path)
|
69
|
+
@routes_list ||= generate_routes_list
|
70
|
+
@routes_list[path].map do |action_info|
|
71
|
+
action_info[:action_path].split('#').last
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "open_api/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zero-rails_openapi"
|
8
|
+
spec.version = OpenApi::VERSION
|
9
|
+
spec.authors = ["zhandao"]
|
10
|
+
spec.email = ["x@skippingcat.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Generate the OpenAPI Specification JSON file for Rails application.}
|
13
|
+
spec.description = %q{Provide concise DSL for you to generate the OpenAPI Specification 3 (Swagger 3)
|
14
|
+
JSON file for Rails application, then you can use Swagger-UI 3.2.0+ to show the documentation.}
|
15
|
+
spec.homepage = "https://github.com/zhandao/zero-rails_openapi"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
# if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
+
# else
|
23
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
# "public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16.a"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zero-rails_openapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- zhandao
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.16.a
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.16.a
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |-
|
56
|
+
Provide concise DSL for you to generate the OpenAPI Specification 3 (Swagger 3)
|
57
|
+
JSON file for Rails application, then you can use Swagger-UI 3.2.0+ to show the documentation.
|
58
|
+
email:
|
59
|
+
- x@skippingcat.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- ".travis.yml"
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- bin/console
|
74
|
+
- bin/setup
|
75
|
+
- lib/examples/examples_controller.rb
|
76
|
+
- lib/examples/open_api.rb
|
77
|
+
- lib/oas_objs/helpers.rb
|
78
|
+
- lib/oas_objs/media_type_obj.rb
|
79
|
+
- lib/oas_objs/param_obj.rb
|
80
|
+
- lib/oas_objs/ref_obj.rb
|
81
|
+
- lib/oas_objs/request_body_obj.rb
|
82
|
+
- lib/oas_objs/response_obj.rb
|
83
|
+
- lib/oas_objs/schema_obj.rb
|
84
|
+
- lib/open_api.rb
|
85
|
+
- lib/open_api/config.rb
|
86
|
+
- lib/open_api/dsl.rb
|
87
|
+
- lib/open_api/dsl_inside_block.rb
|
88
|
+
- lib/open_api/generator.rb
|
89
|
+
- lib/open_api/version.rb
|
90
|
+
- lib/takes/open_api.rake
|
91
|
+
- zero-rails_openapi.gemspec
|
92
|
+
homepage: https://github.com/zhandao/zero-rails_openapi
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.6.12
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Generate the OpenAPI Specification JSON file for Rails application.
|
116
|
+
test_files: []
|