Class: Tzispa::Http::Response

Inherits:
Rack::Response
  • Object
show all
Defined in:
lib/tzispa/http/response.rb

Constant Summary collapse

DROP_BODY_RESPONSES =
[204, 205, 304].freeze

Instance Method Summary collapse

Constructor Details

#initializeResponse

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_controlObject



52
53
54
# File 'lib/tzispa/http/response.rb', line 52

def cache_control
  headers['Cache-control']
end

#cache_privateObject



56
57
58
# File 'lib/tzispa/http/response.rb', line 56

def cache_private
  add_cache_control 'private'
end

#calculate_content_length?Boolean

Returns:

  • (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_headersObject



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

Returns:

  • (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

Returns:

  • (Boolean)


80
81
82
# File 'lib/tzispa/http/response.rb', line 80

def 
  status.to_i / 100 == 1 || drop_body?
end

#eachObject



21
22
23
# File 'lib/tzispa/http/response.rb', line 21

def each
  block_given? ? super : enum_for(:each)
end

#finishObject



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 
    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_revalidateObject



68
69
70
# File 'lib/tzispa/http/response.rb', line 68

def must_revalidate
  add_cache_control 'must-revalidate'
end

#no_cacheObject



64
65
66
# File 'lib/tzispa/http/response.rb', line 64

def no_cache
  add_cache_control 'no-cache'
end

#no_storeObject



60
61
62
# File 'lib/tzispa/http/response.rb', line 60

def no_store
  add_cache_control 'no-store'
end