Sha256: af05cb6c57bd105b2883c30e3f03002bd404765878f707d4c9b6e33f9fced4fc

Contents?: true

Size: 1.37 KB

Versions: 15

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

class Roda
  module RodaPlugins
    # This is a simplified Roda plugin version of the Rack middleware:
    # https://github.com/rack/rack/blob/v2.2.2/lib/rack/method_override.rb
    module MethodOverride
      def self.configure(app, opts = {})
        app.opts[:method_override_param] = opts[:method_override_param] || "_method"
      end

      module RequestMethods
        HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS PATCH LINK UNLINK).freeze
        HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE"
        ALLOWED_METHODS = %w(POST).freeze

        def initialize(scope, env)
          super
          return unless _allowed_methods.include?(env[Rack::REQUEST_METHOD])

          method = _method_override(env)
          return unless HTTP_METHODS.include?(method)

          env[Rack::RACK_METHODOVERRIDE_ORIGINAL_METHOD] = env[Rack::REQUEST_METHOD]
          env[Rack::REQUEST_METHOD] = method
        end

        private

        def _method_override(env)
          method = _method_override_param ||
            env[HTTP_METHOD_OVERRIDE_HEADER]
          method.to_s.upcase
        end

        def _allowed_methods
          ALLOWED_METHODS
        end

        def _method_override_param
          params[scope.class.opts[:method_override_param]]
        end
      end
    end

    register_plugin :method_override, MethodOverride
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/roda/plugins/method_override.rb
bridgetown-core-2.0.0.beta2 lib/roda/plugins/method_override.rb
bridgetown-core-2.0.0.beta1 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.4 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.3 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.2 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.1 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.0 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.0.beta3 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.0.beta2 lib/roda/plugins/method_override.rb
bridgetown-core-1.3.0.beta1 lib/roda/plugins/method_override.rb
bridgetown-core-1.2.0 lib/roda/plugins/method_override.rb
bridgetown-core-1.2.0.beta5 lib/roda/plugins/method_override.rb
bridgetown-core-1.2.0.beta4 lib/roda/plugins/method_override.rb
bridgetown-core-1.2.0.beta3 lib/roda/plugins/method_override.rb