Last active
December 29, 2015 11:03
-
-
Save srabuini/1f016abff2542f31eca4 to your computer and use it in GitHub Desktop.
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
class App | |
REQUEST_METHOD = 'REQUEST_METHOD'.freeze | |
PATH_INFO = 'PATH_INFO'.freeze | |
attr_reader :env | |
def initialize(routes) | |
@routes = routes | |
@h = Hart.new(@routes) | |
end | |
def call(env) | |
@env = env | |
controller_object = controller_class.new(env) | |
action_result = controller_object.send(action) | |
controller_object.res | |
end | |
private | |
def action | |
hart_result[1] | |
end | |
def controller | |
hart_result[0] | |
end | |
def controller_class | |
Object.const_get(controller) | |
end | |
def hart_result | |
@hart_result ||= @h.call(env[REQUEST_METHOD], env[PATH_INFO]).split('#') | |
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 'hart' | |
require_relative './app' | |
require_relative './controllers/controller' | |
require_relative './controllers/pages_controller' | |
routes = { | |
default: "PagesController#not_found", | |
GET: "PagesController#index", | |
"foo" => { | |
GET: "PagesController#foo" | |
} | |
} | |
run App.new(routes) |
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
class Controller | |
attr_reader :env, :req, :res | |
def initialize(env) | |
@env = env | |
@req = Rack::Request.new(@env) | |
@res = Rack::Response.new | |
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
class PagesController < Controller | |
def foo | |
res.write('foo!!') | |
end | |
def index | |
res.write('Hello!!') | |
end | |
def not_found | |
res.status = 404 | |
res.write('Not found!') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment