Class: Faraday::HttpCache::Response
- Inherits:
-
Object
- Object
- Faraday::HttpCache::Response
- Defined in:
- lib/faraday/http_cache/response.rb
Overview
Internal: A class to represent a response from a Faraday request. It decorates the response hash into a smarter object that queries the response headers and status informations about how the caching middleware should handle this specific response.
Constant Summary collapse
- CACHEABLE_STATUS_CODES =
Internal: List of status codes that can be cached:
- 200 - 'OK'
- 203 - 'Non-Authoritative Information'
- 300 - 'Multiple Choices'
- 301 - 'Moved Permanently'
- 302 - 'Found'
- 404 - 'Not Found'
- 410 - 'Gone'
[200, 203, 300, 301, 302, 307, 404, 410].freeze
Instance Attribute Summary collapse
-
#etag ⇒ Object
readonly
Internal: Gets the 'ETag' header from the headers Hash.
-
#last_modified ⇒ Object
readonly
Internal: Gets the 'Last-Modified' header from the headers Hash.
-
#payload ⇒ Object
readonly
Internal: Gets the actual response Hash (status, headers and body).
Instance Method Summary collapse
-
#age ⇒ Object
Internal: Gets the response age in seconds.
-
#cacheable_in_private_cache? ⇒ Boolean
Internal: Checks if the response can be cached by the client when the client is acting as a private cache per RFC 2616.
-
#cacheable_in_shared_cache? ⇒ Boolean
Internal: Checks if the response can be cached by the client when the client is acting as a shared cache per RFC 2616.
-
#date ⇒ Object
Internal: Parses the 'Date' header back into a Time instance.
-
#fresh? ⇒ Boolean
Internal: Checks the response freshness based on expiration headers.
-
#initialize(payload = {}) ⇒ Response
constructor
Internal: Initialize a new Response with the response payload from a Faraday request.
-
#max_age ⇒ Object
Internal: Gets the response max age.
-
#not_modified? ⇒ Boolean
Internal: Checks if the Response returned a 'Not Modified' status.
-
#serializable_hash ⇒ Object
Internal: Exposes a representation of the current payload that we can serialize and cache properly.
-
#shared_cache_authorized? ⇒ Boolean
Internal: Checks if a shared cache may reuse this response for requests other than the one that carried an 'Authorization' header.
-
#stale_while_revalidate ⇒ Object
Internal: Gets the stale-while-revalidate value in seconds.
-
#stale_while_revalidate? ⇒ Boolean
Internal: Checks if the response is stale but can still be served while revalidating in the background.
-
#to_response(env) ⇒ Object
Internal: Creates a new 'Faraday::Response', merging the stored response with the supplied 'env' object.
-
#ttl ⇒ Object
Internal: Calculates the 'Time to live' left on the Response.
Constructor Details
#initialize(payload = {}) ⇒ Response
Internal: Initialize a new Response with the response payload from a Faraday request.
payload - the response Hash returned by a Faraday request. :status - the status code from the response. :response_headers - a 'Hash' like object with the headers. :body - the response body.
39 40 41 42 43 44 45 46 47 |
# File 'lib/faraday/http_cache/response.rb', line 39 def initialize(payload = {}) @now = Time.now @payload = payload wrap_headers! ensure_date_header! @last_modified = headers['Last-Modified'] @etag = headers['ETag'] end |
Instance Attribute Details
#etag ⇒ Object (readonly)
Internal: Gets the 'ETag' header from the headers Hash.
30 31 32 |
# File 'lib/faraday/http_cache/response.rb', line 30 def etag @etag end |
#last_modified ⇒ Object (readonly)
Internal: Gets the 'Last-Modified' header from the headers Hash.
27 28 29 |
# File 'lib/faraday/http_cache/response.rb', line 27 def last_modified @last_modified end |
#payload ⇒ Object (readonly)
Internal: Gets the actual response Hash (status, headers and body).
24 25 26 |
# File 'lib/faraday/http_cache/response.rb', line 24 def payload @payload end |
Instance Method Details
#age ⇒ Object
Internal: Gets the response age in seconds.
Returns the 'Age' header if present, or subtracts the response 'date' from the current time.
121 122 123 |
# File 'lib/faraday/http_cache/response.rb', line 121 def age (headers['Age'] || (@now - date)).to_i end |
#cacheable_in_private_cache? ⇒ Boolean
Internal: Checks if the response can be cached by the client when the client is acting as a private cache per RFC 2616. This is validated by the 'Cache-Control' directives, the response status code and it's freshness or validation status.
Returns false if the 'Cache-Control' says that we can't store the response, or if isn't fresh or it can't be revalidated with the origin server. Otherwise, returns true.
97 98 99 |
# File 'lib/faraday/http_cache/response.rb', line 97 def cacheable_in_private_cache? cacheable?(false) end |
#cacheable_in_shared_cache? ⇒ Boolean
Internal: Checks if the response can be cached by the client when the client is acting as a shared cache per RFC 2616. This is validated by the 'Cache-Control' directives, the response status code and it's freshness or validation status.
Returns false if the 'Cache-Control' says that we can't store the response, or it can be stored in private caches only, or if isn't fresh or it can't be revalidated with the origin server. Otherwise, returns true.
85 86 87 |
# File 'lib/faraday/http_cache/response.rb', line 85 def cacheable_in_shared_cache? cacheable?(true) end |
#date ⇒ Object
Internal: Parses the 'Date' header back into a Time instance.
Returns the Time object.
136 137 138 |
# File 'lib/faraday/http_cache/response.rb', line 136 def date Time.httpdate(headers['Date']) end |
#fresh? ⇒ Boolean
Internal: Checks the response freshness based on expiration headers. The calculated 'ttl' should be present and bigger than 0.
Returns true if the response is fresh, otherwise false.
53 54 55 |
# File 'lib/faraday/http_cache/response.rb', line 53 def fresh? !cache_control.no_cache? && ttl && ttl > 0 end |
#max_age ⇒ Object
Internal: Gets the response max age. The max age is extracted from one of the following:
- The shared max age directive from the 'Cache-Control' header;
- The max age directive from the 'Cache-Control' header;
- The difference between the 'Expires' header and the response date.
Returns the max age value in seconds or nil if all options above fails.
148 149 150 151 152 |
# File 'lib/faraday/http_cache/response.rb', line 148 def max_age cache_control.shared_max_age || cache_control.max_age || (expires && (expires - @now)) end |
#not_modified? ⇒ Boolean
Internal: Checks if the Response returned a 'Not Modified' status.
Returns true if the response status code is 304.
72 73 74 |
# File 'lib/faraday/http_cache/response.rb', line 72 def not_modified? @payload[:status] == 304 end |
#serializable_hash ⇒ Object
Internal: Exposes a representation of the current payload that we can serialize and cache properly.
Returns a 'Hash'.
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/faraday/http_cache/response.rb', line 174 def serializable_hash prepare_to_cache { status: @payload[:status], body: @payload[:body], response_headers: @payload[:response_headers], reason_phrase: @payload[:reason_phrase] } end |
#shared_cache_authorized? ⇒ Boolean
Internal: Checks if a shared cache may reuse this response for requests other than the one that carried an 'Authorization' header.
RFC 9111 section 3.5: a shared cache must not use a cached response to a request with an 'Authorization' header to satisfy any subsequent request unless the response carries a 'Cache-Control' directive that explicitly allows it. The directives with that effect are 'must-revalidate', 'public' and 's-maxage'.
Returns true if one of those directives is present.
111 112 113 114 115 |
# File 'lib/faraday/http_cache/response.rb', line 111 def cache_control.public? || cache_control.must_revalidate? || !cache_control.shared_max_age.nil? end |
#stale_while_revalidate ⇒ Object
Internal: Gets the stale-while-revalidate value in seconds.
Returns an Integer or nil.
157 158 159 |
# File 'lib/faraday/http_cache/response.rb', line 157 def stale_while_revalidate cache_control.stale_while_revalidate end |
#stale_while_revalidate? ⇒ Boolean
Internal: Checks if the response is stale but can still be served while revalidating in the background.
Returns true when the response has exceeded freshness lifetime, but is still inside the stale-while-revalidate window.
62 63 64 65 66 67 |
# File 'lib/faraday/http_cache/response.rb', line 62 def stale_while_revalidate? return false if cache_control.no_cache? return false unless ttl && stale_while_revalidate ttl <= 0 && -ttl <= stale_while_revalidate end |
#to_response(env) ⇒ Object
Internal: Creates a new 'Faraday::Response', merging the stored response with the supplied 'env' object.
Returns a new instance of a 'Faraday::Response' with the payload.
165 166 167 168 |
# File 'lib/faraday/http_cache/response.rb', line 165 def to_response(env) env.update(@payload) Faraday::Response.new(env) end |
#ttl ⇒ Object
Internal: Calculates the 'Time to live' left on the Response.
Returns the remaining seconds for the response, or nil the 'max_age' isn't present.
129 130 131 |
# File 'lib/faraday/http_cache/response.rb', line 129 def ttl max_age - age if max_age end |