Class: Tzispa::Http::Response
- Inherits:
-
Rack::Response
- Object
- Rack::Response
- Tzispa::Http::Response
- Defined in:
- lib/tzispa/http/response.rb
Constant Summary collapse
- DROP_BODY_RESPONSES =
[204, 205, 304].freeze
Instance Method Summary collapse
- #body=(value) ⇒ Object
- #cache_control ⇒ Object
- #cache_private ⇒ Object
- #calculate_content_length? ⇒ Boolean
- #custom_headers ⇒ Object
- #drop_body? ⇒ Boolean
- #drop_content_info? ⇒ Boolean
- #each ⇒ Object
- #finish ⇒ Object
-
#initialize ⇒ Response
constructor
A new instance of Response.
- #max_age(seconds) ⇒ Object
- #must_revalidate ⇒ Object
- #no_cache ⇒ Object
- #no_store ⇒ Object
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
11 12 13 14 |
# File 'lib/tzispa/http/response.rb', line 11 def initialize(*) super headers['Content-Type'] ||= 'text/html' end |
Instance Method Details
#body=(value) ⇒ Object
16 17 18 19 |
# File 'lib/tzispa/http/response.rb', line 16 def body=(value) value = value.body while value.is_a?(Rack::Response) @body = value.is_a?(String) ? [value.to_str] : value end |
#cache_control ⇒ Object
52 53 54 |
# File 'lib/tzispa/http/response.rb', line 52 def cache_control headers['Cache-control'] end |
#cache_private ⇒ Object
56 57 58 |
# File 'lib/tzispa/http/response.rb', line 56 def cache_private add_cache_control 'private' end |
#calculate_content_length? ⇒ Boolean
76 77 78 |
# File 'lib/tzispa/http/response.rb', line 76 def calculate_content_length? headers['Content-Type'] && !headers['Content-Length'] && body.is_a?(Array) end |
#custom_headers ⇒ Object
47 48 49 50 |
# File 'lib/tzispa/http/response.rb', line 47 def custom_headers headers['X-Frame-Options'] = 'SAMEORIGIN' headers['X-Powered-By'] = "#{Tzispa::FRAMEWORK_NAME} #{Tzispa::VERSION}" end |
#drop_body? ⇒ Boolean
84 85 86 |
# File 'lib/tzispa/http/response.rb', line 84 def drop_body? DROP_BODY_RESPONSES.include?(status.to_i) end |
#drop_content_info? ⇒ Boolean
80 81 82 |
# File 'lib/tzispa/http/response.rb', line 80 def drop_content_info? status.to_i / 100 == 1 || drop_body? end |
#each ⇒ Object
21 22 23 |
# File 'lib/tzispa/http/response.rb', line 21 def each block_given? ? super : enum_for(:each) end |
#finish ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tzispa/http/response.rb', line 25 def finish result = body if drop_content_info? headers.delete 'Content-Length' headers.delete 'Content-Type' end if drop_body? close result = [] end if calculate_content_length? # if some other code has already set Content-Length, don't muck with it # currently, this would be the static file-handler headers['Content-Length'] = body.inject(0) { |l, p| l + p.bytesize }.to_s end custom_headers [status.to_i, headers, result] end |
#max_age(seconds) ⇒ Object
72 73 74 |
# File 'lib/tzispa/http/response.rb', line 72 def max_age(seconds) add_cache_control "max-age=#{seconds}" end |
#must_revalidate ⇒ Object
68 69 70 |
# File 'lib/tzispa/http/response.rb', line 68 def must_revalidate add_cache_control 'must-revalidate' end |
#no_cache ⇒ Object
64 65 66 |
# File 'lib/tzispa/http/response.rb', line 64 def no_cache add_cache_control 'no-cache' end |
#no_store ⇒ Object
60 61 62 |
# File 'lib/tzispa/http/response.rb', line 60 def no_store add_cache_control 'no-store' end |