lib/grape/dsl/inside_route.rb in grape-0.16.2 vs lib/grape/dsl/inside_route.rb in grape-0.17.0
- old
+ new
@@ -27,14 +27,17 @@
# has completed
module PostBeforeFilter
def declared(params, options = {}, declared_params = nil)
options = options.reverse_merge(include_missing: true, include_parent_namespaces: true)
- declared_params ||= (!options[:include_parent_namespaces] ? route_setting(:declared_params) : (route_setting(:saved_declared_params) || [])).flatten(1) || []
+ all_declared_params = route_setting(:declared_params)
+ current_namespace_declared_params = route_setting(:saved_declared_params).last
- fail ArgumentError, 'Tried to filter for declared parameters but none exist.' unless declared_params
+ declared_params ||= options[:include_parent_namespaces] ? all_declared_params : current_namespace_declared_params
+ raise ArgumentError, 'Tried to filter for declared parameters but none exist.' unless declared_params
+
if params.is_a? Array
params.map do |param|
declared(param || {}, options, declared_params)
end
else
@@ -68,11 +71,11 @@
# @param params [Hash] The initial hash to filter. Usually this will just be `params`
# @param options [Hash] Can pass `:include_missing`, `:stringify` and `:include_parent_namespaces`
# options. `:include_parent_namespaces` defaults to true, hence must be set to false if
# you want only to return params declared against the current/target endpoint.
def declared(*)
- fail MethodNotYetAvailable, '#declared is not available prior to parameter validation.'
+ raise MethodNotYetAvailable, '#declared is not available prior to parameter validation.'
end
# The API version as specified in the URL.
def version
env[Grape::Env::API_VERSION]
@@ -98,18 +101,16 @@
permanent = options.fetch(:permanent, false)
body_message = options.fetch(:body, nil)
if permanent
status 301
body_message ||= "This resource has been moved permanently to #{url}."
+ elsif env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET
+ status 303
+ body_message ||= "An alternate resource is located at #{url}."
else
- if env[Grape::Http::Headers::HTTP_VERSION] == 'HTTP/1.1' && request.request_method.to_s.upcase != Grape::Http::Headers::GET
- status 303
- body_message ||= "An alternate resource is located at #{url}."
- else
- status 302
- body_message ||= "This resource has been moved temporarily to #{url}."
- end
+ status 302
+ body_message ||= "This resource has been moved temporarily to #{url}."
end
header 'Location', url
content_type 'text/plain'
body body_message
end
@@ -118,15 +119,12 @@
#
# @param status [Integer] The HTTP Status Code to return for this request.
def status(status = nil)
case status
when Symbol
- if Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
- @status = Rack::Utils.status_code(status)
- else
- fail ArgumentError, "Status code :#{status} is invalid."
- end
+ raise ArgumentError, "Status code :#{status} is invalid." unless Rack::Utils::SYMBOL_TO_STATUS_CODE.keys.include?(status)
+ @status = Rack::Utils.status_code(status)
when Fixnum
@status = status
when nil
return @status if @status
case request.request_method.to_s.upcase
@@ -134,11 +132,11 @@
201
else
200
end
else
- fail ArgumentError, 'Status code must be Fixnum or Symbol.'
+ raise ArgumentError, 'Status code must be Fixnum or Symbol.'
end
end
# Set response content-type
def content_type(val = nil)
@@ -258,10 +256,10 @@
representation = { root => representation } if root
if key
representation = (@body || {}).merge(key => representation)
elsif entity_class.present? && @body
- fail ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
+ raise ArgumentError, "Representation of type #{representation.class} cannot be merged." unless representation.respond_to?(:merge)
representation = @body.merge(representation)
end
body representation
end