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



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

def original_request
  @original_request || @request
end

Instance Method Details

#cache_controlObject

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



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

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)


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

def cached?
  @cached
end

#copy_from_cached!Object

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



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

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)


257
258
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
# File 'lib/httpx/plugins/response_cache.rb', line 257

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



220
221
222
223
# File 'lib/httpx/plugins/response_cache.rb', line 220

def initialize(*)
  super
  @cached = false
end

#mark_as_cached!Object

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



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

def mark_as_cached!
  @cached = true
end

#varyObject

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



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

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

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

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