module StrongRoutes
  class Allow
    def initialize(app, options = {})
      @app = app
      @options = options
    end

    def call(env)
      return @app.call(env) unless enabled?

      request = ::Rack::Request.new(env)

      if allowed?(request)
        @app.call(env)
      else
        [ 404, { "Content-Type" => "text/html", "Content-Length" => config.message.length }, [ config.message ] ]
      end
    end

  private

    def allowed_routes
      @allowed_routes ||= begin
        routes = [ config.allowed_routes ]
        routes.flatten!
        routes.compact!
        routes.uniq!
        routes
      end
    end

    def allowed?(request)
      route_matchers.any? { |route_matcher| route_matcher =~ request.path_info }
    end

    def config
      @config ||= StrongRoutes.config.merge(@options)
    end

    def enabled?
      config.enabled?
    end

    def route_matchers
      @route_matchers ||= allowed_routes.map { |allowed_route| ::StrongRoutes::RouteMatcher.new(allowed_route) }
    end
  end
end

module StrongRoutes
  class Allow
    def initialize(app, options = {})
      @app = app
      @options = options
    end

    def call(env)
      return @app.call(env) unless enabled?

      request = ::Rack::Request.new(env)

      if allowed?(request)
        @app.call(env)
      else
        [ 404, { "Content-Type" => "text/html", "Content-Length" => config.message.length.to_s }, [ config.message ] ]
      end
    end

  private

    def allowed_routes
      @allowed_routes ||= begin
        routes = [ config.allowed_routes ]
        routes.flatten!
        routes.compact!
        routes.uniq!
        routes
      end
    end

    def allowed?(request)
      route_matchers.any? { |route_matcher| route_matcher =~ request.path_info }
    end

    def config
      @config ||= StrongRoutes.config.merge(@options)
    end

    def enabled?
      config.enabled?
    end

    def route_matchers
      @route_matchers ||= allowed_routes.map { |allowed_route| ::StrongRoutes::RouteMatcher.new(allowed_route) }
    end
  end
end

Return the content length as a string
Return the content length as a string

All HTTP headers must be strings. Newer versions of WEBrick/Thin choke
on the header value if it's an integer. Guessing that older versions of
Rack or Rails took care of coercing this for you.
