Renamed all occurrences of ctx to env

This commit is contained in:
Sdogruyol 2015-10-29 11:49:58 +02:00
parent 5bc81e7e5b
commit a0c909621c
3 changed files with 16 additions and 16 deletions

View File

@ -65,19 +65,19 @@ In Kemal, a route is an HTTP method paired with a URL-matching pattern. Each rou
## 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
# Matches /hello/kemal
get "/hello/:name" do |ctx|
name = ctx.params["name"]
get "/hello/:name" do |env|
name = env.params["name"]
"Hello back to #{name}"
end
# Matches /resize?width=200&height=200
get "/resize" do |ctx|
width = ctx.params["width"]
height = ctx.params["height"]
get "/resize" do |env|
width = env.params["width"]
height = env.params["height"]
end
```
@ -86,9 +86,9 @@ Kemal uses *text/html* as the default content type. You can change it via the co
```ruby
# Set the content as application/json and return JSON
get "/user.json" do |ctx|
get "/user.json" do |env|
kemal = {name: "Kemal", language: "Crystal"}
ctx.set_content_type "application/json"
env.set_content_type "application/json"
kemal.to_json
end
```

View File

@ -1,9 +1,9 @@
require "kemal"
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.
get "/" do |env|
env.response.content_type = "application/json"
env.set_content_type = "application/json"
{name: "Serdar", age: 27}.to_json
end

View File

@ -13,8 +13,8 @@ describe "Kemal::Handler" do
it "routes request with query string" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |ctx|
"hello #{ctx.params["message"]}"
kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]}"
end
request = HTTP::Request.new("GET", "/?message=world")
response = kemal.call(request)
@ -23,8 +23,8 @@ describe "Kemal::Handler" do
it "routes request with multiple query strings" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |ctx|
"hello #{ctx.params["message"]} time #{ctx.params["time"]}"
kemal.add_route "GET", "/" do |env|
"hello #{env.params["message"]} time #{env.params["time"]}"
end
request = HTTP::Request.new("GET", "/?message=world&time=now")
response = kemal.call(request)
@ -33,8 +33,8 @@ describe "Kemal::Handler" do
it "route parameter has more precedence than query string arguments" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/:message" do |ctx|
"hello #{ctx.params["message"]}"
kemal.add_route "GET", "/:message" do |env|
"hello #{env.params["message"]}"
end
request = HTTP::Request.new("GET", "/world?message=coco")
response = kemal.call(request)