Sha256: 406f7bf5eb1a827064d9aaa3b4df7090c19893c103aca6cf1ea9dff10b10131e

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'rapid_runty/router/routes'

module RapidRunty
  class Application
    attr_reader :routes

    def initialize
      @routes = RapidRunty::Router::Routes.new
    end

    # Core response method. Process the request and return the correct
    # response or status message.
    #
    # @param env
    # @param [Rack::Request] request
    # @param [Rack::Response] response
    def handle(env, request, response)
      verb, path = route_args(request).values

      route = routes.find_route(verb, path)
      if route.nil?
        not_found(response, path)
      else
        param = "&#{Rack::Utils.build_nested_query(route.placeholders)}"
        env['QUERY_STRING'] << param
        env.merge!(route.options)
        response.write dispatch(env, route, response)
      end
    end

    ##
    # Dispatch the Controller and it's action to be rendered
    def dispatch(env, route, response)
      kontroller, action = route.options.values

      controller = Object.const_get("#{kontroller.camel_case}Controller")
      controller.new(env, response).public_send(action)
    end

    ##
    # Default 404 error
    #
    # @param [Rack::Response]
    #
    # @return [Rack::Response]
    def not_found(response, path)
      response.status = 404
      response.write "
      <html>
        <head>
          <body>
            <h1>404 Page not found for #{path}</h1>
          </body>
        </head>
      </html>
      "
    end

    private

    def route_args(request)
      {
        verb: request.request_method.downcase.to_sym,
        path: Rack::Utils.unescape(request.path_info)
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rapid_runty-0.1.3 lib/rapid_runty/router/base_route.rb