lib/grape/dsl/inside_route.rb in grape-0.9.0 vs lib/grape/dsl/inside_route.rb in grape-0.10.0

- old
+ new

@@ -2,10 +2,11 @@ module Grape module DSL module InsideRoute extend ActiveSupport::Concern + include Grape::DSL::Settings # A filtering method that will return a hash # consisting only of keys that have been declared by a # `params` statement against the current/target endpoint or parent # namespaces @@ -16,16 +17,16 @@ # you want only to return params declared against the current/target endpoint def declared(params, options = {}, declared_params = nil) options[:include_missing] = true unless options.key?(:include_missing) options[:include_parent_namespaces] = true unless options.key?(:include_parent_namespaces) if declared_params.nil? - declared_params = !options[:include_parent_namespaces] ? settings[:declared_params] : - settings.gather(:declared_params) + declared_params = (!options[:include_parent_namespaces] ? route_setting(:declared_params) : + (route_setting(:saved_declared_params) || [])).flatten(1) || [] end unless declared_params - raise ArgumentError, "Tried to filter for declared parameters but none exist." + fail ArgumentError, 'Tried to filter for declared parameters but none exist.' end if params.is_a? Array params.map do |param| declared(param || {}, options, declared_params) @@ -34,10 +35,13 @@ declared_params.inject({}) do |hash, key| key = { key => nil } unless key.is_a? Hash key.each_pair do |parent, children| output_key = options[:stringify] ? parent.to_s : parent.to_sym + + next unless options[:include_missing] || children || params[parent] + if params.key?(parent) || options[:include_missing] hash[output_key] = if children declared(params[parent] || {}, options, Array(children)) else params[parent] @@ -59,11 +63,11 @@ # end user with the specified message. # # @param message [String] The message to display. # @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set. def error!(message, status = nil, headers = nil) - self.status(status || settings[:default_error_status]) + self.status(status || namespace_inheritable(:default_error_status)) throw :error, message: message, status: self.status, headers: headers end # Redirect to a new url. # @@ -73,18 +77,18 @@ def redirect(url, options = {}) merged_options = { permanent: false }.merge(options) if merged_options[:permanent] status 301 else - if env['HTTP_VERSION'] == 'HTTP/1.1' && request.request_method.to_s.upcase != "GET" + if env['HTTP_VERSION'] == 'HTTP/1.1' && request.request_method.to_s.upcase != 'GET' status 303 else status 302 end end - header "Location", url - body "" + header 'Location', url + body '' end # Set or retrieve the HTTP status code. # # @param status [Integer] The HTTP Status Code to return for this request. @@ -144,10 +148,13 @@ # # GET /body # => "Body" def body(value = nil) if value @body = value + elsif value == false + @body = '' + status 204 else @body end end @@ -184,11 +191,16 @@ else object end representation = { root => representation } if root - representation = (@body || {}).merge(key => representation) if key + if key + representation = (@body || {}).merge(key => representation) + elsif entity_class.present? && representation.respond_to?('merge') + representation = (@body || {}).merge(representation) + end + body representation end # Returns route information for the current request. # @@ -197,11 +209,11 @@ # desc "Returns the route description." # get '/' do # route.route_description # end def route - env["rack.routing_args"][:route_info] + env['rack.routing_args'][:route_info] end def entity_class_for_obj(object, options) entity_class = options.delete(:with) @@ -212,10 +224,10 @@ else object.respond_to?(:first) ? object.first.class : object.class end object_class.ancestors.each do |potential| - entity_class ||= (settings[:representations] || {})[potential] + entity_class ||= (Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(:representations)) || {})[potential] end entity_class ||= object_class.const_get(:Entity) if object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent) end