open_api-rswag-ui 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +27 -0
  4. data/lib/generators/rswag/ui/custom/USAGE +8 -0
  5. data/lib/generators/rswag/ui/custom/custom_generator.rb +13 -0
  6. data/lib/generators/rswag/ui/install/USAGE +8 -0
  7. data/lib/generators/rswag/ui/install/install_generator.rb +18 -0
  8. data/lib/generators/rswag/ui/install/templates/rswag-ui.rb +10 -0
  9. data/lib/open_api/rswag/ui.rb +16 -0
  10. data/lib/open_api/rswag/ui/configuration.rb +37 -0
  11. data/lib/open_api/rswag/ui/engine.rb +19 -0
  12. data/lib/open_api/rswag/ui/index.erb +103 -0
  13. data/lib/open_api/rswag/ui/middleware.rb +46 -0
  14. data/lib/tasks/rswag-ui_tasks.rake +12 -0
  15. data/node_modules/swagger-ui-dist/README.md +22 -0
  16. data/node_modules/swagger-ui-dist/absolute-path.js +14 -0
  17. data/node_modules/swagger-ui-dist/favicon-16x16.png +0 -0
  18. data/node_modules/swagger-ui-dist/favicon-32x32.png +0 -0
  19. data/node_modules/swagger-ui-dist/index.html +60 -0
  20. data/node_modules/swagger-ui-dist/index.js +17 -0
  21. data/node_modules/swagger-ui-dist/oauth2-redirect.html +67 -0
  22. data/node_modules/swagger-ui-dist/package.json +74 -0
  23. data/node_modules/swagger-ui-dist/swagger-ui-bundle.js +110 -0
  24. data/node_modules/swagger-ui-dist/swagger-ui-bundle.js.map +1 -0
  25. data/node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js +14 -0
  26. data/node_modules/swagger-ui-dist/swagger-ui-standalone-preset.js.map +1 -0
  27. data/node_modules/swagger-ui-dist/swagger-ui.css +3 -0
  28. data/node_modules/swagger-ui-dist/swagger-ui.css.map +1 -0
  29. data/node_modules/swagger-ui-dist/swagger-ui.js +9 -0
  30. data/node_modules/swagger-ui-dist/swagger-ui.js.map +1 -0
  31. metadata +115 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0efd86c8bb66b3d302f7d5a7e3a179971116421d128127f24370cd00ee9d8ef
