Class: Utopia::Content::Response
- Inherits:
-
Object
- Object
- Utopia::Content::Response
- Defined in:
- lib/utopia/content/response.rb
Overview
A basic content response, including useful defaults for typical HTML5 content.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#cache!(duration = 3600, access: "public") ⇒ Object
Specify that the content could be cached.
- #content ⇒ Object
-
#content_type=(value) ⇒ Object
(also: #content_type!)
Specify the content type of the response data.
-
#do_not_cache! ⇒ Object
Specifies that the content shouldn't be cached.
-
#initialize ⇒ Response
constructor
A new instance of Response.
- #lookup(tag) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
31 32 33 34 35 36 37 38 |
# File 'lib/utopia/content/response.rb', line 31 def initialize @status = 200 @headers = {} @body = [] # The default content type: self.content_type = "text/html; charset=utf-8" end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
42 43 44 |
# File 'lib/utopia/content/response.rb', line 42 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
41 42 43 |
# File 'lib/utopia/content/response.rb', line 41 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
40 41 42 |
# File 'lib/utopia/content/response.rb', line 40 def status @status end |
Instance Method Details
#cache!(duration = 3600, access: "public") ⇒ Object
Specify that the content could be cached.
63 64 65 66 67 68 |
# File 'lib/utopia/content/response.rb', line 63 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 |
#content ⇒ Object
44 45 46 |
# File 'lib/utopia/content/response.rb', line 44 def content @body.join end |
#content_type=(value) ⇒ Object Also known as: content_type!
Specify the content type of the response data.
71 72 73 |
# File 'lib/utopia/content/response.rb', line 71 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.
57 58 59 60 |
# File 'lib/utopia/content/response.rb', line 57 def do_not_cache! @headers[CACHE_CONTROL] = "no-cache, must-revalidate" @headers[EXPIRES] = Time.now.httpdate end |
#lookup(tag) ⇒ Object
48 49 50 |
# File 'lib/utopia/content/response.rb', line 48 def lookup(tag) return nil end |
#to_a ⇒ Object
52 53 54 |
# File 'lib/utopia/content/response.rb', line 52 def to_a [@status, @headers, @body] end |