lib/angelo/base.rb in angelo-0.1.22 vs lib/angelo/base.rb in angelo-0.1.23
- old
+ new
@@ -73,16 +73,39 @@
@routes[m] ||= {}
end
@routes
end
+ def filters
+ @filters ||= {before: {default: []}, after: {default: []}}
+ end
+
+ def filter which, opts = {}, &block
+ f = compile! :filter, &block
+ case opts
+ when String
+ filter_by which, opts, f
+ when Hash
+ if opts[:path]
+ filter_by which, opts[:path], f
+ else
+ filters[which][:default] << f
+ end
+ end
+ end
+
+ def filter_by which, path, meth
+ filters[which][path] ||= []
+ filters[which][path] << meth
+ end
+
def before opts = {}, &block
- define_method :before, &block
+ filter :before, opts, &block
end
def after opts = {}, &block
- define_method :after, &block
+ filter :after, opts, &block
end
HTTPABLE.each do |m|
define_method m do |path, &block|
routes[m][path] = Responder.new &block
@@ -91,12 +114,12 @@
def websocket path, &block
routes[:websocket][path] = Responder::Websocket.new &block
end
- def eventsource path, &block
- routes[:get][path] = Responder::Eventsource.new &block
+ def eventsource path, headers = nil, &block
+ routes[:get][path] = Responder::Eventsource.new headers, &block
end
def on_pong &block
Responder::Websocket.on_pong = block
end
@@ -164,13 +187,14 @@
self.class.server.future.__send__ meth, *args
end
def params
@params ||= case request.method
- when GET; parse_query_string
- when POST; parse_post_body
- when PUT; parse_post_body
+ when GET, DELETE, OPTIONS
+ parse_query_string
+ when POST, PUT
+ parse_post_body
end
end
def request_headers
@request_headers ||= Hash.new do |hash, key|
@@ -271,9 +295,15 @@
@@report_errors
end
def sleep time
Celluloid.sleep time
+ end
+
+ def filter which
+ fs = self.class.filters[which][:default]
+ fs += self.class.filters[which][request.path] if self.class.filters[which][request.path]
+ fs.each {|f| f.bind(self).call}
end
end
end