Sha256: 13e87edfc252ae18faf9ff6c7ef6db58a97b932aa9ab7a332139718faedf3b34

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

module Spire
  class Router
    def initialize(base_path, routes)
      @base_path = base_path
      @routes = routes
    end

    def disect(env)
      req = env['REQUEST_PATH'].split("/")
      @request = {}
      @request[:controller] = req[1]
      @request[:action] = req[2]
    end

    def route
      if @request[:controller] == nil
         route = @routes['default'].split("#")
         @controller_value = route[0].capitalize
         if @request[:action] == nil 
           @action_value = "index"
         else
           @action_value = route[1]
         end
      else
        @routes.each do |key, value|
          keys = key.split("/")
          values = value.split("#")
          if @request[:controller] == keys[1] && @request[:action] == keys[3]
            @controller_value = values[0].capitalize
            @action_value = values[1]
          elsif @request[:controller] == keys[1]
            @controller_value = values[0].capitalize
            @action_value = values[1]        
          end
        end
      end
        unless @controller_value
          return Response.new('404 - Page or route not found!')
        end
        return self.run
    end 

    def run
      require "#{@base_path}/controllers/#{@controller_value}Controller"
      @app = Kernel.const_get(@controller_value).new
      return @app.method(@action_value).call
    end

    def call(env)
      self.disect(env)
      response = self.route
      puts response
      if response
        response.to_rack
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spire-0.1.8 lib/spire/router.rb
spire-0.1.7 lib/spire/router.rb
spire-0.1.6 lib/spire/router.rb
spire-0.1.5 lib/spire/router.rb
spire-0.1.4 lib/spire/router.rb
spire-0.1.3 lib/spire/router.rb