mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Refactored context
This commit is contained in:
		
							parent
							
								
									4ce1bc908f
								
							
						
					
					
						commit
						a7cc498dca
					
				
					 4 changed files with 8 additions and 13 deletions
				
			
		|  | @ -14,7 +14,7 @@ describe "Context" do | ||||||
|   it "sets content type" do |   it "sets content type" do | ||||||
|     kemal = Kemal::Handler.new |     kemal = Kemal::Handler.new | ||||||
|     kemal.add_route "GET", "/" do |env| |     kemal.add_route "GET", "/" do |env| | ||||||
|       env.set_content_type "application/json" |       env.content_type = "application/json" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("GET", "/") |     request = HTTP::Request.new("GET", "/") | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|  | @ -37,7 +37,7 @@ describe "Context" do | ||||||
|   it "sets response headers" do |   it "sets response headers" do | ||||||
|     kemal = Kemal::Handler.new |     kemal = Kemal::Handler.new | ||||||
|     kemal.add_route "GET", "/" do |env| |     kemal.add_route "GET", "/" do |env| | ||||||
|       env.set_header "Accept-Language", "tr" |       env.add_header "Accept-Language", "tr" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("GET", "/") |     request = HTTP::Request.new("GET", "/") | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|  |  | ||||||
|  | @ -10,15 +10,11 @@ class Kemal::Context | ||||||
|     @response = Kemal::Response.new |     @response = Kemal::Response.new | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def headers |  | ||||||
|     @request.headers |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   def response_headers |   def response_headers | ||||||
|     @response.headers |     @response.headers | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def set_header(name, value) |   def add_header(name, value) | ||||||
|     @response.headers.add name, value |     @response.headers.add name, value | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  | @ -26,9 +22,8 @@ class Kemal::Context | ||||||
|     @response.content_type |     @response.content_type | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def set_content_type(content_type) |   delegate headers, @request | ||||||
|     @response.content_type = content_type |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   delegate status_code, @response |   delegate status_code, @response | ||||||
|  |   delegate :"status_code=", @response | ||||||
|  |   delegate :"content_type=", @response | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -26,7 +26,7 @@ class Kemal::Handler < HTTP::Handler | ||||||
|         context = Context.new(request, params.not_nil!) |         context = Context.new(request, params.not_nil!) | ||||||
|         begin |         begin | ||||||
|           body = route.handler.call(context).to_s |           body = route.handler.call(context).to_s | ||||||
|           return HTTP::Response.new(200, body, context.response_headers) |           return HTTP::Response.new(context.status_code, body, context.response_headers) | ||||||
|         rescue ex |         rescue ex | ||||||
|           return HTTP::Response.error("text/plain", ex.to_s) |           return HTTP::Response.error("text/plain", ex.to_s) | ||||||
|         end |         end | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ class Kemal::Response | ||||||
|   property content_type |   property content_type | ||||||
| 
 | 
 | ||||||
|   def initialize |   def initialize | ||||||
|     @status_code :: String |     @status_code = 200 | ||||||
|     @content_type = "text/html" |     @content_type = "text/html" | ||||||
|     @headers = HTTP::Headers{"Content-Type": @content_type} |     @headers = HTTP::Headers{"Content-Type": @content_type} | ||||||
|   end |   end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue