From 5d193e649335bc017af1a8e1c246b68917e681cf Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Sun, 13 Oct 2019 18:01:00 +0200 Subject: [PATCH] add optional response status Signed-off-by: Alexis Reigel --- README.md | 3 ++- template/ruby-http/function/handler.rb | 3 ++- template/ruby-http/index.rb | 16 ++++++++-------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index be4125b..c9c13eb 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,11 @@ Edit the `homepage/handler.rb` file to return some HTML: ```ruby class Handler def run(body, headers) + status_code = 200 # Optional status code, defaults to 200 response_headers = {"content-type": "text/html"} body = "Hello world from the Ruby template" - return body, response_headers + return body, response_headers, status_code end end ``` diff --git a/template/ruby-http/function/handler.rb b/template/ruby-http/function/handler.rb index e23c0e6..1e125c0 100644 --- a/template/ruby-http/function/handler.rb +++ b/template/ruby-http/function/handler.rb @@ -1,8 +1,9 @@ class Handler def run(body, headers) + status_code = 200 # Optional status code, defaults to 200 response_headers = {"content-type": "text/plain"} body = "Hello world from the Ruby template" - return body, response_headers + return body, response_headers, status_code end end diff --git a/template/ruby-http/index.rb b/template/ruby-http/index.rb index 9c96e26..c0290dd 100644 --- a/template/ruby-http/index.rb +++ b/template/ruby-http/index.rb @@ -11,25 +11,25 @@ handler = Handler.new get '/*' do - res, res_headers = handler.run request.body, request.env + res, res_headers, status = handler.run request.body, request.env - [200, res_headers, res] + [status || 200, res_headers, res] end post '/*' do - res, res_headers = handler.run request.body, request.env + res, res_headers, status = handler.run request.body, request.env - [200, res_headers, res] + [status || 200, res_headers, res] end put '/*' do - res, res_headers = handler.run request.body, request.env + res, res_headers, status = handler.run request.body, request.env - [200, res_headers, res] + [status || 200, res_headers, res] end delete '/*' do - res, res_headers = handler.run request.body, request.env + res, res_headers, status = handler.run request.body, request.env - [200, res_headers, res] + [status || 200, res_headers, res] end