lib/hanami/router.rb in hanami-router-2.0.0.alpha3 vs lib/hanami/router.rb in hanami-router-2.0.0.alpha4

- old
+ new

@@ -4,11 +4,11 @@ module Hanami # Rack compatible, lightweight and fast HTTP Router. # # @since 0.1.0 - class Router # rubocop:disable Metrics/ClassLength + class Router require "hanami/router/version" require "hanami/router/error" require "hanami/router/segment" require "hanami/router/redirect" require "hanami/router/prefix" @@ -46,10 +46,11 @@ # deployed # @param prefix [String] the relative URL prefix where the HTTP application # is deployed # @param resolver [#call(path, to)] a resolver for route entpoints # @param block_context [Hanami::Router::Block::Context) + # @param not_found [#call(env)] default handler when route is not matched # @param blk [Proc] the route definitions # # @since 0.1.0 # # @return [Hanami::Router] @@ -58,16 +59,17 @@ # require "hanami/router" # # Hanami::Router.new do # get "/", to: ->(*) { [200, {}, ["OK"]] } # end - def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, block_context: nil, &blk) + def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_found: NOT_FOUND, block_context: nil, &blk) # rubocop:disable Layout/LineLength # TODO: verify if Prefix can handle both name and path prefix @path_prefix = Prefix.new(prefix) @name_prefix = Prefix.new("") @url_helpers = UrlHelpers.new(base_url) @resolver = resolver + @not_found = not_found @block_context = block_context @fixed = {} @variable = {} @globbed = {} @mounted = {} @@ -84,11 +86,11 @@ def call(env) endpoint, params = lookup(env) unless endpoint return not_allowed(env) || - not_found + not_found(env) end endpoint.call( _params(env, params) ).to_a @@ -629,17 +631,18 @@ end # @since 2.0.0 # @api private def not_allowed(env) - (_not_allowed_fixed(env) || _not_allowed_variable(env)) and return [405, { "Content-Length" => "11" }, ["Not Allowed"]] + (_not_allowed_fixed(env) || + _not_allowed_variable(env)) and return [405, {"Content-Length" => "11"}, ["Not Allowed"]] end # @since 2.0.0 # @api private - def not_found - [404, { "Content-Length" => "9" }, ["Not Found"]] + def not_found(env) + @not_found.call(env) end protected # Fabricate Rack env for the given Rack env, path or named route @@ -653,11 +656,11 @@ # @since 0.5.0 # @api private # # @see Hanami::Router#recognize # @see http://www.rubydoc.info/github/rack/rack/Rack%2FMockRequest.env_for - def env_for(env, params = {}, options = {}) # rubocop:disable Metrics/MethodLength + def env_for(env, params = {}, options = {}) require "rack/mock" case env when ::String ::Rack::MockRequest.env_for(env, options) @@ -693,11 +696,17 @@ # @since 2.0.0 # @api private PARAMS = "router.params" + # Default response when no route was matched + # + # @api private # @since 2.0.0 + NOT_FOUND = ->(*) { [404, {"Content-Length" => "9"}, ["Not Found"]] }.freeze + + # @since 2.0.0 # @api private def lookup(env) endpoint = fixed(env) return [endpoint, {}] if endpoint @@ -787,10 +796,10 @@ body = Rack::Utils::HTTP_STATUS_CODES.fetch(code) do raise UnknownHTTPStatusCodeError.new(code) end destination = prefixed_path(to) - Redirect.new(destination, ->(*) { [code, { "Location" => destination }, [body]] }) + Redirect.new(destination, ->(*) { [code, {"Location" => destination}, [body]] }) end # @since 2.0.0 # @api private def _params(env, params)