Class: ApiAdaptor::Response::CacheControl
- Inherits:
-
Hash
- Object
- Hash
- ApiAdaptor::Response::CacheControl
- Defined in:
- lib/api_adaptor/response.rb
Overview
Parses and provides access to HTTP Cache-Control header directives.
Constant Summary collapse
- PATTERN =
Regex pattern for parsing Cache-Control directives
/([-a-z]+)(?:\s*=\s*([^,\s]+))?,?+/i
Instance Method Summary collapse
-
#initialize(value = nil) ⇒ CacheControl
constructor
Initializes a new CacheControl object by parsing a header value.
-
#max_age ⇒ Integer?
Returns the max-age directive value.
-
#must_revalidate? ⇒ Boolean
True if must-revalidate directive is present.
-
#no_cache? ⇒ Boolean
True if no-cache directive is present.
-
#no_store? ⇒ Boolean
True if no-store directive is present.
-
#private? ⇒ Boolean
True if cache is private.
-
#proxy_revalidate? ⇒ Boolean
True if proxy-revalidate directive is present.
-
#public? ⇒ Boolean
True if cache is public.
-
#reverse_max_age ⇒ Integer?
(also: #r_maxage)
Returns the r-maxage directive value.
-
#shared_max_age ⇒ Integer?
(also: #s_maxage)
Returns the s-maxage (shared max age) directive value.
- #to_s ⇒ Object
Constructor Details
#initialize(value = nil) ⇒ CacheControl
Initializes a new CacheControl object by parsing a header value
43 44 45 46 |
# File 'lib/api_adaptor/response.rb', line 43 def initialize(value = nil) super() parse(value) end |
Instance Method Details
#max_age ⇒ Integer?
Returns the max-age directive value
81 82 83 |
# File 'lib/api_adaptor/response.rb', line 81 def max_age self["max-age"].to_i if key?("max-age") end |
#must_revalidate? ⇒ Boolean
Returns true if must-revalidate directive is present.
69 70 71 |
# File 'lib/api_adaptor/response.rb', line 69 def must_revalidate? self["must-revalidate"] end |
#no_cache? ⇒ Boolean
Returns true if no-cache directive is present.
59 60 61 |
# File 'lib/api_adaptor/response.rb', line 59 def no_cache? self["no-cache"] end |
#no_store? ⇒ Boolean
Returns true if no-store directive is present.
64 65 66 |
# File 'lib/api_adaptor/response.rb', line 64 def no_store? self["no-store"] end |
#private? ⇒ Boolean
Returns true if cache is private.
54 55 56 |
# File 'lib/api_adaptor/response.rb', line 54 def private? self["private"] end |
#proxy_revalidate? ⇒ Boolean
Returns true if proxy-revalidate directive is present.
74 75 76 |
# File 'lib/api_adaptor/response.rb', line 74 def proxy_revalidate? self["proxy-revalidate"] end |
#public? ⇒ Boolean
Returns true if cache is public.
49 50 51 |
# File 'lib/api_adaptor/response.rb', line 49 def public? self["public"] end |
#reverse_max_age ⇒ Integer? Also known as: r_maxage
Returns the r-maxage directive value
Note: r-maxage is a non-standard Cache-Control directive and is not part of RFC 7234. This method exists for compatibility with custom cache implementations.
91 92 93 |
# File 'lib/api_adaptor/response.rb', line 91 def reverse_max_age self["r-maxage"].to_i if key?("r-maxage") end |
#shared_max_age ⇒ Integer? Also known as: s_maxage
Returns the s-maxage (shared max age) directive value
The s-maxage directive is like max-age but only applies to shared caches (e.g., CDNs, proxies). It overrides max-age for shared caches.
102 103 104 |
# File 'lib/api_adaptor/response.rb', line 102 def shared_max_age self["s-maxage"].to_i if key?("s-maxage") end |
#to_s ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/api_adaptor/response.rb', line 107 def to_s directives = [] values = [] each do |key, value| if value == true directives << key elsif value values << "#{key}=#{value}" end end (directives.sort + values.sort).join(", ") end |