Class: Faraday::HttpCache::Response

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

Overview

Internal: A class to represent a response from a Faraday request. It decorates the response hash into a smarter object that queries the response headers and status informations about how the caching middleware should handle this specific response.

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, 307, 404, 410].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ Response

Internal: Initialize a new Response with the response payload from a Faraday request.

payload - the response Hash returned by a Faraday request.

:status - the status code from the response.
:response_headers - a 'Hash' like object with the headers.
:body - the response body.


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

def initialize(payload = {})
  @now = Time.now
  @payload = payload
  wrap_headers!
  ensure_date_header!

  @last_modified = headers['Last-Modified']
  @etag = headers['ETag']
end

Instance Attribute Details

#etagObject (readonly)

Internal: Gets the ‘ETag’ header from the headers Hash.



28
29
30
# File 'lib/faraday/http_cache/response.rb', line 28

def etag
  @etag
end

#last_modifiedObject (readonly)

Internal: Gets the ‘Last-Modified’ header from the headers Hash.



25
26
27
# File 'lib/faraday/http_cache/response.rb', line 25

def last_modified
  @last_modified
end

#payloadObject (readonly)

Internal: Gets the actual response Hash (status, headers and body).



22
23
24
# File 'lib/faraday/http_cache/response.rb', line 22

def payload
  @payload
end

Instance Method Details

#ageObject

Internal: Gets the response age in seconds.

Returns the ‘Age’ header if present, or subtracts the response ‘date’ from the current time.



91
92
93
# File 'lib/faraday/http_cache/response.rb', line 91

def age
  (headers['Age'] || (@now - date)).to_i
end

#cacheable_in_private_cache?Boolean

Internal: Checks 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)


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

def cacheable_in_private_cache?
  cacheable?(false)
end

#cacheable_in_shared_cache?Boolean

Internal: Checks 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)


71
72
73
# File 'lib/faraday/http_cache/response.rb', line 71

def cacheable_in_shared_cache?
  cacheable?(true)
end

#dateObject

Internal: Parses the ‘Date’ header back into a Time instance.

Returns the Time object.



106
107
108
# File 'lib/faraday/http_cache/response.rb', line 106

def date
  Time.httpdate(headers['Date'])
end

#fresh?Boolean

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

Returns true if the response is fresh, otherwise false.

Returns:

  • (Boolean)


51
52
53
# File 'lib/faraday/http_cache/response.rb', line 51

def fresh?
  !cache_control.no_cache? && ttl && ttl > 0
end

#max_ageObject

Internal: Gets the response max age. The max age is extracted from one of the following:

  • The shared max age directive from the ‘Cache-Control’ header;

  • The max age directive from the ‘Cache-Control’ header;

  • The difference between the ‘Expires’ header and the response date.

Returns the max age value in seconds or nil if all options above fails.



118
119
120
121
122
# File 'lib/faraday/http_cache/response.rb', line 118

def max_age
  cache_control.shared_max_age ||
    cache_control.max_age ||
    (expires && (expires - @now))
end

#not_modified?Boolean

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

Returns true if the response status code is 304.

Returns:

  • (Boolean)


58
59
60
# File 'lib/faraday/http_cache/response.rb', line 58

def not_modified?
  @payload[:status] == 304
end

#serializable_hashObject

Internal: Exposes a representation of the current payload that we can serialize and cache properly.

Returns a ‘Hash’.



137
138
139
140
141
142
143
144
145
# File 'lib/faraday/http_cache/response.rb', line 137

def serializable_hash
  prepare_to_cache

  {
    status: @payload[:status],
    body: @payload[:body],
    response_headers: @payload[:response_headers]
  }
end

#to_response(env) ⇒ Object

Internal: Creates a new ‘Faraday::Response’, merging the stored response with the supplied ‘env’ object.

Returns a new instance of a ‘Faraday::Response’ with the payload.



128
129
130
131
# File 'lib/faraday/http_cache/response.rb', line 128

def to_response(env)
  env.update(@payload)
  Faraday::Response.new(env)
end

#ttlObject

Internal: Calculates the ‘Time to live’ left on the Response.

Returns the remaining seconds for the response, or nil the ‘max_age’ isn’t present.



99
100
101
# File 'lib/faraday/http_cache/response.rb', line 99

def ttl
  max_age - age if max_age
end