lib/helmet/api.rb in helmet-0.0.6 vs lib/helmet/api.rb in helmet-0.1.0

- old
+ new

@@ -1,39 +1,32 @@ require 'goliath/api' -require 'helmet/templates' +require 'helmet/handler' module Helmet class API < Goliath::API - - # Handle application routes - @@routes = {} - # Handle before filters - @@before_filters = [] - class << self - include Templates - - def public_folder=(folder) - @@public_folder = folder + + def set(key, value) + @config[key.to_sym] = value end - def public_folder - @@public_folder + def config(key) + @config[key.to_sym] end - - def views_folder=(folder) - @@views_folder = folder + + def before(route, &block) + @before_filters << [route, block] end - - def views_folder - @@views_folder + + def routes + @routes end - def before(route, &block) - @@before_filters << [route, block] + def before_filters + @before_filters end def get(route, &block) register_route('GET', route, &block); register_route('HEAD', route, &block); @@ -43,62 +36,80 @@ def delete(route, &block) register_route('DELETE', route, &block); end def head(route, &block) register_route('HEAD', route, &block); end def register_route(method, route, &block) sig = API.signature(method, route) - @@routes[sig] = block + @routes[sig] = block end - + def signature(method, route) "#{method}#{route}" end def inherited(klass) + klass.init + # setup basic middlewares setup_middlewares klass # compute public/ views folder - base = File.dirname(caller.first[/^[^:]*/]) - self.public_folder = File.join(base, 'public') - self.views_folder = File.join(base, 'views') + base = File.expand_path(File.dirname(caller.first[/^[^:]*/])) + klass.set :public_folder, File.join(base, 'public') + klass.set :views_folder, File.join(base, 'views') super # update Goliath::Application.app_class end - private + def init + # Handle application routes + @routes = {} + # Handle before filters + @before_filters = [] + + @config = {} + end + + private + def setup_middlewares(klass) # support for session klass.use Rack::Session::Cookie # support for forms klass.use Goliath::Rack::Params end end def response(env) - path = env['REQUEST_PATH'] + # request path + path = env['REQUEST_PATH'] + + # request handler + handler = Handler.new(env) - # evaluate filters - resp = catch(:halt) do - @@before_filters.each do |route| + catch(:halt) do + # evaluate filters + self.class.before_filters.each do |route| case route.first when String - route[1].call(env) if route.first == path + handler.handle! &route[1] if route.first == path when Regexp - route[1].call(env) if route.first =~ path + handler.handle! &route[1] if route.first =~ path end end - nil + + sig = API.signature(env['REQUEST_METHOD'], path) + block = self.class.routes[sig] + if block + handler.handle!(&block) + else + handler.handle! do + status 404 + 'not found!' + end + end end - return resp if resp - - sig = API.signature(env['REQUEST_METHOD'], path) - block = @@routes[sig] - if block - block.call env - else - [404, {}, 'not found!'] - end + handler.response.format_response end end end \ No newline at end of file