Skip to content

Instantly share code, notes, and snippets.

@codesword
Created June 19, 2016 12:25
Show Gist options
  • Save codesword/673de6e3d5a9fc61eef2f7194f6bffec to your computer and use it in GitHub Desktop.
Save codesword/673de6e3d5a9fc61eef2f7194f6bffec to your computer and use it in GitHub Desktop.
Building An MVC Framework - Part 2
require "zucy/version"
module Zucy
class Application
def call(env)
@req = Rack::Request.new(env)
path = @req.path_info
request_method = @req.request_method.downcase
return [500, {}, []] if path == "/favicon.ico"
controller, action = get_controller_and_action_for(path, request_method)
response = controller.new.send(action)
[200, {"Content-Type" => "text/html"}, [response]]
end
def get_controller_and_action_for(path, verb)
_, controller, action, others = path.split("/", 4)
require "#{controller.downcase}_controller.rb"
controller = Object.const_get(controller.capitalize! + "Controller")
action = action.nil? ? verb : "#{verb}_#{action}"
[controller, action]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment