/
response.cr
49 lines (40 loc) · 1.43 KB
/
response.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Marten
module HTTP
# Represents an HTTP response.
#
# This class allows to manipulate HTTP responses. These objects are associated with a specific HTTP response status,
# and they can define an associated content (and content type) as well as additional headers.
class Response
DEFAULT_CONTENT_TYPE = "text/html"
# Returns the content associated with the HTTP response.
getter content
# Returns the content type associated with the HTTP response.
getter content_type
# Returns the cookies associated with the HTTP response.
getter cookies
# Returns the headers associated with the HTTP response.
getter headers
# Returns the status code of the HTTP response.
getter status
# Allows to overridde the response's content.
setter content
def initialize(
@content : String = "",
@content_type : String = DEFAULT_CONTENT_TYPE,
@status : Int32 = 200
)
@cookies = Cookies.new
@headers = Headers.new
end
# Allows to set a specific header.
def []=(header : String | Symbol, value)
headers[header.to_s] = value.to_s
end
macro override_status(status)
def initialize(content : String = "", content_type : String = DEFAULT_CONTENT_TYPE)
super(content: content, content_type: content_type, status: {{ status }})
end
end
end
end
end