Refactored context

This commit is contained in:
Sdogruyol 2015-10-30 22:34:44 +02:00
parent 4ce1bc908f
commit a7cc498dca
4 changed files with 8 additions and 13 deletions

View file

@ -14,7 +14,7 @@ describe "Context" do
it "sets content type" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |env|
env.set_content_type "application/json"
env.content_type = "application/json"
end
request = HTTP::Request.new("GET", "/")
response = kemal.call(request)
@ -37,7 +37,7 @@ describe "Context" do
it "sets response headers" do
kemal = Kemal::Handler.new
kemal.add_route "GET", "/" do |env|
env.set_header "Accept-Language", "tr"
env.add_header "Accept-Language", "tr"
end
request = HTTP::Request.new("GET", "/")
response = kemal.call(request)

View file

@ -10,15 +10,11 @@ class Kemal::Context
@response = Kemal::Response.new
end
def headers
@request.headers
end
def response_headers
@response.headers
end
def set_header(name, value)
def add_header(name, value)
@response.headers.add name, value
end
@ -26,9 +22,8 @@ class Kemal::Context
@response.content_type
end
def set_content_type(content_type)
@response.content_type = content_type
end
delegate headers, @request
delegate status_code, @response
delegate :"status_code=", @response
delegate :"content_type=", @response
end

View file

@ -26,7 +26,7 @@ class Kemal::Handler < HTTP::Handler
context = Context.new(request, params.not_nil!)
begin
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
return HTTP::Response.error("text/plain", ex.to_s)
end

View file

@ -4,7 +4,7 @@ class Kemal::Response
property content_type
def initialize
@status_code :: String
@status_code = 200
@content_type = "text/html"
@headers = HTTP::Headers{"Content-Type": @content_type}
end