lib/grape-swagger/endpoint.rb in grape-swagger-0.23.0 vs lib/grape-swagger/endpoint.rb in grape-swagger-0.24.0
- old
+ new
@@ -1,5 +1,7 @@
+# frozen_string_literal: true
+
require 'active_support'
require 'active_support/core_ext/string/inflections.rb'
module Grape
class Endpoint
@@ -82,11 +84,11 @@
end
# path object
def path_item(routes, options)
routes.each do |route|
- next if hidden?(route)
+ next if hidden?(route, options)
@item, path = GrapeSwagger::DocMethods::PathString.build(route, options)
@entity = route.entity || route.options[:success]
verb, method_object = method_object(route, options, path)
@@ -106,18 +108,23 @@
method[:summary] = summary_object(route)
method[:description] = description_object(route, options[:markdown])
method[:produces] = produces_object(route, options[:produces] || options[:format])
method[:consumes] = consumes_object(route, options[:format])
method[:parameters] = params_object(route)
+ method[:security] = security_object(route)
method[:responses] = response_object(route, options[:markdown])
method[:tags] = tag_object(route)
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
method.delete_if { |_, value| value.blank? }
[route.request_method.downcase.to_sym, method]
end
+ def security_object(route)
+ route.options[:security] if route.options.key?(:security)
+ end
+
def summary_object(route)
summary = route.options[:desc] if route.options.key?(:desc)
summary = route.description if route.description.present?
summary = route.options[:summary] if route.options.key?(:summary)
@@ -213,19 +220,21 @@
def partition_params(route)
declared_params = route.settings[:declared_params] if route.settings[:declared_params].present?
required, exposed = route.params.partition { |x| x.first.is_a? String }
required = GrapeSwagger::DocMethods::Headers.parse(route) + required unless route.headers.nil?
+
default_type(required)
default_type(exposed)
- unless declared_params.nil? && route.headers.nil?
- request_params = parse_request_params(required)
- end
+ request_params = unless declared_params.nil? && route.headers.nil?
+ parse_request_params(required)
+ end || {}
- return route.params if route.params.present? && !route.settings[:declared_params].present?
- request_params || {}
+ request_params = route.params.merge(request_params) if route.params.present? && !route.settings[:declared_params].present?
+
+ request_params
end
def default_type(params)
params.each do |param|
param[-1] = param.last == '' ? { required: true, type: 'Integer' } : param.last
@@ -277,16 +286,26 @@
model_name
end
def model_name(name)
- name.respond_to?(:name) ? name.name.demodulize.camelize : name.split('::').last
+ if name.respond_to?(:entity_name)
+ name.entity_name
+ elsif name.to_s.end_with?('Entity', 'Entities')
+ length = 0
+ name.to_s.split('::')[0..-2].reverse.take_while do |x|
+ length += x.length
+ length < 42
+ end.reverse.join
+ else
+ name.name.demodulize.camelize
+ end
end
- def hidden?(route)
+ def hidden?(route, options)
route_hidden = route.options[:hidden]
- route_hidden = route_hidden.call if route_hidden.is_a?(Proc)
- route_hidden
+ return route_hidden unless route_hidden.is_a?(Proc)
+ options[:oauth_token] ? route_hidden.call(send(options[:oauth_token].to_sym)) : route_hidden.call
end
def public_parameter?(param)
param_options = param.last
return true unless param_options.key?(:documentation) && !param_options[:required]