Module: Rack::Cache::Headers
- Included in:
- Request, RequestHeaders, Response, ResponseHeaders
- Defined in:
- lib/rack/cache/headers.rb
Overview
Generic HTTP header helper methods. Provides access to headers that can be included in requests and responses. This can be mixed into any object that responds to #headers by returning a Hash.
Instance Method Summary collapse
-
#cache_control ⇒ Object
A Hash of name=value pairs that correspond to the Cache-Control header.
-
#cache_control=(hash) ⇒ Object
Set the Cache-Control header to the values specified by the Hash.
-
#etag ⇒ Object
The literal value of the ETag HTTP header or nil if no ETag is specified.
-
#header?(*names) ⇒ Boolean
Determine if any of the header names exist: if header?(‘Authorization’, ‘Cookie’) …
Instance Method Details
#cache_control ⇒ Object
A Hash of name=value pairs that correspond to the Cache-Control header. Valueless parameters (e.g., must-revalidate, no-store) have a Hash value of true. This method always returns a Hash, empty if no Cache-Control header is present.
22 23 24 25 26 27 28 29 |
# File 'lib/rack/cache/headers.rb', line 22 def cache_control @cache_control ||= headers['Cache-Control'].to_s.split(/\s*[,;]\s*/).inject({}) {|hash,token| name, value = token.split(/\s*=\s*/, 2) hash[name.downcase] = (value || true) unless name.empty? hash }.freeze end |
#cache_control=(hash) ⇒ Object
Set the Cache-Control header to the values specified by the Hash. See the #cache_control method for information on expected Hash structure.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rack/cache/headers.rb', line 33 def cache_control=(hash) value = hash.collect { |key,value| next nil unless value next key if value == true "#{key}=#{value}" }.compact.join(', ') if value.empty? headers.delete('Cache-Control') @cache_control = {} else headers['Cache-Control'] = value @cache_control = hash.dup.freeze end end |
#etag ⇒ Object
The literal value of the ETag HTTP header or nil if no ETag is specified.
50 51 52 |
# File 'lib/rack/cache/headers.rb', line 50 def etag headers['Etag'] end |
#header?(*names) ⇒ Boolean
Determine if any of the header names exist:
if header?('Authorization', 'Cookie')
...
end
14 15 16 |
# File 'lib/rack/cache/headers.rb', line 14 def header?(*names) names.any? { |name| headers.include?(name) } end |