Sha256: 04d26fef9489322bcaa576549575545c6e8d20e721f3bc2cb210a696d67d6fa0

Contents?: true

Size: 1.14 KB

Versions: 14

Compression:

Stored size: 1.14 KB

Contents

module Rasti
  module Web
    class Router

      VERBS = %w(delete get head options patch post put).freeze

      NOT_FOUND_PATTERN = '/404'

      VERBS.each do |verb|
        define_method verb do |pattern, endpoint=nil, &block|
          routes[verb.upcase] << Route.new(pattern, endpoint, &block)
        end
      end

      def not_found(endpoint=nil, &block)
        @not_found_route = Route.new(NOT_FOUND_PATTERN, endpoint, &block)
      end

      def call(env)
        route = route_for env 
        env[ROUTE_PARAMS] = route.extract_params env['PATH_INFO']
        route.call env
      end

      def all_routes
        Hash[routes.map { |m,r| [m, r.map(&:pattern)] }]
      end

      private

      def routes
        @routes ||= Hash.new { |h,k| h[k] = [] }
      end

      def route_for(env)
        routes[env['REQUEST_METHOD'].upcase].detect { |r| r.match? env['PATH_INFO'] } || not_found_route
      end

      def not_found_route
        @not_found_route ||= Route.new NOT_FOUND_PATTERN do |request, response, render|
          render.status 404, "Not found: #{request.request_method} #{request.path_info}"
        end
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rasti-web-2.0.1 lib/rasti/web/router.rb
rasti-web-2.0.0 lib/rasti/web/router.rb
rasti-web-1.2.1 lib/rasti/web/router.rb
rasti-web-1.2.0 lib/rasti/web/router.rb
rasti-web-1.1.0 lib/rasti/web/router.rb
rasti-web-1.0.0 lib/rasti/web/router.rb
rasti-web-0.2.3 lib/rasti/web/router.rb
rasti-web-0.2.2 lib/rasti/web/router.rb
rasti-web-0.2.1 lib/rasti/web/router.rb
rasti-web-0.2.0 lib/rasti/web/router.rb
rasti-web-0.1.1 lib/rasti/web/router.rb
rasti-web-0.1.0 lib/rasti/web/router.rb
rasti-web-0.0.7 lib/rasti/web/router.rb
rasti-web-0.0.6 lib/rasti/web/router.rb