lib/grape/dsl/inside_route.rb in grape-1.3.3 vs lib/grape/dsl/inside_route.rb in grape-1.4.0

- old
+ new

@@ -122,14 +122,14 @@ end def optioned_declared_params(**options) declared_params = if options[:include_parent_namespaces] # Declared params including parent namespaces - route_setting(:saved_declared_params).flatten | Array(route_setting(:declared_params)) + route_setting(:declared_params) else # Declared params at current namespace - route_setting(:saved_declared_params).last & Array(route_setting(:declared_params)) + namespace_stackable(:declared_params).last || [] end raise ArgumentError, 'Tried to filter for declared parameters but none exist.' unless declared_params declared_params end @@ -277,27 +277,40 @@ def return_no_content status 204 body false end - # Allows you to define the response as a file-like object. + # Deprecated method to send files to the client. Use `sendfile` or `stream` + def file(value = nil) + if value.is_a?(String) + warn '[DEPRECATION] Use sendfile or stream to send files.' + sendfile(value) + elsif !value.is_a?(NilClass) + warn '[DEPRECATION] Use stream to use a Stream object.' + stream(value) + else + warn '[DEPRECATION] Use sendfile or stream to send files.' + sendfile + end + end + + # Allows you to send a file to the client via sendfile. # # @example # get '/file' do - # file FileStreamer.new(...) + # sendfile FileStreamer.new(...) # end # # GET /file # => "contents of file" - def file(value = nil) + def sendfile(value = nil) if value.is_a?(String) - file_body = Grape::ServeFile::FileBody.new(value) - @file = Grape::ServeFile::FileResponse.new(file_body) + file_body = Grape::ServeStream::FileBody.new(value) + @stream = Grape::ServeStream::StreamResponse.new(file_body) elsif !value.is_a?(NilClass) - warn '[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.' - @file = Grape::ServeFile::FileResponse.new(value) + raise ArgumentError, 'Argument must be a file path' else - instance_variable_defined?(:@file) ? @file : nil + stream end end # Allows you to define the response as a streamable object. # @@ -316,10 +329,19 @@ # * https://github.com/rack/rack/blob/99293fa13d86cd48021630fcc4bd5acc9de5bdc3/lib/rack/etag.rb def stream(value = nil) header 'Content-Length', nil header 'Transfer-Encoding', nil header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front) - file(value) + if value.is_a?(String) + file_body = Grape::ServeStream::FileBody.new(value) + @stream = Grape::ServeStream::StreamResponse.new(file_body) + elsif value.respond_to?(:each) + @stream = Grape::ServeStream::StreamResponse.new(value) + elsif !value.is_a?(NilClass) + raise ArgumentError, 'Stream object must respond to :each.' + else + instance_variable_defined?(:@stream) ? @stream : nil + end end # Allows you to make use of Grape Entities by setting # the response body to the serializable hash of the # entity provided in the `:with` option. This has the