Module: HTTParty::Cache::Response
- Defined in:
- lib/httparty/cache/response.rb
Overview
Extends HTTParty::Response with caching methods
Constant Summary collapse
- X_HEADER =
'x-httparty-cache'
- HIT =
'hit'
- MISS =
'miss'
Instance Method Summary collapse
-
#_dump(_level) ⇒ Object
Prevent stack too deep errors while keeping important information.
-
#age ⇒ Integer
Returns the current age of the request in seconds.
-
#cache_control ⇒ Hash
Parses Cache-Control headers.
-
#cache_key ⇒ String
Generates a cache key.
-
#date ⇒ Time
Returns the request date.
- #fresh! ⇒ Object
- #hit! ⇒ Object
- #hit? ⇒ Boolean
- #miss! ⇒ Object
- #miss? ⇒ Boolean
- #reused? ⇒ Boolean
-
#revalidate? ⇒ Bool
Detects if the response must be revalidated even if still fresh.
-
#stale? ⇒ Bool
The response is stale when it has been cached for more than the allowed max age.
-
#vary ⇒ Hash
Returns the request headers that vary responses.
Instance Method Details
#_dump(_level) ⇒ Object
Prevent stack too deep errors while keeping important information
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/httparty/cache/response.rb', line 17 def _dump(_level) _request = HTTParty::Request.new(request.http_method, request.path, headers: request.[:headers]&.to_hash) _body = response.instance_variable_get(:@body) _read_adapter = _body.is_a?(Net::ReadAdapter) response.instance_variable_set(:@body, nil) if _read_adapter Marshal.dump([_request, response, parsed_response, body]).tap do response.instance_variable_set(:@body, _body) if _read_adapter end end |
#age ⇒ Integer
Returns the current age of the request in seconds
78 79 80 81 82 |
# File 'lib/httparty/cache/response.rb', line 78 def age (Time.now - date).to_i rescue HTTParty::ResponseError 0 end |
#cache_control ⇒ Hash
Parses Cache-Control headers
58 59 60 61 62 63 64 |
# File 'lib/httparty/cache/response.rb', line 58 def cache_control @cache_control ||= headers['Cache-Control'].to_s.downcase.split(',').map(&:strip).map do |c| k, v = c.split('=', 2).map(&:strip) [k.tr('-', '_').to_sym, (v ? Integer(v) : true)] end.to_h end |
#cache_key ⇒ String
Generates a cache key
116 117 118 119 120 121 122 |
# File 'lib/httparty/cache/response.rb', line 116 def cache_key @cache_key ||= begin key = "#{uri}##{URI.encode_www_form(vary)}" "httparty-cache:response:#{Digest::SHA2.hexdigest(key)}" end end |
#date ⇒ Time
Returns the request date
69 70 71 72 73 |
# File 'lib/httparty/cache/response.rb', line 69 def date Time.parse(headers['date']) rescue TypeError, ArgumentError raise HTTParty::ResponseError, response end |
#fresh! ⇒ Object
37 38 39 40 |
# File 'lib/httparty/cache/response.rb', line 37 def fresh! response.delete(X_HEADER) headers.delete(X_HEADER) end |
#hit! ⇒ Object
29 30 31 |
# File 'lib/httparty/cache/response.rb', line 29 def hit! response[X_HEADER] = headers[X_HEADER] = HIT end |
#hit? ⇒ Boolean
42 43 44 |
# File 'lib/httparty/cache/response.rb', line 42 def hit? headers[X_HEADER] == HIT end |
#miss! ⇒ Object
33 34 35 |
# File 'lib/httparty/cache/response.rb', line 33 def miss! response[X_HEADER] = headers[X_HEADER] = MISS end |
#miss? ⇒ Boolean
46 47 48 |
# File 'lib/httparty/cache/response.rb', line 46 def miss? headers[X_HEADER] == MISS end |
#reused? ⇒ Boolean
50 51 52 |
# File 'lib/httparty/cache/response.rb', line 50 def reused? headers.key?(X_HEADER) end |
#revalidate? ⇒ Bool
Detects if the response must be revalidated even if still fresh.
96 97 98 99 100 101 102 103 104 |
# File 'lib/httparty/cache/response.rb', line 96 def revalidate? cache_control.empty? || cache_control[:no_store] || cache_control[:no_cache] || ( cache_control[:max_age]&.zero? && cache_control[:must_revalidate] ) end |
#stale? ⇒ Bool
The response is stale when it has been cached for more than the allowed max age
88 89 90 |
# File 'lib/httparty/cache/response.rb', line 88 def stale? !cache_control[:max_age].nil? && age > cache_control[:max_age] end |
#vary ⇒ Hash
Returns the request headers that vary responses
109 110 111 |
# File 'lib/httparty/cache/response.rb', line 109 def vary @vary ||= request.[:headers].to_h.slice(*headers['vary'].to_s.split(',').map(&:strip).sort).transform_keys(&:downcase) end |