lib/hanami/router.rb in hanami-router-2.0.2 vs lib/hanami/router.rb in hanami-router-2.1.0.beta1

- old
+ new

@@ -70,17 +70,18 @@ # 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, not_found: NOT_FOUND, block_context: nil, inspector: nil, &blk) # rubocop:disable Layout/LineLength + def initialize(base_url: DEFAULT_BASE_URL, prefix: DEFAULT_PREFIX, resolver: DEFAULT_RESOLVER, not_allowed: NOT_ALLOWED, not_found: NOT_FOUND, block_context: nil, inspector: 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) @base_url = base_url @resolver = resolver + @not_allowed = not_allowed @not_found = not_found @block_context = block_context @fixed = {} @variable = {} @globbed = {} @@ -99,12 +100,11 @@ # @since 0.1.0 def call(env) endpoint, params = lookup(env) unless endpoint - return not_allowed(env) || - not_found(env) + return not_allowed(env) || not_found(env) end endpoint.call( _params(env, params) ).to_a @@ -145,11 +145,11 @@ # "Hello from Hanami!" # end # end # # router.path(:root) # => "/" - # router.url(:root) # => "https://hanamirb.org" + # router.url(:root) # => #<URI::HTTPS https://hanamirb.org> def root(to: nil, &blk) get(ROOT_PATH, to: to, as: :root, &blk) end # Defines a route that accepts GET requests for the given path. @@ -189,11 +189,11 @@ # router = Hanami::Router.new do # get "/", to: ->(*) { [200, {}, ["OK"]] }, as: :welcome # end # # router.path(:welcome) # => "/" - # router.url(:welcome) # => "http://localhost/" + # router.url(:welcome) # => #<URI::HTTP http://localhost/> # # @example Constraints # require "hanami/router" # # Hanami::Router.new do @@ -464,11 +464,11 @@ # The additional arguments will be used to compose the relative URL - in # case it has tokens to match - and for compose the query string. # # @param name [Symbol] the route name # - # @return [String] + # @return [URI::HTTP, URI::HTTPS] # # @raise [Hanami::Router::MissingRouteError] when the router fails to # recognize a route, because of the given arguments. # # @since 0.1.0 @@ -481,13 +481,13 @@ # router = Hanami::Router.new(base_url: "https://hanamirb.org") do # get "/login", to: ->(*) { ... }, as: :login # get "/:name", to: ->(*) { ... }, as: :framework # end # - # router.url(:login) # => "https://hanamirb.org/login" - # router.url(:login, return_to: "/dashboard") # => "https://hanamirb.org/login?return_to=%2Fdashboard" - # router.url(:framework, name: "router") # => "https://hanamirb.org/router" + # router.url(:login) # => #<URI::HTTPS https://hanamirb.org/login> + # router.url(:login, return_to: "/dashboard") # => #<URI::HTTPS https://hanamirb.org/login?return_to=%2Fdashboard> + # router.url(:framework, name: "router") # => #<URI::HTTPS https://hanamirb.org/router> def url(name, variables = {}) url_helpers.url(name, variables) end # Recognize the given env, path, or name and return a route for testing @@ -650,19 +650,14 @@ end # @since 2.0.0 # @api private def not_allowed(env) - http_methods = _not_allowed_fixed(env) || _not_allowed_variable(env) - return if http_methods.nil? + allowed_http_methods = _not_allowed_fixed(env) || _not_allowed_variable(env) + return if allowed_http_methods.nil? - [HTTP_STATUS_NOT_ALLOWED, - { - ::Rack::CONTENT_LENGTH => HTTP_BODY_NOT_ALLOWED_LENGTH, - "Allow" => http_methods.join(", ") - }, - [HTTP_BODY_NOT_ALLOWED]] + @not_allowed.call(env, allowed_http_methods) end # @since 2.0.0 # @api private def not_found(env) @@ -774,10 +769,25 @@ # @since 2.0.0 # @api private ROUTE_GLOBBED_MATCHER = /\*/ + # Default response when the route method was not allowed + # + # @api private + # @since 2.1.0 + NOT_ALLOWED = -> (_, allowed_http_methods) { + [ + HTTP_STATUS_NOT_ALLOWED, + { + ::Rack::CONTENT_LENGTH => HTTP_BODY_NOT_ALLOWED_LENGTH, + "Allow" => allowed_http_methods.join(", ") + }, + [HTTP_BODY_NOT_ALLOWED] + ] + } + # Default response when no route was matched # # @api private # @since 2.0.0 NOT_FOUND = ->(*) { @@ -788,11 +798,11 @@ # @api private def lookup(env) endpoint = fixed(env) return [endpoint, {}] if endpoint - variable(env) || globbed(env) || mounted(env) + variable(env) || mounted(env) || globbed(env) end # @since 2.0.0 # @api private def add_route(http_method, path, to, as, constraints, &blk) @@ -898,9 +908,10 @@ def with(**new_options, &blk) options = { base_url: @base_url, prefix: @path_prefix.to_s, resolver: @resolver, + not_allowed: @not_allowed, not_found: @not_found, block_context: @block_context, inspector: @inspector }