Class: Rack::AcornCache::ServerResponse
- Inherits:
-
Response
- Object
- Response
- Rack::AcornCache::ServerResponse
show all
- Defined in:
- lib/acorn_cache/server_response.rb
Constant Summary
collapse
- CACHEABLE_STATUS_CODES =
[200, 203, 300, 301, 302, 404, 410]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(status, headers, body) ⇒ ServerResponse
Returns a new instance of ServerResponse.
9
10
11
12
13
14
|
# File 'lib/acorn_cache/server_response.rb', line 9
def initialize(status, , body)
@status = status
=
@body = body
= .new(["Cache-Control"])
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
84
85
86
|
# File 'lib/acorn_cache/server_response.rb', line 84
def method_missing(method, *args)
.send(method, *args)
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
7
8
9
|
# File 'lib/acorn_cache/server_response.rb', line 7
def body
@body
end
|
Returns the value of attribute cache_control_header.
7
8
9
|
# File 'lib/acorn_cache/server_response.rb', line 7
def
end
|
Returns the value of attribute headers.
7
8
9
|
# File 'lib/acorn_cache/server_response.rb', line 7
def
end
|
#status ⇒ Object
Returns the value of attribute status.
7
8
9
|
# File 'lib/acorn_cache/server_response.rb', line 7
def status
@status
end
|
Instance Method Details
#body_string ⇒ Object
37
38
39
40
41
|
# File 'lib/acorn_cache/server_response.rb', line 37
def body_string
result = ""
body.each { |part| result << part }
result
end
|
#cache!(cache_key) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/acorn_cache/server_response.rb', line 47
def cache!(cache_key)
update_date!
.delete("Set-Cookie")
CacheWriter.write(cache_key, serialize)
self
end
|
#cacheable? ⇒ Boolean
20
21
22
23
|
# File 'lib/acorn_cache/server_response.rb', line 20
def cacheable?
[:private?, :no_store?].none? { |directive| send(directive) } &&
CACHEABLE_STATUS_CODES.include?(status)
end
|
29
30
31
|
# File 'lib/acorn_cache/server_response.rb', line 29
def
["ETag"]
end
|
#serialize ⇒ Object
33
34
35
|
# File 'lib/acorn_cache/server_response.rb', line 33
def serialize
{ status: status, headers: , body: body_string }.to_json
end
|
#status_304? ⇒ Boolean
25
26
27
|
# File 'lib/acorn_cache/server_response.rb', line 25
def status_304?
status == 304
end
|
#to_a ⇒ Object
43
44
45
|
# File 'lib/acorn_cache/server_response.rb', line 43
def to_a
[status, , body]
end
|
#update_date! ⇒ Object
16
17
18
|
# File 'lib/acorn_cache/server_response.rb', line 16
def update_date!
["Date"] = Time.now.httpdate unless ["Date"]
end
|
#update_with_page_rules!(page_rule) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/acorn_cache/server_response.rb', line 54
def update_with_page_rules!(page_rule)
if page_rule[:must_revalidate]
self.no_cache = true
self.must_revalidate = true
self.private = nil
self.max_age = nil
self.s_maxage = nil
self.no_store = nil
end
if page_rule[:acorn_cache_ttl] || page_rule[:browser_cache_ttl]
self.no_cache = nil
self.no_store = nil
self.must_revalidate = nil
end
if page_rule[:acorn_cache_ttl]
self.max_age = nil
self.s_maxage = page_rule[:acorn_cache_ttl]
self.private = nil
end
if page_rule[:browser_cache_ttl]
self.max_age = page_rule[:browser_cache_ttl]
end
["Cache-Control"] = .to_s
self
end
|