2012-11-16 17:56:34 +01:00
|
|
|
module Zero
|
|
|
|
|
|
|
|
# This is the representation of a response
|
|
|
|
#
|
|
|
|
class Response
|
2012-11-17 14:35:15 +01:00
|
|
|
attr_reader :status
|
|
|
|
attr_accessor :header, :body
|
|
|
|
|
2012-11-18 16:24:50 +01:00
|
|
|
# Constructor
|
2012-11-17 14:46:53 +01:00
|
|
|
# Sets default status code to 200.
|
|
|
|
#
|
|
|
|
def initialize
|
|
|
|
@status = 200
|
2012-11-18 15:02:07 +01:00
|
|
|
@header = {}
|
2012-11-18 16:09:41 +01:00
|
|
|
@body = []
|
2012-11-17 14:46:53 +01:00
|
|
|
end
|
|
|
|
|
2012-11-17 14:35:15 +01:00
|
|
|
# Sets the status.
|
|
|
|
# Also converts every input directly to an integer
|
|
|
|
#
|
|
|
|
# @param [Integer] status
|
|
|
|
#
|
|
|
|
def status=(status)
|
|
|
|
@status = status.to_i
|
|
|
|
end
|
2012-11-16 17:56:34 +01:00
|
|
|
|
|
|
|
# Returns the data of the response as an array:
|
|
|
|
# [status, header, body]
|
|
|
|
# to be usable by any webserver
|
|
|
|
#
|
2012-11-18 16:09:41 +01:00
|
|
|
# @return [Array]
|
2012-11-16 17:56:34 +01:00
|
|
|
#
|
|
|
|
def to_a()
|
2012-11-18 16:24:50 +01:00
|
|
|
# TODO Remove content length and body, on certain status codes
|
|
|
|
# TODO Set content length, if not already set
|
|
|
|
# TODO Set content type, if not already set
|
|
|
|
|
2012-11-23 17:45:36 +01:00
|
|
|
[status, header, body]
|
2012-11-16 17:56:34 +01:00
|
|
|
end
|
|
|
|
|
2012-11-23 17:43:39 +01:00
|
|
|
# Sets the content length header to the current length of the body
|
|
|
|
# Also creates one, if it does not exists
|
|
|
|
#
|
|
|
|
def content_length
|
2012-11-23 17:59:33 +01:00
|
|
|
header['Content-Length'] = body.join.length
|
2012-11-23 17:43:39 +01:00
|
|
|
end
|
|
|
|
|
2012-11-16 17:56:34 +01:00
|
|
|
end
|
|
|
|
end
|