4
+ data.tar.gz: 18c6711947bd3acd2488449380befef5f1e8fc1e5f6b899737528a1d17d9d588
5
+ SHA512:
6
+ metadata.gz: d822407d7123659014d046e0b63617dca9da9852d95dfd6fc8989239605c0451ff107c7898e244af95a95831dcbabb75ac6fd2615dacde5b82b7c695ea90a32b
7
+ data.tar.gz: 125c0a903b2a0af879eb598c1248d123d7c13283bd502f1937e57ada67536dfa26d4947d74c111779b17f464e751c4506a2e2f65794d2633f493c18cd3788a73
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 domaindrivendev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'rswag-specs'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Adds a local version of index.html.erb for customizing the swagger-ui
3
+
4
+ Example:
5
+ rails generate rswag:ui:custom
6
+
7
+ This will create:
8
+ app/views/rswag/ui/home/index.html.erb
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module Rswag
4
+ module Ui
5
+ class CustomGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('../../../../../../lib/open_api/rswag/ui', __FILE__)
7
+
8
+ def add_custom_index
9
+ copy_file('index.erb', 'app/views/rswag/ui/home/index.html.erb')
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Adds rswag-api initializer for configuration
3
+
4
+ Example:
5
+ rails generate rswag:api:install
6
+
7
+ This will create:
8
+ config/initializers/rswag-api.rb
@@ -0,0 +1,18 @@
1
+ require 'rails/generators'
2
+
3
+ module Rswag
4
+ module Ui
5
+
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def add_initializer
10
+ template('rswag-ui.rb', 'config/initializers/rswag-ui.rb')
11
+ end
12
+
13
+ def add_routes
14
+ route("mount OpenApi::Rswag::Ui::Engine => '/api-docs'")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ OpenApi::Rswag::Ui.configure do |c|
2
+
3
+ # List the Swagger endpoints that you want to be documented through the swagger-ui
4
+ # The first parameter is the path (absolute or relative to the UI host) to the corresponding
5
+ # JSON endpoint and the second is a title that will be displayed in the document selector
6
+ # NOTE: If you're using rspec-api to expose Swagger files (under swagger_root) as JSON endpoints,
7
+ # then the list below should correspond to the relative paths for those endpoints
8
+
9
+ c.swagger_endpoint '/api-docs/v1/swagger.json', 'API V1 Docs'
10
+ end
@@ -0,0 +1,16 @@
1
+ require 'open_api/rswag/ui/configuration'
2
+ require 'open_api/rswag/ui/engine'
3
+
4
+ module OpenApi
5
+ module Rswag
6
+ module Ui
7
+ def self.configure
8
+ yield(config)
9
+ end
10
+
11
+ def self.config
12
+ @config ||= Configuration.new
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ require 'ostruct'
2
+
3
+ module OpenApi
4
+ module Rswag
5
+ module Ui
6
+ class Configuration
7
+ attr_reader :template_locations
8
+ attr_accessor :config_object
9
+ attr_accessor :oauth_config_object
10
+ attr_reader :assets_root
11
+
12
+ def initialize
13
+ @template_locations = [
14
+ # preffered override location
15
+ "#{Rack::Directory.new('').root}/swagger/index.erb",
16
+ # backwards compatible override location
17
+ "#{Rack::Directory.new('').root}/app/views/rswag/ui/home/index.html.erb",
18
+ # default location
19
+ File.expand_path('../index.erb', __FILE__)
20
+ ]
21
+ @assets_root = File.expand_path('../../../../../node_modules/swagger-ui-dist', __FILE__)
22
+ @config_object = {}
23
+ @oauth_config_object = {}
24
+ end
25
+
26
+ def swagger_endpoint(url, name)
27
+ @config_object[:urls] ||= []
28
+ @config_object[:urls] << { url: url, name: name }
29
+ end
30
+
31
+ def get_binding
32
+ binding
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require 'open_api/rswag/ui/middleware'
2
+
3
+ module OpenApi
4
+ module Rswag
5
+ module Ui
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace OpenApi::Rswag::Ui
8
+
9
+ initializer 'rswag-ui.initialize' do |app|
10
+ middleware.use OpenApi::Rswag::Ui::Middleware, OpenApi::Rswag::Ui.config
11
+ end
12
+
13
+ rake_tasks do
14
+ load File.expand_path('../../../../tasks/rswag-ui_tasks.rake', __FILE__)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,103 @@
1
+ <!-- HTML for static distribution bundle build -->
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <title>Swagger UI</title>
7
+ <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Source+Code+Pro:300,600|Titillium+Web:400,600,700" rel="stylesheet">
8
+ <link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
9
+ <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
10
+ <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
11
+ <style>
12
+ html
13
+ {
14
+ box-sizing: border-box;
15
+ overflow: -moz-scrollbars-vertical;
16
+ overflow-y: scroll;
17
+ }
18
+ *,
19
+ *:before,
20
+ *:after
21
+ {
22
+ box-sizing: inherit;
23
+ }
24
+
25
+ body {
26
+ margin:0;
27
+ background: #fafafa;
28
+ }
29
+ </style>
30
+ </head>
31
+
32
+ <body>
33
+
34
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
35
+ <defs>
36
+ <symbol viewBox="0 0 20 20" id="unlocked">
37
+ <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
38
+ </symbol>
39
+
40
+ <symbol viewBox="0 0 20 20" id="locked">
41
+ <path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8zM12 8H8V5.199C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8z"/>
42
+ </symbol>
43
+
44
+ <symbol viewBox="0 0 20 20" id="close">
45
+ <path d="M14.348 14.849c-.469.469-1.229.469-1.697 0L10 11.819l-2.651 3.029c-.469.469-1.229.469-1.697 0-.469-.469-.469-1.229 0-1.697l2.758-3.15-2.759-3.152c-.469-.469-.469-1.228 0-1.697.469-.469 1.228-.469 1.697 0L10 8.183l2.651-3.031c.469-.469 1.228-.469 1.697 0 .469.469.469 1.229 0 1.697l-2.758 3.152 2.758 3.15c.469.469.469 1.229 0 1.698z"/>
46
+ </symbol>
47
+
48
+ <symbol viewBox="0 0 20 20" id="large-arrow">
49
+ <path d="M13.25 10L6.109 2.58c-.268-.27-.268-.707 0-.979.268-.27.701-.27.969 0l7.83 7.908c.268.271.268.709 0 .979l-7.83 7.908c-.268.271-.701.27-.969 0-.268-.269-.268-.707 0-.979L13.25 10z"/>
50
+ </symbol>
51
+
52
+ <symbol viewBox="0 0 20 20" id="large-arrow-down">
53
+ <path d="M17.418 6.109c.272-.268.709-.268.979 0s.271.701 0 .969l-7.908 7.83c-.27.268-.707.268-.979 0l-7.908-7.83c-.27-.268-.27-.701 0-.969.271-.268.709-.268.979 0L10 13.25l7.418-7.141z"/>
54
+ </symbol>
55
+
56
+
57
+ <symbol viewBox="0 0 24 24" id="jump-to">
58
+ <path d="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z"/>
59
+ </symbol>
60
+
61
+ <symbol viewBox="0 0 24 24" id="expand">
62
+ <path d="M10 18h4v-2h-4v2zM3 6v2h18V6H3zm3 7h12v-2H6v2z"/>
63
+ </symbol>
64
+
65
+ </defs>
66
+ </svg>
67
+
68
+ <div id="swagger-ui"></div>
69
+
70
+ <!-- Workaround for https://github.com/swagger-api/swagger-editor/issues/1371 -->
71
+ <script>
72
+ if (window.navigator.userAgent.indexOf("Edge") > -1) {
73
+ console.log("Removing native Edge fetch in favor of swagger-ui's polyfill")
74
+ window.fetch = undefined;
75
+ }
76
+ </script>
77
+
78
+ <script src="./swagger-ui-bundle.js"> </script>
79
+ <script src="./swagger-ui-standalone-preset.js"> </script>
80
+ <script>
81
+ window.onload = function () {
82
+ var configObject = JSON.parse('<%= config_object.to_json %>');
83
+ var oauthConfigObject = JSON.parse('<%= oauth_config_object.to_json %>');
84
+
85
+ // Apply mandatory parameters
86
+ configObject.dom_id = "#swagger-ui";
87
+ configObject.presets = [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset];
88
+ configObject.layout = "StandaloneLayout";
89
+
90
+ // If oauth2RedirectUrl isn't specified, use the built-in default
91
+ if (!configObject.hasOwnProperty("oauth2RedirectUrl"))
92
+ configObject.oauth2RedirectUrl = window.location.href.replace("index.html", "oauth2-redirect.html");
93
+
94
+ // Build a system
95
+ const ui = SwaggerUIBundle(configObject);
96
+
97
+ // Apply OAuth config
98
+ ui.initOAuth(oauthConfigObject);
99
+ }
100
+ </script>
101
+ </body>
102
+
103
+ </html>
@@ -0,0 +1,46 @@
1
+ module OpenApi
2
+ module Rswag
3
+ module Ui
4
+ class Middleware < Rack::Static
5
+
6
+ def initialize(app, config)
7
+ @config = config
8
+ super(app, urls: [ '' ], root: config.assets_root )
9
+ end
10
+
11
+ def call(env)
12
+ if base_path?(env)
13
+ redirect_uri = env['SCRIPT_NAME'].chomp('/') + '/index.html'
14
+ return [ 301, { 'Location' => redirect_uri }, [ ] ]
15
+ end
16
+
17
+ if index_path?(env)
18
+ return [ 200, { 'Content-Type' => 'text/html' }, [ render_template ] ]
19
+ end
20
+
21
+ super
22
+ end
23
+
24
+ private
25
+
26
+ def base_path?(env)
27
+ env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/"
28
+ end
29
+
30
+ def index_path?(env)
31
+ env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/index.html"
32
+ end
33
+
34
+ def render_template
35
+ file = File.new(template_filename)
36
+ template = ERB.new(file.read)
37
+ template.result(@config.get_binding)
38
+ end
39
+
40
+ def template_filename
41
+ @config.template_locations.find { |filename| File.exists?(filename) }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ namespace :rswag do
2
+ namespace :ui do
3
+
4
+ desc 'TODO'
5
+ task :copy_assets, [ :dest ] do |t, args|
6
+ dest = args[:dest]
7
+ FileUtils.rm_r(dest, force: true)
8
+ FileUtils.mkdir_p(dest)
9
+ FileUtils.cp_r(Dir.glob("#{OpenApi::Rswag::Ui.config.assets_root}/{*.js,*.png,*.css}"), dest)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ # Swagger UI Dist
2
+ [![NPM version](https://badge.fury.io/js/swagger-ui-dist.svg)](http://badge.fury.io/js/swagger-ui-dist)
3
+
4
+ # API
5
+
6
+ This module, `swagger-ui-dist`, exposes Swagger-UI's entire dist folder as a dependency-free npm module.
7
+ Use `swagger-ui` instead, if you'd like to have npm install dependencies for you.
8
+
9
+ `SwaggerUIBundle` and `SwaggerUIStandalonePreset` can be imported:
10
+ ```javascript
11
+ import { SwaggerUIBundle, SwaggerUIStandalonePreset } from "swagger-ui-dist"
12
+ ```
13
+
14
+ To get an absolute path to this directory for static file serving, use the exported `getAbsoluteFSPath` method:
15
+
16
+ ```javascript
17
+ const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath()
18
+
19
+ // then instantiate server that serves files from the swaggerUiAssetPath
20
+ ```
21
+
22
+ For anything else, check the [Swagger-UI](https://github.com/swagger-api/swagger-ui) repository.
@@ -0,0 +1,14 @@
1
+ /*
2
+ * getAbsoluteFSPath
3
+ * @return {string} When run in NodeJS env, returns the absolute path to the current directory
4
+ * When run outside of NodeJS, will return an error message
5
+ */
6
+ const getAbsoluteFSPath = function () {
7
+ // detect whether we are running in a browser or nodejs
8
+ if (typeof module !== "undefined" && module.exports) {
9
+ return require("path").resolve(__dirname)
10
+ }
11
+ throw new Error('getAbsoluteFSPath can only be called within a Nodejs environment');
12
+ }
13
+
14
+ module.exports = getAbsoluteFSPath
@@ -0,0 +1,60 @@
1
+ <!-- HTML for static distribution bundle build -->
2
+ <!DOCTYPE html>
3
+ <html lang="en">
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <title>Swagger UI</title>
7
+ <link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
8
+ <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
9
+ <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
10
+ <style>
11
+ html
12
+ {
13
+ box-sizing: border-box;
14
+ overflow: -moz-scrollbars-vertical;
15
+ overflow-y: scroll;
16
+ }
17
+
18
+ *,
19
+ *:before,
20
+ *:after
21
+ {
22
+ box-sizing: inherit;
23
+ }
24
+
25
+ body
26
+ {
27
+ margin:0;
28
+ background: #fafafa;
29
+ }
30
+ </style>
31
+ </head>
32
+
33
+ <body>
34
+ <div id="swagger-ui"></div>
35
+
36
+ <script src="./swagger-ui-bundle.js"> </script>
37
+ <script src="./swagger-ui-standalone-preset.js"> </script>
38
+ <script>
39
+ window.onload = function() {
40
+
41
+ // Build a system
42
+ const ui = SwaggerUIBundle({
43
+ url: "https://petstore.swagger.io/v2/swagger.json",
44
+ dom_id: '#swagger-ui',
45
+ deepLinking: true,
46
+ presets: [
47
+ SwaggerUIBundle.presets.apis,
48
+ SwaggerUIStandalonePreset
49
+ ],
50
+ plugins: [
51
+ SwaggerUIBundle.plugins.DownloadUrl
52
+ ],
53
+ layout: "StandaloneLayout"
54
+ })
55
+
56
+ window.ui = ui
57
+ }
58
+ </script>
59
+ </body>
60
+ </html>