mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Move spec generator files, organize whitespace in generator output
This commit is contained in:
parent
5ee880b769
commit
189a7ef061
@ -1,24 +0,0 @@
|
|||||||
require 'rspec/rails/swagger/route_parser'
|
|
||||||
require 'rails/generators'
|
|
||||||
|
|
||||||
module Rspec
|
|
||||||
module Generators
|
|
||||||
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
|
||||||
source_root File.expand_path('../templates', __FILE__)
|
|
||||||
|
|
||||||
def setup
|
|
||||||
@routes = RSpec::Rails::Swagger::RouteParser.new(controller_path).routes
|
|
||||||
end
|
|
||||||
|
|
||||||
def create_spec_file
|
|
||||||
template 'spec.rb', File.join('spec', 'requests', "#{controller_path}_spec.rb")
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def controller_path
|
|
||||||
file_path.chomp('_controller')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
22
rswag-specs/lib/generators/rspec/swagger_generator.rb
Normal file
22
rswag-specs/lib/generators/rspec/swagger_generator.rb
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
require 'rswag/route_parser'
|
||||||
|
require 'rails/generators'
|
||||||
|
|
||||||
|
module Rspec
|
||||||
|
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
||||||
|
source_root File.expand_path('../templates', __FILE__)
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@routes = Rswag::RouteParser.new(controller_path).routes
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_spec_file
|
||||||
|
template 'spec.rb', File.join('spec', 'requests', "#{controller_path}_spec.rb")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def controller_path
|
||||||
|
file_path.chomp('_controller')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,21 +1,21 @@
|
|||||||
require 'swagger_helper'
|
require 'swagger_helper'
|
||||||
|
|
||||||
RSpec.describe '<%= controller_path %>', type: :request do
|
RSpec.describe '<%= controller_path %>', type: :request do
|
||||||
<% @routes.each do | template, path_item | %>
|
<% @routes.each do | template, path_item | %>
|
||||||
path '<%= template %>' do
|
path '<%= template %>' do
|
||||||
<% unless path_item[:params].empty? -%>
|
<% unless path_item[:params].empty? -%>
|
||||||
# You'll want to customize the parameter types...
|
# You'll want to customize the parameter types...
|
||||||
<% path_item[:params].each do |param| -%>
|
<% path_item[:params].each do |param| -%>
|
||||||
parameter '<%= param %>', in: :body, type: :string
|
parameter '<%= param %>', in: :body, type: :string
|
||||||
<% end -%>
|
|
||||||
<% end -%>
|
<% end -%>
|
||||||
<% path_item[:actions].each do | action, details | -%>
|
<% end -%>
|
||||||
|
<% path_item[:actions].each do | action, details | %>
|
||||||
<%= action %>('<%= details[:summary] %>') do
|
<%= action %>('<%= details[:summary] %>') do
|
||||||
response(200, 'successful') do
|
response(200, 'successful') do
|
||||||
<% unless path_item[:params].empty? -%>
|
<% unless path_item[:params].empty? -%>
|
||||||
<% path_item[:params].each do |param| -%>
|
<% path_item[:params].each do |param| -%>
|
||||||
let(:<%= param %>) { '123' }
|
let(:<%= param %>) { '123' }
|
||||||
<% end -%>
|
<% end -%>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
||||||
after do |example|
|
after do |example|
|
||||||
@ -1,62 +0,0 @@
|
|||||||
module RSpec
|
|
||||||
module Rails
|
|
||||||
module Swagger
|
|
||||||
class RouteParser
|
|
||||||
attr_reader :controller
|
|
||||||
|
|
||||||
def initialize(controller)
|
|
||||||
@controller = controller
|
|
||||||
end
|
|
||||||
|
|
||||||
def routes
|
|
||||||
::Rails.application.routes.routes.select do |route|
|
|
||||||
route.defaults[:controller] == controller
|
|
||||||
end.reduce({}) do |tree, route|
|
|
||||||
path = path_from(route)
|
|
||||||
verb = verb_from(route)
|
|
||||||
tree[path] ||= { params: params_from(route), actions: {} }
|
|
||||||
tree[path][:actions][verb] = { summary: summary_from(route) }
|
|
||||||
tree
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def path_from(route)
|
|
||||||
route.path.spec.to_s
|
|
||||||
.chomp('(.:format)') # Ignore any format suffix
|
|
||||||
.gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
|
|
||||||
end
|
|
||||||
|
|
||||||
def verb_from(route)
|
|
||||||
verb = route.verb
|
|
||||||
if verb.kind_of? String
|
|
||||||
verb.downcase
|
|
||||||
else
|
|
||||||
verb.source.gsub(/[$^]/, '').downcase
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def summary_from(route)
|
|
||||||
verb = route.requirements[:action]
|
|
||||||
noun = route.requirements[:controller].split('/').last.singularize
|
|
||||||
|
|
||||||
# Apply a few customizations to make things more readable
|
|
||||||
case verb
|
|
||||||
when 'index'
|
|
||||||
verb = 'list'
|
|
||||||
noun = noun.pluralize
|
|
||||||
when 'destroy'
|
|
||||||
verb = 'delete'
|
|
||||||
end
|
|
||||||
|
|
||||||
"#{verb} #{noun}"
|
|
||||||
end
|
|
||||||
|
|
||||||
def params_from(route)
|
|
||||||
route.segments - ['format']
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
58
rswag-specs/lib/rswag/route_parser.rb
Normal file
58
rswag-specs/lib/rswag/route_parser.rb
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
module Rswag
|
||||||
|
class RouteParser
|
||||||
|
attr_reader :controller
|
||||||
|
|
||||||
|
def initialize(controller)
|
||||||
|
@controller = controller
|
||||||
|
end
|
||||||
|
|
||||||
|
def routes
|
||||||
|
::Rails.application.routes.routes.select do |route|
|
||||||
|
route.defaults[:controller] == controller
|
||||||
|
end.reduce({}) do |tree, route|
|
||||||
|
path = path_from(route)
|
||||||
|
verb = verb_from(route)
|
||||||
|
tree[path] ||= { params: params_from(route), actions: {} }
|
||||||
|
tree[path][:actions][verb] = { summary: summary_from(route) }
|
||||||
|
tree
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def path_from(route)
|
||||||
|
route.path.spec.to_s
|
||||||
|
.chomp('(.:format)') # Ignore any format suffix
|
||||||
|
.gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
|
||||||
|
end
|
||||||
|
|
||||||
|
def verb_from(route)
|
||||||
|
verb = route.verb
|
||||||
|
if verb.kind_of? String
|
||||||
|
verb.downcase
|
||||||
|
else
|
||||||
|
verb.source.gsub(/[$^]/, '').downcase
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def summary_from(route)
|
||||||
|
verb = route.requirements[:action]
|
||||||
|
noun = route.requirements[:controller].split('/').last.singularize
|
||||||
|
|
||||||
|
# Apply a few customizations to make things more readable
|
||||||
|
case verb
|
||||||
|
when 'index'
|
||||||
|
verb = 'list'
|
||||||
|
noun = noun.pluralize
|
||||||
|
when 'destroy'
|
||||||
|
verb = 'delete'
|
||||||
|
end
|
||||||
|
|
||||||
|
"#{verb} #{noun}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def params_from(route)
|
||||||
|
route.segments - ['format']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user