From a0c909621cf11587e7defc2ec79bb41a3e5ac9f9 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Thu, 29 Oct 2015 11:49:58 +0200 Subject: [PATCH] Renamed all occurrences of ctx to env --- README.md | 16 ++++++++-------- samples/json_api.cr | 4 ++-- spec/kemal_handler_spec.cr | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 86132a0..10a4fe0 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/samples/json_api.cr b/samples/json_api.cr index 098b67d..6d71523 100644 --- a/samples/json_api.cr +++ b/samples/json_api.cr @@ -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 diff --git a/spec/kemal_handler_spec.cr b/spec/kemal_handler_spec.cr index a43cdec..74d14e0 100644 --- a/spec/kemal_handler_spec.cr +++ b/spec/kemal_handler_spec.cr @@ -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)