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.
|
2012-11-24 15:42:08 +01:00
|
|
|
# Also converts every input directly to an integer.
|
2012-11-17 14:35:15 +01:00
|
|
|
#
|
2012-11-24 15:42:08 +01:00
|
|
|
# @param [Integer] status The status code
|
2012-11-17 14:35:15 +01:00
|
|
|
#
|
|
|
|
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]
|
2012-11-24 15:42:08 +01:00
|
|
|
# to be usable by any webserver.
|
2012-11-16 17:56:34 +01:00
|
|
|
#
|
2012-11-24 15:42:08 +01:00
|
|
|
# Sets the Content-Type to 'text/html', if it's not already set.
|
|
|
|
# Sets the Content-Length, if it's not already set. (Won't fix wrong
|
|
|
|
# lengths!)
|
|
|
|
# Removes Content-Type, Content-Length and body on status code 204 and 304.
|
|
|
|
#
|
|
|
|
# @return [Array] Usable by webservers
|
2012-11-16 17:56:34 +01:00
|
|
|
#
|
2012-11-23 21:24:37 +01:00
|
|
|
def to_a
|
2012-11-24 15:28:42 +01:00
|
|
|
# Remove content length and body, on status 204 and 304
|
|
|
|
if status == 204 or status == 304
|
2012-11-23 21:24:37 +01:00
|
|
|
header.delete('Content-Length')
|
|
|
|
header.delete('Content-Type')
|
2012-11-24 15:19:02 +01:00
|
|
|
self.body = []
|
2012-11-23 21:24:37 +01:00
|
|
|
else
|
|
|
|
# Set content length, if not already set
|
|
|
|
content_length unless header.has_key? 'Content-Length'
|
|
|
|
# Set content type, if not already set
|
2012-11-24 15:35:23 +01:00
|
|
|
self.content_type = 'text/html' unless header.has_key? 'Content-Type'
|
2012-11-23 21:24:37 +01:00
|
|
|
end
|
2012-11-18 16:24:50 +01:00
|
|
|
|
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-24 16:17:14 +01:00
|
|
|
self.header['Content-Length'] = body.join.bytesize.to_s
|
2012-11-23 17:43:39 +01:00
|
|
|
end
|
|
|
|
|
2012-11-24 15:42:08 +01:00
|
|
|
# Sets the content type header to the given value
|
2012-11-23 19:54:17 +01:00
|
|
|
# Also creates it, if it does not exists
|
|
|
|
#
|
2012-11-24 15:42:08 +01:00
|
|
|
# @param [String] value Content-Type tp set
|
|
|
|
#
|
2012-11-24 15:35:23 +01:00
|
|
|
def content_type=(value)
|
2012-11-24 15:19:02 +01:00
|
|
|
self.header['Content-Type'] = value
|
2012-11-23 19:54:17 +01:00
|
|
|
end
|
|
|
|
|
2012-11-16 17:56:34 +01:00
|
|
|
end
|
|
|
|
end
|