open_api-rswag-api 0.0.4

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: 99586077f486a758641fb1285804f62c990f850c8dc3d201cbd4205d62743692
4
+ data.tar.gz: 7c9b4885ec478205ecdfb71fb5d611466406b079cdfae6ec6383e638fa83ac13
5
+ SHA512:
6
+ metadata.gz: 0353d1e672511b0bc14eadf7f072f3be1074c0bf094101b0a8a733fe0cc9ca7100f0416be584ddacc51659a9252620a38a4590768a972af019eecbe48b836521
7
+ data.tar.gz: 7f3fe88b823cec5a6f78e0e9a5d401e1171e19ec279a8355ebda3cf5db1ed1cf63ba26c6b26ef0ef78391d85fbf6024f15239a0ccfb9ea5135ca3f09d8a94cf6
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 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 Api
5
+
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def add_initializer
10
+ template('rswag_api.rb', 'config/initializers/rswag_api.rb')
11
+ end
12
+
13
+ def add_routes
14
+ route("mount OpenApi::Rswag::Api::Engine => '/api-docs'")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ OpenApi::Rswag::Api.configure do |c|
2
+
3
+ # Specify a root folder where Swagger JSON files are located
4
+ # This is used by the Swagger middleware to serve requests for API descriptions
5
+ # NOTE: If you're using rswag-specs to generate Swagger, you'll need to ensure
6
+ # that it's configured to generate files in the same folder
7
+ c.swagger_root = Rails.root.to_s + '/swagger'
8
+
9
+ # Inject a lamda function to alter the returned Swagger prior to serialization
10
+ # The function will have access to the rack env for the current request
11
+ # For example, you could leverage this to dynamically assign the "host" property
12
+ #
13
+ #c.swagger_filter = lambda { |swagger, env| swagger['host'] = env['HTTP_HOST'] }
14
+ end
@@ -0,0 +1,14 @@
1
+ module OpenApi
2
+ module Rswag
3
+ module Api
4
+ class Configuration
5
+ attr_accessor :swagger_root, :swagger_filter
6
+
7
+ def resolve_swagger_root(env)
8
+ path_params = env['action_dispatch.request.path_parameters'] || {}
9
+ path_params[:swagger_root] || swagger_root
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ require 'open_api/rswag/api/middleware'
2
+
3
+ module OpenApi
4
+ module Rswag
5
+ module Api
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace OpenApi::Rswag::Api
8
+
9
+ initializer 'rswag-api.initialize' do |app|
10
+ middleware.use OpenApi::Rswag::Api::Middleware, OpenApi::Rswag::Api.config
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+
3
+ module OpenApi
4
+ module Rswag
5
+ module Api
6
+ class Middleware
7
+
8
+ def initialize(app, config)
9
+ @app = app
10
+ @config = config
11
+ end
12
+
13
+ def call(env)
14
+ path = env['PATH_INFO']
15
+ filename = "#{@config.resolve_swagger_root(env)}/#{path}"
16
+
17
+ if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
18
+ swagger = load_json(filename)
19
+ @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
20
+
21
+ return [
22
+ '200',
23
+ { 'Content-Type' => 'application/json' },
24
+ [ JSON.dump(swagger) ]
25
+ ]
26
+ end
27
+
28
+ return @app.call(env)
29
+ end
30
+
31
+ private
32
+
33
+ def load_json(filename)
34
+ JSON.parse(File.read(filename))
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module OpenApi
2
+
3
+ end
4
+ require 'open_api/rswag/api/configuration'
5
+ require 'open_api/rswag/api/engine'
6
+
7
+ module OpenApi
8
+ module Rswag
9
+ module Api
10
+ def self.configure
11
+ yield(config)
12
+ end
13
+
14
+ def self.config
15
+ @config ||= Configuration.new
16
+ end
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: open_api-rswag-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Richie Morris
8
+ - Jay Danielian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '6.0'
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ version: '3.1'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ description: Open up your API to the phenomenal Swagger ecosystem by exposing Swagger
35
+ files, that describe your service, as JSON endpoints
36
+ email:
37
+ - domaindrivendev@gmail.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - MIT-LICENSE
43
+ - Rakefile
44
+ - lib/generators/rswag/api/install/USAGE
45
+ - lib/generators/rswag/api/install/install_generator.rb
46
+ - lib/generators/rswag/api/install/templates/rswag_api.rb
47
+ - lib/open_api/rswag/api.rb
48
+ - lib/open_api/rswag/api/configuration.rb
49
+ - lib/open_api/rswag/api/engine.rb
50
+ - lib/open_api/rswag/api/middleware.rb
51
+ homepage: https://github.com/jdanielian/open-api-rswag
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.0.4
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A Rails Engine that exposes Swagger files as JSON endpoints
74
+ test_files: []