class FourOFour

A small Rack middleware to handle 404 responses with a delegation

Constants

VERSION

Public Class Methods

new(app, delegation = nil, skip_formats = []) click to toggle source
# File lib/four_o_four.rb, line 7
def initialize(app, delegation = nil, skip_formats = [])
  @app = app
  @delegation = (delegation || "#{self.class}::DefaultApplication").to_s
  @skip_formats = Regexp.union(Array(skip_formats))
end

Public Instance Methods

call(env) click to toggle source
# File lib/four_o_four.rb, line 13
def call(env)
  status, headers, response = @app.call(env)

  if processable_request_method?(env) && !Utils::Asset.match(env['REQUEST_URI']) &&
     can_process_request?(status, headers)
    Object.const_get(@delegation).new.call(env)
  else
    [status, headers, response]
  end
end

Private Instance Methods

can_process_request?(status, headers) click to toggle source

Test if the current request can be processed by four_o_four. It will depend on the request and if any skipped formats have been set.

@param [Integer] status Status of the current request. @param [Rack::Utils::HeaderHash] headers Header of the current request. @return [Boolean]

@api private

# File lib/four_o_four.rb, line 49
def can_process_request?(status, headers)
  content_type = headers['Content-Type']

  404 == status && processable_type?(content_type)
end
processable_request_method?(env) click to toggle source

Return true if the request method is a GET

@param [Hash] env Rack environment @return [Boolean]

@api private

# File lib/four_o_four.rb, line 71
def processable_request_method?(env)
  env['REQUEST_METHOD'] == 'GET'
end
processable_type?(content_type) click to toggle source

Return true if the request type can be processed (try to match with possible skipped formats).

@param [String] content_type Header's content type value. @return [Boolean]

@api private

# File lib/four_o_four.rb, line 61
def processable_type?(content_type)
  content_type.nil? || (content_type && content_type !~ @skip_formats)
end