class RouteDowncaser::DowncaseRouteMiddleware

Public Class Methods

new(app) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 6
def initialize(app)
  @app = app
end

Public Instance Methods

_call(env) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 14
def _call(env)
  request_uri = env['REQUEST_URI']
  path_info = env['PATH_INFO']

  # Don't touch anything, if uri/path is part of exclude_patterns
  return @app.call(env) if excluded?([request_uri, path_info])

  # Downcase request_uri and/or path_info if applicable
  request_uri = downcased_uri(request_uri)
  path_info = downcased_uri(path_info)

  # If redirect configured, then return redirect request,
  # if either request_uri or path_info has changed
  if RouteDowncaser.redirect && env['REQUEST_METHOD'] == 'GET'
    if request_uri.present? && request_uri != env['REQUEST_URI']
      return redirect_header(request_uri)
    end

    if path_info.present? && path_info != env['PATH_INFO']
      return redirect_header(path_info)
    end
  end

  env['PATH_INFO'] = path_info.to_s if path_info
  env['REQUEST_URI'] = request_uri.to_s if request_uri

  # Default just move to next chain in Rack callstack
  # calling with downcased uri if needed
  @app.call(env)
end
call(env) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 10
def call(env)
  dup._call(env)
end

Private Instance Methods

downcased_uri(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 57
def downcased_uri(uri)
  return nil unless uri.present?
  if has_querystring?(uri)
    "#{path(uri).mb_chars.downcase}?#{querystring(uri)}"
  else
    path(uri).mb_chars.downcase
  end
end
exclude_patterns_match?(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 47
def exclude_patterns_match?(uri)
  uri.match(Regexp.union(RouteDowncaser.exclude_patterns)) if uri && RouteDowncaser.exclude_patterns
end
excluded?(paths) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 51
def excluded?(paths)
  paths.any? do |path|
    exclude_patterns_match?(path)
  end
end
has_querystring?(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 74
def has_querystring?(uri)
  uri_items(uri).length > 1
end
path(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 66
def path(uri)
  uri_items(uri).first
end
querystring(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 70
def querystring(uri)
  uri_items(uri).last
end
redirect_header(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 82
def redirect_header(uri)
  [301, { 'Location' => uri.to_s, 'Content-Type' => 'text/html' }, []]
end
uri_items(uri) click to toggle source
# File lib/route_downcaser/downcase_route_middleware.rb, line 78
def uri_items(uri)
  uri.split('?', 2)
end