mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Renamed all occurrences of ctx to env
This commit is contained in:
		
							parent
							
								
									5bc81e7e5b
								
							
						
					
					
						commit
						a0c909621c
					
				
					 3 changed files with 16 additions and 16 deletions
				
			
		
							
								
								
									
										16
									
								
								README.md
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
										
									
									
									
								
							|  | @ -65,19 +65,19 @@ In Kemal, a route is an HTTP method paired with a URL-matching pattern. Each rou | ||||||
| 
 | 
 | ||||||
| ## Context | ## Context | ||||||
| 
 | 
 | ||||||
| Accessing the request context (query params, body, headers e.g) is super easy. You can use the context returned from the block: | Accessing the request environment (query params, body, headers e.g) is super easy. You can use the context returned from the block: | ||||||
| 
 | 
 | ||||||
| ```ruby | ```ruby | ||||||
|   # Matches /hello/kemal |   # Matches /hello/kemal | ||||||
|   get "/hello/:name" do |ctx| |   get "/hello/:name" do |env| | ||||||
|     name = ctx.params["name"] |     name = env.params["name"] | ||||||
|     "Hello back to #{name}" |     "Hello back to #{name}" | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   # Matches /resize?width=200&height=200 |   # Matches /resize?width=200&height=200 | ||||||
|   get "/resize" do |ctx| |   get "/resize" do |env| | ||||||
|     width = ctx.params["width"] |     width = env.params["width"] | ||||||
|     height = ctx.params["height"] |     height = env.params["height"] | ||||||
|   end |   end | ||||||
| ``` | ``` | ||||||
| 
 | 
 | ||||||
|  | @ -86,9 +86,9 @@ Kemal uses *text/html* as the default content type. You can change it via the co | ||||||
| 
 | 
 | ||||||
| ```ruby | ```ruby | ||||||
|   # Set the content as application/json and return JSON |   # Set the content as application/json and return JSON | ||||||
|   get "/user.json" do |ctx| |   get "/user.json" do |env| | ||||||
|     kemal = {name: "Kemal", language: "Crystal"} |     kemal = {name: "Kemal", language: "Crystal"} | ||||||
|     ctx.set_content_type "application/json" |     env.set_content_type "application/json" | ||||||
|     kemal.to_json |     kemal.to_json | ||||||
|   end |   end | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|  | @ -1,9 +1,9 @@ | ||||||
| require "kemal" | require "kemal" | ||||||
| require "json" | require "json" | ||||||
| 
 | 
 | ||||||
| # You can easily access the environment and set content_type like 'application/json'. | # You can easily access the context and set content_type like 'application/json'. | ||||||
| # Look how easy to build a JSON serving API. | # Look how easy to build a JSON serving API. | ||||||
| get "/" do |env| | get "/" do |env| | ||||||
|   env.response.content_type = "application/json" |   env.set_content_type = "application/json" | ||||||
|   {name: "Serdar", age: 27}.to_json |   {name: "Serdar", age: 27}.to_json | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -13,8 +13,8 @@ describe "Kemal::Handler" do | ||||||
| 
 | 
 | ||||||
|   it "routes request with query string" do |   it "routes request with query string" do | ||||||
|     kemal = Kemal::Handler.new |     kemal = Kemal::Handler.new | ||||||
|     kemal.add_route "GET", "/" do |ctx| |     kemal.add_route "GET", "/" do |env| | ||||||
|       "hello #{ctx.params["message"]}" |       "hello #{env.params["message"]}" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("GET", "/?message=world") |     request = HTTP::Request.new("GET", "/?message=world") | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|  | @ -23,8 +23,8 @@ describe "Kemal::Handler" do | ||||||
| 
 | 
 | ||||||
|   it "routes request with multiple query strings" do |   it "routes request with multiple query strings" do | ||||||
|     kemal = Kemal::Handler.new |     kemal = Kemal::Handler.new | ||||||
|     kemal.add_route "GET", "/" do |ctx| |     kemal.add_route "GET", "/" do |env| | ||||||
|       "hello #{ctx.params["message"]} time #{ctx.params["time"]}" |       "hello #{env.params["message"]} time #{env.params["time"]}" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("GET", "/?message=world&time=now") |     request = HTTP::Request.new("GET", "/?message=world&time=now") | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|  | @ -33,8 +33,8 @@ describe "Kemal::Handler" do | ||||||
| 
 | 
 | ||||||
|   it "route parameter has more precedence than query string arguments" do |   it "route parameter has more precedence than query string arguments" do | ||||||
|     kemal = Kemal::Handler.new |     kemal = Kemal::Handler.new | ||||||
|     kemal.add_route "GET", "/:message" do |ctx| |     kemal.add_route "GET", "/:message" do |env| | ||||||
|       "hello #{ctx.params["message"]}" |       "hello #{env.params["message"]}" | ||||||
|     end |     end | ||||||
|     request = HTTP::Request.new("GET", "/world?message=coco") |     request = HTTP::Request.new("GET", "/world?message=coco") | ||||||
|     response = kemal.call(request) |     response = kemal.call(request) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue