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.


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

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.



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

def etag
  @etag
end

#last_modifiedObject (readonly)

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



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

def last_modified
  @last_modified
end

#payloadObject (readonly)

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



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

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.



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

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)


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

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)


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

def cacheable_in_shared_cache?
  cacheable?(true)
end

#dateObject

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

Returns the Time object.



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

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)


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

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.



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

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)


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

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’.



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

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.



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

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.



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

def ttl
  max_age - age if max_age
end