Class: Hurley::HttpCache::Response

Inherits:
Response
  • Object
show all
Defined in:
lib/hurley/http_cache/response.rb

Overview

Internal: Delegator that extends the Hurley::Response class with some behavior required for the HTTP caching mechanism.

Constant Summary collapse

CACHEABLE_STATUS_CODES =

Internal: List of status codes that can be cached:

  • 200 - ‘OK’

  • 203 - ‘Non-Authoritative Information’

  • 300 - ‘Multiple Choices’

  • 301 - ‘Moved Permanently’

  • 302 - ‘Found’

  • 404 - ‘Not Found’

  • 410 - ‘Gone’

[200, 203, 300, 301, 302, 404, 410]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Internal: Initialize a new Response object, and sets the ‘Date’ header if its missing.



23
24
25
26
27
# File 'lib/hurley/http_cache/response.rb', line 23

def initialize(response)
  super(response)
  @now = Time.now
  header['Date'] ||= @now.httpdate
end

Class Method Details

.restore(request, response) ⇒ Object

Internal: Recreate a Response object from a Hurley::Request and a serialized response retrieved from the cache. a

request - A Hurley::Request instance of an incoming request. response - A Hash of a persisted response object.

Returns a Hurley::HttpCache::Response object.



37
38
39
40
41
42
43
44
45
# File 'lib/hurley/http_cache/response.rb', line 37

def self.restore(request, response)
  instance = Hurley::Response.new(request) do |res|
    res.status_code = response['status_code']
    res.header.update(response['header'])
    res.body = response['body']
  end

  new(instance)
end

Instance Method Details

#cacheable_in_private_cache?Boolean

Internal: Check if the response can be cached by the client when the client is acting as a private cache per RFC 2616. This is validated by the ‘Cache-Control’ directives, the response status code and it’s freshness or validation status.

Returns false if the ‘Cache-Control’ says that we can’t store the response, or if isn’t fresh or it can’t be revalidated with the origin server. Otherwise, returns true.

Returns:

  • (Boolean)


68
69
70
# File 'lib/hurley/http_cache/response.rb', line 68

def cacheable_in_private_cache?
  cacheable?(shared: false)
end

#cacheable_in_shared_cache?Boolean

Internal: Check if the response can be cached by the client when the client is acting as a shared cache per RFC 2616. This is validated by the ‘Cache-Control’ directives, the response status code and it’s freshness or validation status.

Returns false if the ‘Cache-Control’ says that we can’t store the response, or it can be stored in private caches only, or if isn’t fresh or it can’t be revalidated with the origin server. Otherwise, returns true.

Returns:

  • (Boolean)


56
57
58
# File 'lib/hurley/http_cache/response.rb', line 56

def cacheable_in_shared_cache?
  cacheable?(shared: true)
end

#etagObject

Internal: Get the ‘ETag’ header.



88
89
90
# File 'lib/hurley/http_cache/response.rb', line 88

def etag
  header['ETag']
end

#fresh?Boolean

Internal: Check the response freshness based on expiration header. The calculated ‘ttl’ should be present and bigger than 0.

Returns true if the response is fresh, otherwise false.

Returns:

  • (Boolean)


83
84
85
# File 'lib/hurley/http_cache/response.rb', line 83

def fresh?
  ttl && ttl > 0
end

#last_modifiedObject

Internal: Get the ‘Last-Modified’ header.



93
94
95
# File 'lib/hurley/http_cache/response.rb', line 93

def last_modified
  header['Last-Modified']
end

#not_modified?Boolean

Internal: Check if the Response returned a ‘Not Modified’ status code.

Returns true if the response status code is 304.

Returns:

  • (Boolean)


75
76
77
# File 'lib/hurley/http_cache/response.rb', line 75

def not_modified?
  status_code == 304
end

#serializable_hashObject

Internal: Get a Hash that represents the response that can be properly serialized.

Returns a Hash.



101
102
103
104
105
106
107
# File 'lib/hurley/http_cache/response.rb', line 101

def serializable_hash
  {
    'status_code' => status_code,
    'header' => header.to_hash,
    'body' => body
  }
end