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

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!
  headers['Date'] ||= @now.httpdate

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



79
80
81
# File 'lib/faraday/http_cache/response.rb', line 79

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

#cacheable?Boolean

Internal: Checks if the response can be cached by the client. 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)


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

def cacheable?
  return false if cache_control.private? || cache_control.no_store?

  cacheable_status_code? && (validateable? || fresh?)
end

#dateObject

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

Returns the Time object.



94
95
96
# File 'lib/faraday/http_cache/response.rb', line 94

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



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

def max_age
  cache_control.shared_max_age ||
    cache_control.max_age ||
    (expires && (expires - date))
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’.



125
126
127
# File 'lib/faraday/http_cache/response.rb', line 125

def serializable_hash
  @payload.slice(:status, :body, :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.



116
117
118
119
# File 'lib/faraday/http_cache/response.rb', line 116

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.



87
88
89
# File 'lib/faraday/http_cache/response.rb', line 87

def ttl
  max_age - age if max_age
end