mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	allow %w in Handler macros (#385)
This commit is contained in:
		
							parent
							
								
									fc4c97aafd
								
							
						
					
					
						commit
						5a83522866
					
				
					 2 changed files with 29 additions and 2 deletions
				
			
		|  | @ -47,6 +47,26 @@ class PostExcludeHandler < Kemal::Handler | ||||||
|   end |   end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
|  | class ExcludeHandlerPercentW < Kemal::Handler | ||||||
|  |   exclude %w[/exclude] | ||||||
|  | 
 | ||||||
|  |   def call(env) | ||||||
|  |     return call_next(env) if exclude_match?(env) | ||||||
|  |     env.response.print "Exclude" | ||||||
|  |     call_next env | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
|  | class PostOnlyHandlerPercentW < Kemal::Handler | ||||||
|  |   only %w[/only /route1 /route2], "POST" | ||||||
|  | 
 | ||||||
|  |   def call(env) | ||||||
|  |     return call_next(env) unless only_match?(env) | ||||||
|  |     env.response.print "Only" | ||||||
|  |     call_next env | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | 
 | ||||||
| describe "Handler" do | describe "Handler" do | ||||||
|   it "adds custom handler before before_*" do |   it "adds custom handler before before_*" do | ||||||
|     filter_middleware = Kemal::FilterHandler.new |     filter_middleware = Kemal::FilterHandler.new | ||||||
|  | @ -131,4 +151,11 @@ describe "Handler" do | ||||||
|     Kemal.config.handlers = [post_only_handler, post_exclude_handler] |     Kemal.config.handlers = [post_only_handler, post_exclude_handler] | ||||||
|     Kemal.config.handlers.should eq [post_only_handler, post_exclude_handler] |     Kemal.config.handlers.should eq [post_only_handler, post_exclude_handler] | ||||||
|   end |   end | ||||||
|  | 
 | ||||||
|  |   it "is able to use %w in macros" do | ||||||
|  |     post_only_handler = PostOnlyHandlerPercentW.new | ||||||
|  |     exclude_handler = ExcludeHandlerPercentW.new | ||||||
|  |     Kemal.config.handlers = [post_only_handler, exclude_handler] | ||||||
|  |     Kemal.config.handlers.should eq [post_only_handler, exclude_handler] | ||||||
|  |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -9,14 +9,14 @@ module Kemal | ||||||
| 
 | 
 | ||||||
|     macro only(paths, method = "GET") |     macro only(paths, method = "GET") | ||||||
|     class_name = {{@type.name}} |     class_name = {{@type.name}} | ||||||
|     {{paths}}.each do |path| |     ({{paths}}).each do |path| | ||||||
|       @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" |       @@only_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|     macro exclude(paths, method = "GET") |     macro exclude(paths, method = "GET") | ||||||
|     class_name = {{@type.name}} |     class_name = {{@type.name}} | ||||||
|     {{paths}}.each do |path| |     ({{paths}}).each do |path| | ||||||
|       @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" |       @@exclude_routes_tree.add "#{class_name}/#{{{method}}.downcase}#{path}", "/#{{{method}}.downcase}#{path}" | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue