lib/lotus/routing/http_router.rb in lotus-router-0.4.3 vs lib/lotus/routing/http_router.rb in lotus-router-0.5.0

- old
+ new

@@ -2,10 +2,11 @@ require 'lotus/utils/io' require 'lotus/routing/endpoint_resolver' require 'lotus/routing/route' require 'lotus/routing/parsers' require 'lotus/routing/force_ssl' +require 'lotus/routing/error' require 'lotus/utils/path_prefix' Lotus::Utils::IO.silence_warnings do HttpRouter::Route::VALID_HTTP_VERBS = %w{GET POST PUT PATCH DELETE HEAD OPTIONS TRACE} end @@ -15,11 +16,11 @@ # Invalid route # This is raised when the router fails to recognize a route, because of the # given arguments. # # @since 0.1.0 - class InvalidRouteException < ::StandardError + class InvalidRouteException < Lotus::Routing::Error end # HTTP router # # This implementation is based on ::HttpRouter (http_router gem). @@ -28,22 +29,33 @@ # public API from any future change of ::HttpRouter. # # @since 0.1.0 # @api private class HttpRouter < ::HttpRouter + # Script name - rack enviroment variable + # + # @since 0.5.0 + # @api private + SCRIPT_NAME = 'SCRIPT_NAME'.freeze + + # @since 0.5.0 + # @api private + attr_reader :namespace + # Initialize the router. # # @see Lotus::Router#initialize # # @since 0.1.0 # @api private def initialize(options = {}, &blk) super(options, &nil) - @default_scheme = options[:scheme] if options[:scheme] - @default_host = options[:host] if options[:host] - @default_port = options[:port] if options[:port] + @namespace = options[:namespace] if options[:namespace] + @default_scheme = options[:scheme] if options[:scheme] + @default_host = options[:host] if options[:host] + @default_port = options[:port] if options[:port] @route_class = options[:route] || Routing::Route @resolver = options[:resolver] || Routing::EndpointResolver.new(options) @parsers = Routing::Parsers.new(options[:parsers]) @prefix = Utils::PathPrefix.new(options[:prefix] || '') @force_ssl = Lotus::Routing::ForceSsl.new(!!options[:force_ssl], host: @default_host, port: @default_port) @@ -143,19 +155,27 @@ else @default_app.call(env) end end - private - def add_with_request_method(path, method, opts = {}, &app) - super.generate(@resolver, opts, &app) + # @api private + # @since 0.5.0 + def rewrite_path_info(env, request) + super + env[SCRIPT_NAME] = @prefix + env[SCRIPT_NAME] end + private + def _rescue_url_recognition yield rescue ::HttpRouter::InvalidRouteException, ::HttpRouter::TooManyParametersException => e - raise Routing::InvalidRouteException.new(e.message) + raise Routing::InvalidRouteException.new("#{ e.message } - please check given arguments") + end + + def add_with_request_method(path, method, opts = {}, &app) + super.generate(@resolver, opts, &app) end def _custom_path(uri_string) uri = URI.parse(uri_string) uri.path = @prefix.join(uri.path)