19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/swagger/dsl/rails_controller.rb', line 19
def swagger_define_path(action, format = :json, path: nil, method: nil, &block)
defaults = { action: action.to_s, controller: name.underscore.sub(/_controller$/, "") }
route = Rails.application.routes.routes.routes.find { |r| r.required_defaults == defaults }
unless route
raise NotMatch,
"route not found! specify additional :path and :method key like { path: '/foos/{id}', method: 'get'}"
end
method ||= route.verb.downcase
if method.include?("|")
raise NotExactMatch, "route matched but verb can be #{verb}! specify :method key like 'get'."
end
method = %w[put patch] if %w[put patch].include?(method)
path ||= route.path.spec.to_s.sub("(.:format)", "").gsub(/:(\w+)/, "{\\1}")
Swagger::DSL.current["paths"][path] ||= {}
Array(method).each do |single_method|
Swagger::DSL.current["paths"][path][single_method] = Swagger::DSL::Operation.new("#{name}##{action}.#{single_method}", format: format, &block)
end
end
|