0
0
zero/lib/zero/response.rb

51 lines
1.1 KiB
Ruby
Raw Normal View History

2012-11-16 17:56:34 +01:00
module Zero
# This is the representation of a response
#
class Response
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
# 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
2012-11-23 19:35:14 +01:00
content_length
2012-11-18 16:24:50 +01:00
# 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
# Sets the content length header to the current length of the body
# Also creates one, if it does not exists
#
def content_length
header['Content-Length'] = body.join.bytesize
end
2012-11-16 17:56:34 +01:00
end
end