rails-swagger 0.1.1 → 0.2.0
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 +5 -5
- data/lib/rails/swagger.rb +2 -2
- data/lib/rails/swagger/controller.rb +41 -37
- data/lib/rails/swagger/engine.rb +156 -154
- data/lib/rails/swagger/router.rb +180 -173
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d296e1d1dc96b987c238535613568bdc345e7b8c0fe9eb40559d7aafe6b059ae
|
4
|
+
data.tar.gz: 7e4304f36b6b494878e0a86938757f5cfdbdf811e2af9d1464b6f33a9844546c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c674eb007e4e5b247d97f86a976ef267f336b70a1c3de68425c42bff583c14f0f88c8f549ae821c66eac0c17435cea2112f2dae866e35372a24f8518b0328d09
|
7
|
+
data.tar.gz: 32037a7aaf48446df4c1aeef58d1103622304d4560e2aa22b01936caf3bc14fd0b86105e26bf9a96367a1aa0d9c2770747a8941b282655ecf37ce0dc3ee9adcd
|
data/lib/rails/swagger.rb
CHANGED
@@ -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,195 @@
|
|
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
|
156
124
|
|
157
|
-
|
125
|
+
# Draw subroutes directly
|
126
|
+
draw_subroutes! map
|
158
127
|
|
159
|
-
|
128
|
+
when :action
|
160
129
|
|
161
|
-
|
130
|
+
# Draw actions directly
|
131
|
+
draw_actions! map
|
162
132
|
|
163
|
-
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
164
136
|
|
165
|
-
|
166
|
-
|
167
|
-
params[:via] = route[:method]
|
168
|
-
params[:on] = self.action_mode unless self.action_mode == :param
|
169
|
-
params[:action] = self.action_for route
|
137
|
+
# Returns the routing tree in text format
|
138
|
+
def to_s
|
170
139
|
|
171
|
-
|
172
|
-
next if Symbol === params[:action]
|
140
|
+
output = ""
|
173
141
|
|
174
|
-
|
175
|
-
|
142
|
+
path = "/" + @prefix.join('/')
|
143
|
+
@endpoints.each do |route|
|
144
|
+
output += "#{route[:method].to_s.upcase} #{path}\n"
|
145
|
+
end
|
146
|
+
@subroutes.each do |k, subroute|
|
147
|
+
output += subroute.to_s
|
148
|
+
end
|
176
149
|
|
177
|
-
|
150
|
+
output
|
178
151
|
|
179
|
-
|
152
|
+
end
|
180
153
|
|
181
|
-
|
182
|
-
|
183
|
-
end
|
154
|
+
# Outputs a visual representation of the routing tree
|
155
|
+
def _debug_routing_tree
|
184
156
|
|
185
|
-
|
157
|
+
puts self.path + " - #{self.route_mode}"
|
158
|
+
@endpoints.each do |route|
|
159
|
+
puts "\t#{route[:method].to_s.upcase} to ##{self.action_for route} (#{self.action_mode})"
|
160
|
+
end
|
161
|
+
@subroutes.each do |k, subroute| subroute._debug_routing_tree end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
protected
|
166
|
+
|
167
|
+
def draw_actions! map
|
168
|
+
|
169
|
+
@endpoints.each do |route|
|
170
|
+
|
171
|
+
# Params hash for the route to be added
|
172
|
+
params = Hash.new
|
173
|
+
params[:via] = route[:method]
|
174
|
+
params[:on] = self.action_mode unless self.action_mode == :param
|
175
|
+
params[:action] = self.action_for route
|
176
|
+
|
177
|
+
# These are handled in the resource
|
178
|
+
next if Symbol === params[:action]
|
179
|
+
|
180
|
+
# Add this individual route
|
181
|
+
map.match @prefix.last, params
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
def draw_subroutes! map
|
188
|
+
@subroutes.values.each { |r| r.draw map }
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
186
194
|
|
187
|
-
end
|
188
195
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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: 2019-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,8 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
|
101
|
-
rubygems_version: 2.6.8
|
100
|
+
rubygems_version: 3.0.1
|
102
101
|
signing_key:
|
103
102
|
specification_version: 4
|
104
103
|
summary: Turns Swagger API schemas into mountable Rails engines
|