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



231
232
233
# File 'lib/httpx/plugins/response_cache.rb', line 231

def original_request
  @original_request || @request
end

Instance Method Details

#cache_controlObject

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



295
296
297
298
299
300
301
302
303
# File 'lib/httpx/plugins/response_cache.rb', line 295

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)


236
237
238
# File 'lib/httpx/plugins/response_cache.rb', line 236

def cached?
  @cached
end

#copy_from_cached!Object

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



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/httpx/plugins/response_cache.rb', line 246

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)


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
290
291
292
# File 'lib/httpx/plugins/response_cache.rb', line 262

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



225
226
227
228
# File 'lib/httpx/plugins/response_cache.rb', line 225

def initialize(*)
  super
  @cached = false
end

#mark_as_cached!Object

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



241
242
243
# File 'lib/httpx/plugins/response_cache.rb', line 241

def mark_as_cached!
  @cached = true
end

#varyObject

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



306
307
308
309
310
311
312
313
314
# File 'lib/httpx/plugins/response_cache.rb', line 306

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

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

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