<commit_before>module Kemal
  class OverrideMethodHandler
    include HTTP::Handler
    INSTANCE = new

    ALLOWED_METHODS           = ["PUT", "PATCH", "DELETE"]
    OVERRIDE_METHOD           = "POST"
    OVERRIDE_METHOD_PARAM_KEY = "_method"

    def call(context)
      request = context.request
      if request.method == OVERRIDE_METHOD
        if context.params.body.has_key?(OVERRIDE_METHOD_PARAM_KEY) && override_method_valid?(context.params.body["_method"])
          request.method = context.params.body["_method"].upcase
        end
      end
      call_next(context)
    end

    def override_method_valid?(override_method : String)
      ALLOWED_METHODS.includes?(override_method.upcase)
    end
  end
end<commit_msg>Add docs to Kemal::OverrideMethodHandler<commit_after>module Kemal
  # Adds support for `_method` magic parameter to simulate PUT, PATCH, DELETE requests in an html form.
  #
  # This middleware is **not** in the default Kemal handlers. You need to explicitly add this to your handlers:
  #
  # ```ruby
  # add_handler Kemal::OverrideMethodHandler
  # ```
  #
  # **Important:** This middleware consumes `params.body` to read the `_method` magic parameter.
  class OverrideMethodHandler
    include HTTP::Handler
    INSTANCE = new

    ALLOWED_METHODS           = ["PUT", "PATCH", "DELETE"]
    OVERRIDE_METHOD           = "POST"
    OVERRIDE_METHOD_PARAM_KEY = "_method"

    def call(context)
      request = context.request
      if request.method == OVERRIDE_METHOD
        if context.params.body.has_key?(OVERRIDE_METHOD_PARAM_KEY) && override_method_valid?(context.params.body[OVERRIDE_METHOD_PARAM_KEY])
          request.method = context.params.body["_method"].upcase
        end
      end
      call_next(context)
    end

    private def override_method_valid?(override_method : String)
      ALLOWED_METHODS.includes?(override_method.upcase)
    end
  end
end
{'fraction_non_alphanumeric': 0.09100473435034193, 'fraction_numerical': 0.0, 'mean_word_length': 3.293453724604966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6943376', 'n_tokens_mistral': 591, 'n_tokens_neox': 573, 'n_words': 129}