backend: add Servlet::ResponseMixin
This commit is contained in:
parent
05f7ce8c49
commit
3b1546388b
2 changed files with 42 additions and 18 deletions
|
@ -2,24 +2,10 @@ class Twenty::Servlet < WEBrick::HTTPServlet::AbstractServlet
|
|||
require_relative "servlet/response"
|
||||
require_relative "servlet/projects"
|
||||
require_relative "servlet/tasks"
|
||||
|
||||
# mixins
|
||||
require_relative "servlet/mixin/server_mixin"
|
||||
require_relative "servlet/mixin/response_mixin"
|
||||
extend ServerMixin
|
||||
|
||||
def ok(res, body = {})
|
||||
Response.new(res)
|
||||
.set_status(200)
|
||||
.set_body(body)
|
||||
end
|
||||
|
||||
def not_found(res)
|
||||
Response.new(res)
|
||||
.set_status(404)
|
||||
.set_body({errors: ["The requested path was not found"]})
|
||||
end
|
||||
|
||||
def bad_request(res, body = {})
|
||||
Response.new(res)
|
||||
.set_status(400)
|
||||
.set_body({errors: ["Bad request"]}.merge(body))
|
||||
end
|
||||
include ResponseMixin
|
||||
end
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
module Twenty::Servlet::ResponseMixin
|
||||
##
|
||||
# Sets a 200 OK response.
|
||||
# @param [WEBrick::HTTPResponse] res
|
||||
# An instance of {WEBrick::HTTPResponse WEBrick::HTTPResponse}
|
||||
# @param [#to_json] body
|
||||
# The response body.
|
||||
# @return [void]
|
||||
def ok(res, body = {})
|
||||
Response.new(res)
|
||||
.set_status(200)
|
||||
.set_body(body)
|
||||
end
|
||||
|
||||
##
|
||||
# Sets a 400 Bad Request response.
|
||||
# @param [WEBrick::HTTPResponse] res
|
||||
# An instance of {WEBrick::HTTPResponse WEBrick::HTTPResponse}
|
||||
# @param [#to_json] body
|
||||
# The response body.
|
||||
# @return [void]
|
||||
def bad_request(res, body = {})
|
||||
Response.new(res)
|
||||
.set_status(400)
|
||||
.set_body({errors: ["Bad request"]}.merge(body))
|
||||
end
|
||||
|
||||
##
|
||||
# Set a 404 Not Found response.
|
||||
# @param [WEBrick::HTTPResponse] res
|
||||
# An instance of {WEBrick::HTTPResponse WEBrick::HTTPResponse}
|
||||
# @return [void]
|
||||
def not_found(res)
|
||||
Response.new(res)
|
||||
.set_status(404)
|
||||
.set_body({errors: ["The requested path was not found"]})
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue