Module: HTTParty::Cache::Response

Defined in:
lib/httparty/cache/response.rb

Overview

Extends HTTParty::Response with caching methods

Constant Summary collapse

X_HEADER =
'x-httparty-cache'
HIT =
'hit'
MISS =
'miss'

Instance Method Summary collapse

Instance Method Details

#_dump(_level) ⇒ Object

Prevent stack too deep errors while keeping important information



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/httparty/cache/response.rb', line 17

def _dump(_level)
  _request = HTTParty::Request.new(request.http_method, request.path, headers: request.options[:headers]&.to_hash)
  _body = response.instance_variable_get(:@body)
  _read_adapter = _body.is_a?(Net::ReadAdapter)

  response.instance_variable_set(:@body, nil) if _read_adapter

  Marshal.dump([_request, response, parsed_response, body]).tap do
    response.instance_variable_set(:@body, _body) if _read_adapter
  end
end

#ageInteger

Returns the current age of the request in seconds

Returns:

  • (Integer)


78
79
80
81
82
# File 'lib/httparty/cache/response.rb', line 78

def age
  (Time.now - date).to_i
rescue HTTParty::ResponseError
  0
end

#cache_controlHash

Parses Cache-Control headers



58
59
60
61
62
63
64
# File 'lib/httparty/cache/response.rb', line 58

def cache_control
  @cache_control ||= headers['Cache-Control'].to_s.downcase.split(',').map(&:strip).map do |c|
    k, v = c.split('=', 2).map(&:strip)

    [k.tr('-', '_').to_sym, (v ? Integer(v) : true)]
  end.to_h
end

#cache_keyString

Generates a cache key

Returns:

  • (String)


116
117
118
119
120
121
122
# File 'lib/httparty/cache/response.rb', line 116

def cache_key
  @cache_key ||=
    begin
      key = "#{uri}##{URI.encode_www_form(vary)}"
      "httparty-cache:response:#{Digest::SHA2.hexdigest(key)}"
    end
end

#dateTime

Returns the request date

Returns:

  • (Time)


69
70
71
72
73
# File 'lib/httparty/cache/response.rb', line 69

def date
  Time.parse(headers['date'])
rescue TypeError, ArgumentError
  raise HTTParty::ResponseError, response
end

#fresh!Object



37
38
39
40
# File 'lib/httparty/cache/response.rb', line 37

def fresh!
  response.delete(X_HEADER)
  headers.delete(X_HEADER)
end

#hit!Object



29
30
31
# File 'lib/httparty/cache/response.rb', line 29

def hit!
  response[X_HEADER] = headers[X_HEADER] = HIT
end

#hit?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/httparty/cache/response.rb', line 42

def hit?
  headers[X_HEADER] == HIT
end

#miss!Object



33
34
35
# File 'lib/httparty/cache/response.rb', line 33

def miss!
  response[X_HEADER] = headers[X_HEADER] = MISS
end

#miss?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/httparty/cache/response.rb', line 46

def miss?
  headers[X_HEADER] == MISS
end

#reused?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/httparty/cache/response.rb', line 50

def reused?
  headers.key?(X_HEADER)
end

#revalidate?Bool

Detects if the response must be revalidated even if still fresh.



96
97
98
99
100
101
102
103
104
# File 'lib/httparty/cache/response.rb', line 96

def revalidate?
  cache_control.empty? ||
    cache_control[:no_store] ||
    cache_control[:no_cache] ||
    (
      cache_control[:max_age]&.zero? &&
      cache_control[:must_revalidate]
    )
end

#stale?Bool

The response is stale when it has been cached for more than the allowed max age

Returns:

  • (Bool)


88
89
90
# File 'lib/httparty/cache/response.rb', line 88

def stale?
  !cache_control[:max_age].nil? && age > cache_control[:max_age]
end

#varyHash

Returns the request headers that vary responses

Returns:

  • (Hash)


109
110
111
# File 'lib/httparty/cache/response.rb', line 109

def vary
  @vary ||= request.options[:headers].to_h.slice(*headers['vary'].to_s.split(',').map(&:strip).sort).transform_keys(&:downcase)
end