Class: Lennarb::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/lennarb/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Initialize the response object



46
47
48
49
50
51
# File 'lib/lennarb/response.rb', line 46

def initialize
	@status = 404
	@headers = {}
	@body    = []
	@length  = 0
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



16
17
18
# File 'lib/lennarb/response.rb', line 16

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



21
22
23
# File 'lib/lennarb/response.rb', line 21

def headers
  @headers
end

#lengthObject (readonly)

Returns the value of attribute length.



26
27
28
# File 'lib/lennarb/response.rb', line 26

def length
  @length
end

#statusObject



11
12
13
# File 'lib/lennarb/response.rb', line 11

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object

Set the response header



59
60
61
# File 'lib/lennarb/response.rb', line 59

def [](key)
	@headers[key]
end

#[]=(key, value) ⇒ Object

Get the response header



70
71
72
# File 'lib/lennarb/response.rb', line 70

def []=(key, value)
	@headers[key] = value
end

#finishObject

Finish the response



134
135
136
# File 'lib/lennarb/response.rb', line 134

def finish
	[@status, @headers, @body]
end

#html(str) ⇒ Object

Set the response type to html



104
105
106
107
# File 'lib/lennarb/response.rb', line 104

def html(str)
	@headers[CONTENT_TYPE] = ContentType[:HTML]
	write(str)
end

#json(str) ⇒ Object

Set the response type to json



115
116
117
118
# File 'lib/lennarb/response.rb', line 115

def json(str)
	@headers[CONTENT_TYPE] = ContentType[:JSON]
	write(str)
end

#redirect(path, status = 302) ⇒ Object

Redirect the response



125
126
127
128
# File 'lib/lennarb/response.rb', line 125

def redirect(path, status = 302)
	@headers[LOCATION] = path
	@status = status
end

#text(str) ⇒ Object

Set the response type to text



93
94
95
96
# File 'lib/lennarb/response.rb', line 93

def text(str)
	@headers[CONTENT_TYPE] = ContentType[:TEXT]
	write(str)
end

#write(str) ⇒ Object

Write to the response body



80
81
82
83
84
85
# File 'lib/lennarb/response.rb', line 80

def write(str)
	str = str.to_s
	@length += str.bytesize
	@headers[CONTENT_LENGTH] = @length.to_s
	@body << str
end