Sha256: 02e2f4c0b956418cd6d3330c5735444a85d713153156267f9633c4fba4c49fa7

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module RouteDowncaser
  class DowncaseRouteMiddleware
    def initialize(app)
      @app = app
    end

    def call(env)
      @env = env

      if env['REQUEST_URI']
        if RouteDowncaser.redirect == true
          if path != path.downcase
            return [301, {'Location' => downcased_uri, 'Content-Type' => 'text/html'}, []]
          end
        else
          env['REQUEST_URI'] = downcased_uri
        end
      end

      if env['PATH_INFO'] =~ /assets\//i
        pieces = env['PATH_INFO'].split('/')
        env['PATH_INFO'] = pieces.slice(0..-2).join('/').downcase + '/' + pieces.last
      elsif env['PATH_INFO']
        env['PATH_INFO'] = env['PATH_INFO'].downcase
      end

      @app.call(env)
    end

    private

    def uri_items
      @env['REQUEST_URI'].split('?')
    end

    def path
      uri_items[0]
    end

    def query?
      uri_items.length > 1
    end

    def downcased_uri
      if query?
        "#{path.downcase!}?#{uri_items[1]}"
      else
        path.downcase!
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
route_downcaser-0.2.2 lib/route_downcaser/downcase_route_middleware.rb