Class: Utopia::Content::Response

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

Overview

A basic content response, including useful defaults for typical HTML5 content.

Direct Known Subclasses

Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



33
34
35
36
37
38
39
40
# File 'lib/utopia/content/response.rb', line 33

def initialize
	@status = 200
	@headers = {}
	@body = []
	
	# The default content type:
	self.content_type = "text/html; charset=utf-8"
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



44
45
46
# File 'lib/utopia/content/response.rb', line 44

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



43
44
45
# File 'lib/utopia/content/response.rb', line 43

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



42
43
44
# File 'lib/utopia/content/response.rb', line 42

def status
  @status
end

Instance Method Details

#cache!(duration = 3600, access: "public") ⇒ Object

Specify that the content could be cached.



65
66
67
68
69
70
# File 'lib/utopia/content/response.rb', line 65

def cache!(duration = 3600, access: "public")
	unless cache_control = @headers[CACHE_CONTROL] and cache_control.include?(NO_CACHE)
		@headers[CACHE_CONTROL] = "#{access}, max-age=#{duration}"
		@headers[EXPIRES] = (Time.now + duration).httpdate
	end
end

#contentObject



46
47
48
# File 'lib/utopia/content/response.rb', line 46

def content
	@body.join
end

#content_type=(value) ⇒ Object Also known as: content_type!

Specify the content type of the response data.



73
74
75
# File 'lib/utopia/content/response.rb', line 73

def content_type= value
	@headers[CONTENT_TYPE] = value
end

#do_not_cache!Object

Specifies that the content shouldn’t be cached. Overrides ‘cache!` if already called.



59
60
61
62
# File 'lib/utopia/content/response.rb', line 59

def do_not_cache!
	@headers[CACHE_CONTROL] = "no-cache, must-revalidate"
	@headers[EXPIRES] = Time.now.httpdate
end

#lookup(tag) ⇒ Object



50
51
52
# File 'lib/utopia/content/response.rb', line 50

def lookup(tag)
	return nil
end

#to_aObject



54
55
56
# File 'lib/utopia/content/response.rb', line 54

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