lib/helmet/api.rb in helmet-0.2.0 vs lib/helmet/api.rb in helmet-0.2.1

- old
+ new

@@ -2,24 +2,16 @@ require 'goliath/constants' require 'http_router' require 'helmet/handler' -require 'pry' - module Helmet class API < Goliath::API class << self - - def set(key, value) - @config[key.to_sym] = value - end - def config(key) - @config[key.to_sym] - end + attr_accessor :settings def before(route, opts = {}, &block) @before_filters.add(route, opts, &block) end @@ -76,12 +68,13 @@ # setup basic middlewares setup_middlewares klass # compute public/ views folder base = File.expand_path(File.dirname(caller.first[/^[^:]*/])) - klass.set :public_folder, File.join(base, 'public') - klass.set :views_folder, File.join(base, 'views') + + klass.settings[:public_folder] = File.join(base, 'public') + klass.settings[:views_folder] = File.join(base, 'views') super # update Goliath::Application.app_class end def init @@ -89,11 +82,11 @@ @routes = HttpRouter.new # Handle before filters @before_filters = HttpRouter.new - @config = {} + @settings = {} @helpers = Module.new end private @@ -111,18 +104,29 @@ # include Helpers handler.extend(self.class.get_helpers) catch(:halt) do # evaluate any route match - filters = self.class.before_filters.recognize(env).first || [] + filters = self.class.before_filters.recognize(env).first + filters.each do |f| + # include route params into env + route_params = f.params + env.params.merge! route_params + handler.handle! &f.route.dest - end + + # remove after usage + env.params.delete_if {|k,v| route_params[k] == v} + end if filters routes_recognized = self.class.routes.recognize(env).first if routes_recognized # Use the first matched route - handler.handle! &routes_recognized.first.route.dest + http_router_response = routes_recognized.first + # Bind params to the env + env.params.merge! http_router_response.params + handler.handle! &http_router_response.route.dest else handler.handle! do status 404 'not found!' end \ No newline at end of file