Sha256: 684cf7d8a0c31acb93b3a55bf840e94275b0a56176297bbca8bc209e4a5bcc69

Contents?: true

Size: 1.51 KB

Versions: 30

Compression:

Stored size: 1.51 KB

Contents

# frozen-string-literal: true

#
class Roda
  module RodaPlugins
    # The run_append_slash plugin makes +r.run+ use +/+ as the +PATH_INFO+
    # when calling the rack application if +PATH_INFO+ would be empty.
    # Example:
    #
    #   route do |r|
    #     r.on "a" do
    #       r.run App
    #     end
    #   end
    #
    #   # without run_append_slash: 
    #   # GET /a => App gets "" as PATH_INFO
    #   # GET /a/ => App gets "/" as PATH_INFO
    #
    #   # with run_append_slash: 
    #   # GET /a => App gets "/" as PATH_INFO
    #   # GET /a/ => App gets "/" as PATH_INFO
    module RunAppendSlash
      # Set plugin specific options.  Options:
      # :use_redirects :: Whether to issue 302 redirects when appending the
      #                   trailing slash.
      def self.configure(app, opts=OPTS)
        app.opts[:run_append_slash_redirect] = !!opts[:use_redirects]
      end

      module RequestMethods
        # Calls the given rack app. If the path matches the root of the app but
        # does not contain a trailing slash, a trailing slash is appended to the
        # path internally, or a redirect is issued when configured with
        # <tt>use_redirects: true</tt>.
        def run(*)
          if @remaining_path.empty?
            if scope.opts[:run_append_slash_redirect]
              redirect("#{path}/")
            else
              @remaining_path += '/'
            end
          end
          super
        end
      end
    end

    register_plugin(:run_append_slash, RunAppendSlash)
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
roda-3.86.0 lib/roda/plugins/run_append_slash.rb
roda-3.85.0 lib/roda/plugins/run_append_slash.rb
roda-3.84.0 lib/roda/plugins/run_append_slash.rb
roda-3.83.0 lib/roda/plugins/run_append_slash.rb
roda-3.82.0 lib/roda/plugins/run_append_slash.rb
roda-3.81.0 lib/roda/plugins/run_append_slash.rb
roda-3.79.0 lib/roda/plugins/run_append_slash.rb
roda-3.78.0 lib/roda/plugins/run_append_slash.rb
roda-3.77.0 lib/roda/plugins/run_append_slash.rb
roda-3.76.0 lib/roda/plugins/run_append_slash.rb
roda-3.75.0 lib/roda/plugins/run_append_slash.rb
roda-3.74.0 lib/roda/plugins/run_append_slash.rb
roda-3.73.0 lib/roda/plugins/run_append_slash.rb
roda-3.72.0 lib/roda/plugins/run_append_slash.rb
roda-3.71.0 lib/roda/plugins/run_append_slash.rb
roda-3.70.0 lib/roda/plugins/run_append_slash.rb
roda-3.69.0 lib/roda/plugins/run_append_slash.rb
roda-3.68.0 lib/roda/plugins/run_append_slash.rb
roda-3.67.0 lib/roda/plugins/run_append_slash.rb
roda-3.66.0 lib/roda/plugins/run_append_slash.rb