Module: HTTPX::Plugins::ResponseCache::ResponseMethods

Defined in:
lib/httpx/plugins/response_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#original_requestObject

a copy of the request this response was originally cached from



228
229
230
# File 'lib/httpx/plugins/response_cache.rb', line 228

def original_request
  @original_request || @request
end

Instance Method Details

#cache_controlObject

returns the “cache-control” directives as an Array of String(s).



292
293
294
295
296
297
298
299
300
# File 'lib/httpx/plugins/response_cache.rb', line 292

def cache_control
  return @cache_control if defined?(@cache_control)

  @cache_control = begin
    return unless @headers.key?("cache-control")

    @headers["cache-control"].split(/ *, */)
  end
end

#cached?Boolean

whether this Response was duplicated from a previously HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.

Returns:

  • (Boolean)


233
234
235
# File 'lib/httpx/plugins/response_cache.rb', line 233

def cached?
  @cached
end

#copy_from_cached!Object

eager-copies the response headers and body from HTTPX::Plugins::ResponseCache::RequestMethods#cached_response.



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/httpx/plugins/response_cache.rb', line 243

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
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

Returns:

  • (Boolean)


259
260
261
262
263
264
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
# File 'lib/httpx/plugins/response_cache.rb', line 259

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

#initializeObject



222
223
224
225
# File 'lib/httpx/plugins/response_cache.rb', line 222

def initialize(*)
  super
  @cached = false
end

#mark_as_cached!Object

sets this Response as being duplicated from a previously cached response.



238
239
240
# File 'lib/httpx/plugins/response_cache.rb', line 238

def mark_as_cached!
  @cached = true
end

#varyObject

returns the “vary” header value as an Array of (String) headers.



303
304
305
306
307
308
309
310
311
# File 'lib/httpx/plugins/response_cache.rb', line 303

def vary
  return @vary if defined?(@vary)

  @vary = begin
    return unless @headers.key?("vary")

    @headers["vary"].split(/ *, */).map(&:downcase)
  end
end