lib/helmet/api.rb in helmet-0.0.3 vs lib/helmet/api.rb in helmet-0.0.4

- old
+ new

@@ -2,12 +2,17 @@ require 'helmet/templates' module Helmet class API < Goliath::API - @@routes = {} + # Handle application routes + @@routes = {} + + # Handle before filters + @@before_filters = [] + class << self include Templates def public_folder=(folder) @@public_folder = folder @@ -22,11 +27,15 @@ end def views_folder @@views_folder end - + + def before(route, &block) + @@before_filters << [route, block] + end + def get(route, &block) register_route('GET', route, &block); register_route('HEAD', route, &block); end def post(route, &block) register_route('POST', route, &block); end @@ -64,10 +73,27 @@ klass.use Goliath::Rack::Params end end def response(env) - sig = API.signature(env['REQUEST_METHOD'], env['REQUEST_PATH']) + path = env['REQUEST_PATH'] + + # evaluate filters + resp = catch(:halt) do + @@before_filters.each do |route| + case route.first + when String + route[1].call if route.first == path + when Regexp + route[1].call if route.first =~ path + end + end + nil + end + p resp + return resp if resp + + sig = API.signature(env['REQUEST_METHOD'], path) block = @@routes[sig] if block block.call env else [404, {}, 'not found!'] \ No newline at end of file