Module: HTTPX::Plugins::ResponseCache::ResponseMethods
- Defined in:
- lib/httpx/plugins/response_cache.rb
Instance Attribute Summary collapse
-
#original_request ⇒ Object
a copy of the request this response was originally cached from.
-
#revalidated_at ⇒ Object
writeonly
Sets the attribute revalidated_at.
Instance Method Summary collapse
-
#cache_control ⇒ Object
returns the “cache-control” directives as an Array of String(s).
-
#cached? ⇒ Boolean
whether this Response was duplicated from a previously HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.
-
#copy_from_cached! ⇒ Object
eager-copies the response headers and body from HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.
-
#fresh? ⇒ Boolean
A response is fresh if its age has not yet exceeded its freshness lifetime.
- #initialize ⇒ Object
-
#mark_as_cached! ⇒ Object
sets this Response as being duplicated from a previously cached response.
-
#vary ⇒ Object
returns the “vary” header value as an Array of (String) headers.
Instance Attribute Details
#original_request ⇒ Object
a copy of the request this response was originally cached from
232 233 234 |
# File 'lib/httpx/plugins/response_cache.rb', line 232 def original_request @original_request || @request end |
#revalidated_at=(value) ⇒ Object (writeonly)
Sets the attribute revalidated_at
223 224 225 |
# File 'lib/httpx/plugins/response_cache.rb', line 223 def revalidated_at=(value) @revalidated_at = value end |
Instance Method Details
#cache_control ⇒ Object
returns the “cache-control” directives as an Array of String(s).
298 299 300 301 302 303 304 |
# File 'lib/httpx/plugins/response_cache.rb', line 298 def cache_control return @cache_control if defined?(@cache_control) @cache_control = begin @headers["cache-control"].split(/ *, */) if @headers.key?("cache-control") end end |
#cached? ⇒ Boolean
whether this Response was duplicated from a previously HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.
237 238 239 |
# File 'lib/httpx/plugins/response_cache.rb', line 237 def cached? @cached end |
#copy_from_cached! ⇒ Object
eager-copies the response headers and body from HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/httpx/plugins/response_cache.rb', line 247 def copy_from_cached! cached_response = @request.cached_response return unless cached_response # 304 responses do not have content-type, which are needed for decoding. @headers = @headers.class.new(cached_response.headers.merge(@headers)) @body = cached_response.body.dup @body.rewind cached_response.revalidated_at = date end |
#fresh? ⇒ Boolean
A response is fresh if its age has not yet exceeded its freshness lifetime. other (#cache_control} directives may influence the outcome, as per the rules from the rfc
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/httpx/plugins/response_cache.rb', line 265 def fresh? if cache_control return false if cache_control.include?("no-cache") return true if cache_control.include?("immutable") # check age: max-age max_age = cache_control.find { |directive| directive.start_with?("s-maxage") } max_age ||= cache_control.find { |directive| directive.start_with?("max-age") } max_age = max_age[/age=(\d+)/, 1] if max_age max_age = max_age.to_i if max_age return max_age > age if max_age end # check age: expires if @headers.key?("expires") begin expires = Time.httpdate(@headers["expires"]) rescue ArgumentError return false end return (expires - Time.now).to_i.positive? end false end |
#initialize ⇒ Object
225 226 227 228 229 |
# File 'lib/httpx/plugins/response_cache.rb', line 225 def initialize(*) super @cached = false @revalidated_at = nil end |
#mark_as_cached! ⇒ Object
sets this Response as being duplicated from a previously cached response.
242 243 244 |
# File 'lib/httpx/plugins/response_cache.rb', line 242 def mark_as_cached! @cached = true end |
#vary ⇒ Object
returns the “vary” header value as an Array of (String) headers.
307 308 309 310 311 312 313 |
# File 'lib/httpx/plugins/response_cache.rb', line 307 def vary return @vary if defined?(@vary) @vary = begin @headers["vary"].split(/ *, */).map(&:downcase) if @headers.key?("vary") end end |