backend: add Twenty::Servlet::Response docs

This commit is contained in:
0x1eef 2023-12-21 02:24:06 -03:00
parent 6cba2133b9
commit b8afeccc39

View file

@ -1,28 +1,49 @@
class Twenty::Servlet::Response class Twenty::Servlet::Response
attr_reader :res ##
# @param [WEBrick::HTTPResponse] res
# An instance of WEBrick::HTTPResponse.
# @return [Twenty::Servlet::Response]
# Returns an instance of Twenty::Servlet::Response.
def initialize(res) def initialize(res)
@res = res @res = res
set_headers({"content-type" => "application/json"}) set_headers({"content-type" => "application/json"})
end end
##
# Sets the response status.
# @param [Integer] status
# A status code.
# @return [Twenty::Servlet::Response]
# Returns self.
def set_status(status) def set_status(status)
res.status = status res.status = status
self self
end end
##
# Sets the response body.
# @param [#to_json] body
# The response body.
# @return [Twenty::Servlet::Response]
# Returns self.
def set_body(body) def set_body(body)
res.body = default_body_for(res.status).merge(body).to_json res.body = default_body_for(res.status).merge(body).to_json
self self
end end
##
# Sets the response headers.
# @param [#each] headers
# The response headers.
# @return [Twenty::Servlet::Response]
# Returns self.
def set_headers(headers) def set_headers(headers)
headers.each { res[_1] = _2 } headers.each { res[_1] = _2 }
self self
end end
private private
attr_reader :res
def default_body_for(status) def default_body_for(status)
case status case status
when 200 when 200