Sha256: bb9fa4535afee94378ff5a2df8221b84ce58c27dd86f172a7016b2c751617f98

Contents?: true

Size: 1.49 KB

Versions: 18

Compression:

Stored size: 1.49 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The run_handler plugin allows r.run to take a block, which is yielded 
    # the rack response array, before it returns it as a response.
    #
    # Additionally, r.run also takes a options hash, and you can provide a
    # <tt>:not_found=>:pass</tt> option to keep routing normally if the rack
    # app returns a 404 response.
    #
    #
    #   plugin :run_handler
    #
    #   route do |r|
    #     r.on 'a' do
    #       # Keep running code if RackAppFoo doesn't return a result
    #       r.run RackAppFoo, :not_found=>:pass
    #
    #       # Change response status codes before returning.
    #       r.run(RackAppBar) do |response|
    #         response[0] = 200 if response[0] == 201
    #       end
    #     end
    #   end
    module RunHandler
      OPTS = {}.freeze

      module RequestMethods
        # If a block is given, yield the rack response array to it.  The response can
        # be modified before it is returned by the current app.
        # 
        # If the <tt>:not_found=>:pass</tt> option is given, and the rack response
        # returned by the app is a 404 response, do not return the response, continue
        # routing normally.
        def run(app, opts=OPTS)
          res = catch(:halt){super(app)}
          yield res if block_given?
          throw(:halt, res) unless opts[:not_found] == :pass && res[0] == 404
        end
      end
    end

    register_plugin(:run_handler, RunHandler)
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
roda-2.28.0 lib/roda/plugins/run_handler.rb
roda-2.27.0 lib/roda/plugins/run_handler.rb
roda-2.26.0 lib/roda/plugins/run_handler.rb
roda-2.25.0 lib/roda/plugins/run_handler.rb
roda-2.24.0 lib/roda/plugins/run_handler.rb
roda-2.23.0 lib/roda/plugins/run_handler.rb
roda-2.22.0 lib/roda/plugins/run_handler.rb
roda-2.21.0 lib/roda/plugins/run_handler.rb
roda-2.20.0 lib/roda/plugins/run_handler.rb
roda-2.19.0 lib/roda/plugins/run_handler.rb
roda-2.18.0 lib/roda/plugins/run_handler.rb
roda-2.17.0 lib/roda/plugins/run_handler.rb
roda-2.16.0 lib/roda/plugins/run_handler.rb
roda-2.15.0 lib/roda/plugins/run_handler.rb
roda-2.14.0 lib/roda/plugins/run_handler.rb
roda-2.13.0 lib/roda/plugins/run_handler.rb
roda-2.12.0 lib/roda/plugins/run_handler.rb
roda-2.11.0 lib/roda/plugins/run_handler.rb