openapi-rswag-api 0.0.4

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9106b48849a9699eb75e67340017873b9d1df7c2b9ad247e3942ad8d38706572
4
+ data.tar.gz: c620687aae5a1abcc49fff05da72edd4d3fb42a77335628509901fedd66b74f0
5
+ SHA512:
6
+ metadata.gz: 3bbda0cb283ddc7c517c4b8908b32855a43a40f8611dddb53c006f59f9cff26cc64d60aaf2dc6e6daa64df4ff30b9101a7dcb20ec90758546b84ee13e8863a7e
7
+ data.tar.gz: d75dea98fdb20f2f7332db399125b9f0e9164c1b7375970e7f22b130c656c67363bc13a94d97a7aa8ed9f9928f727ec45acd040f9ce3cd959579de96d8ed8230
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,16 @@
1
+ require 'openapi/rswag/api/configuration'
2
+ require 'openapi/rswag/api/engine'
3
+
4
+ module Openapi
5
+ module Rswag
6
+ module Api
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,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 'openapi/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,63 @@
1
+ require 'json'
2
+ require 'yaml'
3
+ require 'rack/mime'
4
+
5
+ module Openapi
6
+ module Rswag
7
+ module Api
8
+ class Middleware
9
+
10
+ def initialize(app, config)
11
+ @app = app
12
+ @config = config
13
+ end
14
+
15
+ def call(env)
16
+ path = env['PATH_INFO']
17
+ filename = "#{@config.resolve_swagger_root(env)}/#{path}"
18
+
19
+ if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
20
+ swagger = parse_file(filename)
21
+ @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
22
+ mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain')
23
+ body = unload_swagger(filename, swagger)
24
+
25
+ return [
26
+ '200',
27
+ { 'Content-Type' => mime },
28
+ [ body ]
29
+ ]
30
+ end
31
+
32
+ return @app.call(env)
33
+ end
34
+
35
+ private
36
+
37
+ def parse_file(filename)
38
+ if /\.ya?ml$/ === filename
39
+ load_yaml(filename)
40
+ else
41
+ load_json(filename)
42
+ end
43
+ end
44
+
45
+ def load_yaml(filename)
46
+ YAML.safe_load(File.read(filename))
47
+ end
48
+
49
+ def load_json(filename)
50
+ JSON.parse(File.read(filename))
51
+ end
52
+
53
+ def unload_swagger(filename, swagger)
54
+ if /\.ya?ml$/ === filename
55
+ YAML.dump(swagger)
56
+ else
57
+ JSON.dump(swagger)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openapi-rswag-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Emmanuel Ndangurura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ description: Open up your API to the phenomenal Swagger ecosystem by exposing Swagger
34
+ files, that describe your service, as JSON endpoints
35
+ email:
36
+ - endangurura@gmail.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - MIT-LICENSE
42
+ - Rakefile
43
+ - lib/generators/rswag/api/install/USAGE
44
+ - lib/generators/rswag/api/install/install_generator.rb
45
+ - lib/generators/rswag/api/install/templates/rswag-api.rb
46
+ - lib/openapi/rswag/api.rb
47
+ - lib/openapi/rswag/api/configuration.rb
48
+ - lib/openapi/rswag/api/engine.rb
49
+ - lib/openapi/rswag/api/middleware.rb
50
+ homepage: https://github.com/endangurura/rswag
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.2
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A Rails Engine that exposes Swagger files as JSON endpoints
73
+ test_files: []