Created
July 21, 2012 20:22
-
-
Save erikeldridge/3157042 to your computer and use it in GitHub Desktop.
yet another rack router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'router' | |
use Rack::Static, :urls => ['/css', '/js'], :root => "static" | |
run Router.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /controllers/default.rb | |
module Controllers | |
class Default | |
def self.default env | |
[200, {'content-type' => 'text/html'}, "default text"] | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
# Instantiating the Router class does a couple things: | |
# 1. read route definition file, and generate case/when logic | |
# 2. define a "call" function (called by Rack) that calls the appropriate controller.action pair | |
# | |
# Routes are defined as "<path pattern> <controller name> <action name>" | |
# | |
# Example routes.yml | |
# # pattern Controller action | |
# - /user/\d+ User note | |
# - /users User index | |
# - / Default default | |
# | |
# Controllers are assumed to be wrapped in a "Controllers" module | |
# Actions are assumed to be static, and accept the env object | |
# | |
# For example, | |
# module Controllers | |
# class Foo | |
# def static.default env | |
# [200, {'content-type' => 'text/html'}, "foo"] | |
# end | |
# end | |
# end | |
require 'yaml' | |
require './controllers.rb' | |
class Router | |
def initialize | |
whens = '' | |
YAML.load_file('routes.yml').each do |route| | |
pattern, controller, action = route.split /\s+/ | |
whens += <<-EOS | |
when %r{#{pattern}} | |
controller = Controllers::#{controller}.new env | |
controller.#{action} | |
controller.response | |
EOS | |
end | |
Router.class_eval <<-EOS | |
def call env | |
case env['REQUEST_PATH'] | |
#{whens} | |
end | |
end | |
EOS | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pattern Controller action | |
- /user/\d+ User find | |
- /user User default | |
- / Default default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /controllers/user.rb | |
module Controllers | |
class User | |
def self.default env | |
[200, {'content-type' => 'text/html'}, "User#default"] | |
end | |
def self.find env | |
[200, {'content-type' => 'text/html'}, "User#find"] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment