rswag-api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f6ee076c91f10f2f2153af5909894942120f584
4
+ data.tar.gz: 9ec6137c51c1ead2a770b829c35abf9e811d0a3f
5
+ SHA512:
6
+ metadata.gz: 6639e7eb987de21234bf298b6b08d46767f32775d8352027ed29ea109a1675968baaea207cb90078c9a1fbba00773623f080f01467d6d6c2ceea30d3bd0d2c4e
7
+ data.tar.gz: 08b49778960052d1e36de9e82de3ad0068a1c828aeaede45e88048a071aa66a202af96b07bc1739be883dc0faf88064d9bd0a7550411ccfc39764c9f9b4849cb
@@ -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.
@@ -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 Rswag::Api::Engine => '/api-docs'")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ 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,15 @@
1
+ require 'rswag/api/version'
2
+ require 'rswag/api/configuration'
3
+ require 'rswag/api/engine'
4
+
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
@@ -0,0 +1,12 @@
1
+ module Rswag
2
+ module Api
3
+ class Configuration
4
+ attr_accessor :swagger_root, :swagger_filter
5
+
6
+ def resolve_swagger_root(env)
7
+ path_params = env['action_dispatch.request.path_parameters'] || {}
8
+ path_params[:swagger_root] || swagger_root
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'rswag/api/middleware'
2
+
3
+ module Rswag
4
+ module Api
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Rswag::Api
7
+
8
+ initializer 'rswag-api.initialize' do |app|
9
+ middleware.use Rswag::Api::Middleware, Rswag::Api.config
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ module Rswag
4
+ module Api
5
+ class Middleware
6
+
7
+ def initialize(app, config)
8
+ @app = app
9
+ @config = config
10
+ end
11
+
12
+ def call(env)
13
+ path = env['PATH_INFO']
14
+ filename = "#{@config.resolve_swagger_root(env)}/#{path}"
15
+
16
+ if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
17
+ swagger = load_json(filename)
18
+ @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?
19
+
20
+ return [
21
+ '200',
22
+ { 'Content-Type' => 'application/json' },
23
+ [ JSON.dump(swagger) ]
24
+ ]
25
+ end
26
+
27
+ return @app.call(env)
28
+ end
29
+
30
+ private
31
+
32
+ def load_json(filename)
33
+ JSON.parse(File.read(filename))
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ module Rswag
2
+ module Api
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rswag-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Richie Morris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
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: '5.1'
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: '5.1'
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
+ - domaindrivendev@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/rswag/api.rb
47
+ - lib/rswag/api/configuration.rb
48
+ - lib/rswag/api/engine.rb
49
+ - lib/rswag/api/middleware.rb
50
+ - lib/rswag/api/version.rb
51
+ homepage: https://github.com/domaindrivendev/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
+ rubyforge_project:
71
+ rubygems_version: 2.4.5
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A Rails Engine that exposes Swagger files as JSON endpoints
75
+ test_files: []