rails-swagger 0.1.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/rails/swagger/controller.rb +41 -37
- data/lib/rails/swagger/engine.rb +156 -154
- data/lib/rails/swagger/router.rb +181 -173
- data/lib/rails/swagger.rb +2 -3
- metadata +3 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 462fb2b6ed49fc0de9bbf5542f0d71932ab5b469f3b9734bea7bd2d542ceae70
|
4
|
+
data.tar.gz: f6422820a1a13d6973ddc71e8d903bee18058883bfd2bb9d2df147d511acacfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb82c2935ea4b28614689a94f1861d0477c77281f53fe39222bc51ce9b0ab102ecec418ef6f3ad4b6292dd77c15da3ed382c4809ebe0c6c4450d8e5b5782eaa4
|
7
|
+
data.tar.gz: f616ceb7ffafc3feb4ebc5e935fdb0a153c724e93e8da0497ea9592f0e8afc249d50eacdf3b4b06d70f0748a3ed89092f8a96ad95fdef207ee7301e74aeea710
|
@@ -1,39 +1,43 @@
|
|
1
1
|
module Rails
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
|
5
|
+
module Controller
|
6
|
+
|
7
|
+
# METHODS_WITH_BODIES = [
|
8
|
+
# :post,
|
9
|
+
# :patch,
|
10
|
+
# :put
|
11
|
+
# ].freeze
|
12
|
+
|
13
|
+
# Injects swagger-related code into the controller when included
|
14
|
+
def self.included base
|
15
|
+
|
16
|
+
# Add controller hooks
|
17
|
+
# base.class_eval do
|
18
|
+
# before_action :swagger_validate_params
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Returns the swagger spec definition for the endpoint serving
|
22
|
+
# the current request.
|
23
|
+
def swagger_endpoint
|
24
|
+
key = "#{params[:controller]}##{params[:action]}"
|
25
|
+
swagger_engine.endpoints[key]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Validates request parameters against the Swagger API spec
|
29
|
+
# associated with this controller.
|
30
|
+
# def swagger_validate_params
|
31
|
+
# if METHODS_WITH_BODIES.include? request.method_symbol
|
32
|
+
# body = request.POST
|
33
|
+
# # TODO: add validation here
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
39
43
|
end
|
data/lib/rails/swagger/engine.rb
CHANGED
@@ -1,156 +1,158 @@
|
|
1
1
|
module Rails
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
2
|
+
|
3
|
+
module Swagger
|
4
|
+
|
5
|
+
# Defines the Swagger spec file formats that are parsable by this gem.
|
6
|
+
# Currently only the JSON format is supported.
|
7
|
+
ALLOWED_FORMATS = [".json"].freeze
|
8
|
+
|
9
|
+
# Defines a base class from which Swagger API engines can be created.
|
10
|
+
# Uses namespace isolation to ensure routes don't conflict with any
|
11
|
+
# pre-existing routes from the main rails application.
|
12
|
+
class Engine < Rails::Engine
|
13
|
+
isolate_namespace Rails::Swagger
|
14
|
+
end
|
15
|
+
|
16
|
+
# Helper method to create a new engine based on a module namespace
|
17
|
+
# prefix and Swagger spec file. The engine ceated will be a subclass of
|
18
|
+
# Rails::Swagger::Engine, which itself inherits from Rails::Engine.
|
19
|
+
def self.Engine base_module, file
|
20
|
+
|
21
|
+
# Convert the module prefix into a constant if passed in as a string
|
22
|
+
base_module = Object.const_get base_module if String === base_module
|
23
|
+
|
24
|
+
# Ensure the Swagger spec file is in an acceptable format
|
25
|
+
ext = File.extname(file)
|
26
|
+
unless ALLOWED_FORMATS.include? ext
|
27
|
+
raise "Swagger files must end with #{ALLOWED_FORMATS.join(' or ')}. File given: #{file}"
|
28
|
+
end
|
29
|
+
|
30
|
+
# Attempt to read and parse the Swagger spec file
|
31
|
+
document = File.read file
|
32
|
+
case File.extname file
|
33
|
+
when ".json"
|
34
|
+
begin
|
35
|
+
require 'json'
|
36
|
+
document = JSON.parse document
|
37
|
+
rescue JSON::ParserError
|
38
|
+
raise $!, "Problem parsing swagger spec file \"#{file}\": #{$!.message.lines.first.strip}", $@
|
39
|
+
end
|
40
|
+
else
|
41
|
+
raise "Swagger files must end with #{ALLOWED_FORMATS.join(' or ')}. File given: #{file}"
|
42
|
+
end
|
43
|
+
|
44
|
+
# Verify that the swagger version is supported
|
45
|
+
unless document["swagger"] == "2.0"
|
46
|
+
raise "Unsupported swagger version: #{document["swagger"]}. #{self} supports only version 2.0"
|
47
|
+
end
|
48
|
+
|
49
|
+
# Builds a routing tree based on the swagger spec file.
|
50
|
+
# We'll add each endpoint to the routing tree and additionally
|
51
|
+
# store it in an array to be used below.
|
52
|
+
router = Router.new
|
53
|
+
endpoints = []
|
54
|
+
document["paths"].each do |url, actions|
|
55
|
+
actions.each do |verb, definition|
|
56
|
+
route = Endpoint.new(verb.downcase.to_sym, url, definition)
|
57
|
+
router << route
|
58
|
+
endpoints << route
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Creates the engine that will be used to actually route the
|
63
|
+
# contents of the swagger spec file. The engine will eventually be
|
64
|
+
# attached to the base module (argument to this current method).
|
65
|
+
#
|
66
|
+
# Exposes `::router` and `::endpoints` methods to allow other parts
|
67
|
+
# of the code to tie requests back to their spec file definitions.
|
68
|
+
engine = Class.new Engine do
|
69
|
+
|
70
|
+
@router = router
|
71
|
+
@endpoints = Hash.new
|
72
|
+
@schema = document
|
73
|
+
|
74
|
+
class << self
|
75
|
+
def router
|
76
|
+
@router
|
77
|
+
end
|
78
|
+
def endpoints
|
79
|
+
@endpoints
|
80
|
+
end
|
81
|
+
def schema
|
82
|
+
@schema
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Rack app for serving the original swagger file
|
87
|
+
# swagger_app = Class.new do
|
88
|
+
# def inspect
|
89
|
+
# "Rails::Swagger::Engine"
|
90
|
+
# end
|
91
|
+
# define_method :call do |env|
|
92
|
+
# [
|
93
|
+
# 200,
|
94
|
+
# {"Content-Type" => "application/json"},
|
95
|
+
# [engine.schema.to_json]
|
96
|
+
# ]
|
97
|
+
# end
|
98
|
+
# end
|
99
|
+
|
100
|
+
# Adds routes to the engine by passing the Mapper to the top
|
101
|
+
# of the routing tree. `self` inside the block refers to an
|
102
|
+
# instance of `ActionDispatch::Routing::Mapper`.
|
103
|
+
self.routes.draw do
|
104
|
+
scope module: base_module.name.underscore, format: false do
|
105
|
+
# get "swagger.json", to: swagger_app.new
|
106
|
+
router.draw self
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
# Assign the engine as a class on the base module
|
113
|
+
base_module.const_set :Engine, engine
|
114
|
+
|
115
|
+
# Creates a hash that maps routes back to their swagger spec file
|
116
|
+
# equivalents. This is accomplished by mocking a request for each
|
117
|
+
# swagger spec file endpoint and determining which controller and
|
118
|
+
# action the request is routed to. Swagger spec file definitions
|
119
|
+
# are then attached to that controller/action pair.
|
120
|
+
endpoints.each do |route|
|
121
|
+
|
122
|
+
# Mocks a request using the route's URL
|
123
|
+
url = ::ActionDispatch::Journey::Router::Utils.normalize_path route.path
|
124
|
+
env = ::Rack::MockRequest.env_for url, method: route[:method].upcase
|
125
|
+
req = ::ActionDispatch::Request.new env
|
126
|
+
|
127
|
+
# Maps the swagger spec endpoint to the destination controller
|
128
|
+
# action by routing the request.
|
129
|
+
mapped = engine.routes.router.recognize(req){}.first[1]
|
130
|
+
key = "#{mapped[:controller]}##{mapped[:action]}"
|
131
|
+
engine.endpoints[key] = route
|
132
|
+
|
133
|
+
end
|
134
|
+
engine.endpoints.freeze
|
135
|
+
|
136
|
+
# Defines a helper module on the base module that can be used to
|
137
|
+
# properly generate swagger-aware controllers. Any controllers
|
138
|
+
# referenced from a swagger spec file should include this module.
|
139
|
+
mod = Module.new do
|
140
|
+
@base = base_module
|
141
|
+
def self.included controller
|
142
|
+
base_module = @base
|
143
|
+
controller.include Controller
|
144
|
+
define_method :swagger_engine do
|
145
|
+
base_module.const_get :Engine
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
base_module.const_set :SwaggerController, mod
|
150
|
+
|
151
|
+
# Returns the new engine
|
152
|
+
base_module.const_get :Engine
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
156
158
|
end
|
data/lib/rails/swagger/router.rb
CHANGED
@@ -1,188 +1,196 @@
|
|
1
1
|
module Rails
|
2
|
-
module Swagger
|
3
|
-
|
4
|
-
# Internally represents individual routes
|
5
|
-
Endpoint = Struct.new(:method, :url, :definition, :_path) do
|
6
|
-
def initialize *opts
|
7
|
-
super
|
8
|
-
self[:_path] = self.path
|
9
|
-
end
|
10
|
-
# Translates path params from {bracket} syntax to :symbol syntax
|
11
|
-
def path
|
12
|
-
self[:url].gsub /\{(.+)\}/, ':\\1'
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
# Defines RESTful routing conventions
|
17
|
-
RESOURCE_ROUTES = {
|
18
|
-
get: :index,
|
19
|
-
post: :create
|
20
|
-
}.freeze
|
21
|
-
PARAM_ROUTES = {
|
22
|
-
get: :show,
|
23
|
-
patch: :update,
|
24
|
-
put: :update,
|
25
|
-
delete: :destroy
|
26
|
-
}.freeze
|
27
|
-
|
28
|
-
class Router
|
29
|
-
|
30
|
-
attr_accessor :endpoints
|
31
|
-
|
32
|
-
def initialize prefix = [], parent = nil
|
33
|
-
@parent = parent
|
34
|
-
@prefix = prefix.freeze
|
35
|
-
@endpoints = []
|
36
|
-
@subroutes = Hash.new do |hash, k|
|
37
|
-
hash[k] = Router.new(@prefix + [k], self)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Adds an individual endpoint to the routing tree
|
42
|
-
def << route
|
43
|
-
raise "Argument must be an Endpoint" unless Endpoint === route
|
44
|
-
base, *subroute = route[:_path].split '/' # Split out first element
|
45
|
-
if subroute.count == 0
|
46
|
-
route[:_path] = ""
|
47
|
-
@endpoints << route
|
48
|
-
else
|
49
|
-
route[:_path] = subroute.join '/'
|
50
|
-
self[subroute[0]] << route
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
# Returns a specific branch of the routing tree
|
55
|
-
def [] path
|
56
|
-
@subroutes[path]
|
57
|
-
end
|
58
|
-
|
59
|
-
# Returns the routing path
|
60
|
-
def path
|
61
|
-
"/" + @prefix.join("/")
|
62
|
-
end
|
63
|
-
|
64
|
-
# Returns the mode used for collecting routes
|
65
|
-
def route_mode
|
66
|
-
mode = :resource
|
67
|
-
mode = :namespace if @endpoints.count == 0
|
68
|
-
mode = :action if @subroutes.count == 0 && @parent && @parent.route_mode == :resource
|
69
|
-
mode
|
70
|
-
end
|
71
|
-
|
72
|
-
# Returns the mode used for actions in this router
|
73
|
-
def action_mode
|
74
|
-
if /^:/ === @prefix[-2]
|
75
|
-
:member
|
76
|
-
elsif /^:/ === @prefix[-1]
|
77
|
-
:param
|
78
|
-
else
|
79
|
-
:collection
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# Determines the action for a specific route
|
84
|
-
def action_for route
|
85
|
-
raise "Argument must be an Endpoint" unless Endpoint === route
|
86
|
-
action = @prefix[-1]
|
87
|
-
action = PARAM_ROUTES[route[:method]] if self.action_mode == :param
|
88
|
-
action = RESOURCE_ROUTES[route[:method]] if self.route_mode == :resource && self.action_mode == :collection
|
89
|
-
action
|
90
|
-
end
|
91
|
-
|
92
|
-
# Draws the routes for this router
|
93
|
-
def draw map
|
94
|
-
case self.route_mode
|
95
|
-
when :resource
|
96
|
-
|
97
|
-
# Find collection-level resource actions
|
98
|
-
actions = @endpoints.map{ |r| self.action_for r }.select{ |a| Symbol === a }
|
99
|
-
|
100
|
-
# Find parameter-level resource actions
|
101
|
-
@subroutes.select{ |k, _| /^:/ === k }.values.each do |subroute|
|
102
|
-
actions += subroute.endpoints.map{ |r| subroute.action_for r }.select{ |a| Symbol === a }
|
103
|
-
end
|
104
|
-
|
105
|
-
# Draw a resource
|
106
|
-
map.resources @prefix.last.to_sym, only: actions do
|
107
|
-
draw_actions! map
|
108
|
-
draw_subroutes! map
|
109
|
-
end
|
110
|
-
|
111
|
-
when :namespace
|
112
|
-
|
113
|
-
# Draw a namespace (unless at the top)
|
114
|
-
if @prefix.join("/").blank?
|
115
|
-
draw_subroutes! map
|
116
|
-
else
|
117
|
-
map.namespace @prefix.last do
|
118
|
-
draw_subroutes! map
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
when :action
|
123
|
-
|
124
|
-
# Draw actions directly
|
125
|
-
draw_actions! map
|
126
|
-
|
127
|
-
end
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
# Returns the routing tree in text format
|
132
|
-
def to_s
|
133
|
-
|
134
|
-
output = ""
|
135
|
-
|
136
|
-
path = "/" + @prefix.join('/')
|
137
|
-
@endpoints.each do |route|
|
138
|
-
output += "#{route[:method].to_s.upcase} #{path}\n"
|
139
|
-
end
|
140
|
-
@subroutes.each do |k, subroute|
|
141
|
-
output += subroute.to_s
|
142
|
-
end
|
143
|
-
|
144
|
-
output
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
# Outputs a visual representation of the routing tree
|
149
|
-
def _debug_routing_tree
|
150
2
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
3
|
+
module Swagger
|
4
|
+
|
5
|
+
# Internally represents individual routes
|
6
|
+
Endpoint = Struct.new(:method, :url, :definition, :_path) do
|
7
|
+
def initialize *opts
|
8
|
+
super
|
9
|
+
self[:_path] = self.path
|
10
|
+
end
|
11
|
+
# Translates path params from {bracket} syntax to :symbol syntax
|
12
|
+
def path
|
13
|
+
self[:url].gsub(/\{([^}]+)\}/, ':\\1')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Defines RESTful routing conventions
|
18
|
+
RESOURCE_ROUTES = {
|
19
|
+
get: :index,
|
20
|
+
post: :create
|
21
|
+
}.freeze
|
22
|
+
PARAM_ROUTES = {
|
23
|
+
get: :show,
|
24
|
+
patch: :update,
|
25
|
+
put: :update,
|
26
|
+
delete: :destroy
|
27
|
+
}.freeze
|
28
|
+
|
29
|
+
class Router
|
30
|
+
|
31
|
+
attr_accessor :endpoints
|
32
|
+
|
33
|
+
def initialize prefix = [], parent = nil
|
34
|
+
@parent = parent
|
35
|
+
@prefix = prefix.freeze
|
36
|
+
@endpoints = []
|
37
|
+
@subroutes = Hash.new do |hash, k|
|
38
|
+
hash[k] = Router.new(@prefix + [k], self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Adds an individual endpoint to the routing tree
|
43
|
+
def << route
|
44
|
+
raise "Argument must be an Endpoint" unless Endpoint === route
|
45
|
+
_base, *subroute = route[:_path].split '/' # Split out first element
|
46
|
+
if subroute.count == 0
|
47
|
+
route[:_path] = ""
|
48
|
+
@endpoints << route
|
49
|
+
else
|
50
|
+
route[:_path] = subroute.join '/'
|
51
|
+
self[subroute[0]] << route
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Returns a specific branch of the routing tree
|
56
|
+
def [] path
|
57
|
+
@subroutes[path]
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the routing path
|
61
|
+
def path
|
62
|
+
"/" + @prefix.join("/")
|
63
|
+
end
|
64
|
+
|
65
|
+
# Returns the mode used for collecting routes
|
66
|
+
def route_mode
|
67
|
+
mode = :resource
|
68
|
+
mode = :namespace if @endpoints.count == 0
|
69
|
+
mode = :action if @subroutes.count == 0 && @parent && @parent.route_mode == :resource
|
70
|
+
mode = :param if /^:/ === @prefix.last
|
71
|
+
mode
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the mode used for actions in this router
|
75
|
+
def action_mode
|
76
|
+
if /^:/ === @prefix[-1]
|
77
|
+
:param
|
78
|
+
else
|
79
|
+
:collection
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# Determines the action for a specific route
|
84
|
+
def action_for route
|
85
|
+
raise "Argument must be an Endpoint" unless Endpoint === route
|
86
|
+
action = @prefix[-1]
|
87
|
+
action = PARAM_ROUTES[route[:method]] if self.action_mode == :param
|
88
|
+
action = RESOURCE_ROUTES[route[:method]] if self.route_mode == :resource && self.action_mode == :collection
|
89
|
+
action
|
90
|
+
end
|
91
|
+
|
92
|
+
# Draws the routes for this router
|
93
|
+
def draw map
|
94
|
+
|
95
|
+
case self.route_mode
|
96
|
+
when :resource
|
97
|
+
|
98
|
+
# Find collection-level resource actions
|
99
|
+
actions = @endpoints.map{ |r| self.action_for r }.select{ |a| Symbol === a }
|
100
|
+
|
101
|
+
# Find parameter-level resource actions
|
102
|
+
@subroutes.select{ |k, _| /^:/ === k }.values.each do |subroute|
|
103
|
+
actions += subroute.endpoints.map{ |r| subroute.action_for r }.select{ |a| Symbol === a }
|
104
|
+
end
|
105
|
+
|
106
|
+
# Draw a resource
|
107
|
+
map.resources @prefix.last.to_sym, only: actions do
|
108
|
+
draw_actions! map
|
109
|
+
draw_subroutes! map
|
110
|
+
end
|
111
|
+
|
112
|
+
when :namespace
|
113
|
+
|
114
|
+
# Draw a namespace (unless at the top)
|
115
|
+
if @prefix.join("/").blank?
|
116
|
+
draw_subroutes! map
|
117
|
+
else
|
118
|
+
map.namespace @prefix.last do
|
119
|
+
draw_subroutes! map
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
when :param
|
124
|
+
|
125
|
+
# Draw subroutes directly
|
126
|
+
draw_subroutes! map
|
127
|
+
draw_actions! map
|
156
128
|
|
157
|
-
|
129
|
+
when :action
|
158
130
|
|
159
|
-
|
131
|
+
# Draw actions directly
|
132
|
+
draw_actions! map
|
133
|
+
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
160
137
|
|
161
|
-
|
138
|
+
# Returns the routing tree in text format
|
139
|
+
def to_s
|
162
140
|
|
163
|
-
|
141
|
+
output = ""
|
164
142
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
143
|
+
path = "/" + @prefix.join('/')
|
144
|
+
@endpoints.each do |route|
|
145
|
+
output += "#{route[:method].to_s.upcase} #{path}\n"
|
146
|
+
end
|
147
|
+
@subroutes.each do |k, subroute|
|
148
|
+
output += subroute.to_s
|
149
|
+
end
|
170
150
|
|
171
|
-
|
172
|
-
next if Symbol === params[:action]
|
151
|
+
output
|
173
152
|
|
174
|
-
|
175
|
-
map.match @prefix.last, params
|
153
|
+
end
|
176
154
|
|
177
|
-
|
155
|
+
# Outputs a visual representation of the routing tree
|
156
|
+
def _debug_routing_tree
|
178
157
|
|
179
|
-
|
158
|
+
puts self.path + " - #{self.route_mode}"
|
159
|
+
@endpoints.each do |route|
|
160
|
+
puts "\t#{route[:method].to_s.upcase} to ##{self.action_for route} (#{self.action_mode})"
|
161
|
+
end
|
162
|
+
@subroutes.each do |k, subroute| subroute._debug_routing_tree end
|
180
163
|
|
181
|
-
|
182
|
-
@subroutes.values.each { |r| r.draw map }
|
183
|
-
end
|
164
|
+
end
|
184
165
|
|
185
|
-
|
166
|
+
protected
|
167
|
+
|
168
|
+
def draw_actions! map
|
169
|
+
|
170
|
+
@endpoints.each do |route|
|
171
|
+
|
172
|
+
# Params hash for the route to be added
|
173
|
+
params = Hash.new
|
174
|
+
params[:via] = route[:method]
|
175
|
+
params[:on] = self.action_mode unless self.action_mode == :param
|
176
|
+
params[:action] = self.action_for route
|
177
|
+
|
178
|
+
# These are handled in the resource
|
179
|
+
next if Symbol === params[:action]
|
180
|
+
|
181
|
+
# Add this individual route
|
182
|
+
map.match @prefix.last, params
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
def draw_subroutes! map
|
189
|
+
@subroutes.values.each { |r| r.draw map }
|
190
|
+
end
|
191
|
+
|
192
|
+
end
|
193
|
+
|
194
|
+
end
|
186
195
|
|
187
|
-
end
|
188
196
|
end
|
data/lib/rails/swagger.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kenaniah Cerny
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: json-schema
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: bundler
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -111,8 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
97
|
- !ruby/object:Gem::Version
|
112
98
|
version: '0'
|
113
99
|
requirements: []
|
114
|
-
|
115
|
-
rubygems_version: 2.6.8
|
100
|
+
rubygems_version: 3.1.2
|
116
101
|
signing_key:
|
117
102
|
specification_version: 4
|
118
103
|
summary: Turns Swagger API schemas into mountable Rails engines
|