Class: HTTP::Request::Caching

Inherits:
HTTP::Request show all
Defined in:
lib/http/request/caching.rb

Overview

Decorator for requests to provide convenience methods related to caching.

Constant Summary collapse

INVALIDATING_METHODS =
[:post, :put, :delete, :patch].freeze
CACHEABLE_METHODS =
[:get, :head].freeze

Constants inherited from HTTP::Request

METHODS, PORTS, SCHEMES, USER_AGENT

Instance Attribute Summary collapse

Attributes inherited from HTTP::Request

#body, #proxy, #scheme, #uri, #verb, #version

Attributes included from Headers::Mixin

#headers

Instance Method Summary collapse

Methods inherited from HTTP::Request

#connect_using_proxy, #headline, #include_proxy_authorization_header, #proxy_authorization_header, #proxy_connect_header, #proxy_connect_headers, #redirect, #socket_host, #socket_port, #stream, #using_authenticated_proxy?, #using_proxy?

Methods included from Headers::Mixin

#[], #[]=

Constructor Details

#initialize(obj) ⇒ Caching

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inits a new instance



17
18
19
20
21
# File 'lib/http/request/caching.rb', line 17

def initialize(obj)
  super
  @requested_at = nil
  @received_at  = nil
end

Instance Attribute Details

#sent_atObject

When was this request sent to the server



13
14
15
# File 'lib/http/request/caching.rb', line 13

def sent_at
  @sent_at
end

Instance Method Details

#cache_headersHTTP::Cache::Headers

Returns cache control helper for this request.

Returns:



68
69
70
# File 'lib/http/request/caching.rb', line 68

def cache_headers
  @cache_headers ||= HTTP::Cache::Headers.new headers
end

#cacheable?Boolean

Returns true if request is cacheable.

Returns:

  • (Boolean)

    true if request is cacheable



39
40
41
42
# File 'lib/http/request/caching.rb', line 39

def cacheable?
  CACHEABLE_METHODS.include?(verb) &&
    !cache_headers.no_store?
end

#cachingHTTP::Request::Caching



24
25
26
# File 'lib/http/request/caching.rb', line 24

def caching
  self
end

#conditional_on_changes_to(cached_response) ⇒ HTTP::Request::Caching

one but conditional on the resource having changed since cached_response

Returns:



60
61
62
63
64
# File 'lib/http/request/caching.rb', line 60

def conditional_on_changes_to(cached_response)
  self.class.new HTTP::Request.new(
    verb, uri, headers.merge(conditional_headers_for(cached_response)),
    proxy, body, version).caching
end

#envObject



72
73
74
# File 'lib/http/request/caching.rb', line 72

def env
  {"rack-cache.cache_key" => lambda { |r| r.uri.to_s }}
end

#invalidates_cache?Boolean

Returns true iff request demands the resources cache entry be invalidated.

Returns:

  • (Boolean)

    true iff request demands the resources cache entry be invalidated



31
32
33
34
# File 'lib/http/request/caching.rb', line 31

def invalidates_cache?
  INVALIDATING_METHODS.include?(verb) ||
    cache_headers.no_store?
end

#skips_cache?Boolean

request demands that the response be revalidated by the origin server.

Returns:

  • (Boolean)

    true iff the cache control info of this



49
50
51
52
53
# File 'lib/http/request/caching.rb', line 49

def skips_cache?
  0 == cache_headers.max_age       ||
    cache_headers.must_revalidate? ||
    cache_headers.no_cache?
end