Module: Swagger::DSL::RailsController

Defined in:
lib/swagger/dsl/rails_controller.rb

Defined Under Namespace

Classes: NotExactMatch, NotMatch

Instance Method Summary collapse

Instance Method Details

#swagger(action, format = :json, path: nil, method: nil, &block) ⇒ Object Also known as: oas3



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/swagger/dsl/rails_controller.rb', line 10

def swagger(action, format = :json, path: nil, method: nil, &block)
  operation_id = "#{name}##{action}"

  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
  path ||= route.path.spec.to_s.sub("(.:format)", "").gsub(/:(\w+)/, "{\\1}")

  operation = Swagger::DSL::Operation.new(operation_id, format: format, &block)
  Swagger::DSL.current["paths"][path] ||= {}
  Swagger::DSL.current["paths"][path][method] = operation
end