Module: HTTPX::Plugins::ResponseCache::ResponseMethods

Defined in:
lib/httpx/plugins/response_cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_controlObject



144
145
146
147
148
149
150
151
152
# File 'lib/httpx/plugins/response_cache.rb', line 144

def cache_control
  return @cache_control if defined?(@cache_control)

  @cache_control = begin
    return unless @headers.key?("cache-control")

    @headers["cache-control"].split(/ *, */)
  end
end

#copy_from_cached(other) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/httpx/plugins/response_cache.rb', line 104

def copy_from_cached(other)
  # 304 responses do not have content-type, which are needed for decoding.
  @headers = @headers.class.new(other.headers.merge(@headers))

  @body = other.body.dup

  @body.rewind
end

#fresh?Boolean

A response is fresh if its age has not yet exceeded its freshness lifetime.

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/httpx/plugins/response_cache.rb', line 114

def fresh?
  if cache_control
    return false if cache_control.include?("no-cache")

    # check age: max-age
    max_age = cache_control.find { |directive| directive.start_with?("s-maxage") }

    max_age ||= cache_control.find { |directive| directive.start_with?("max-age") }

    max_age = max_age[/age=(\d+)/, 1] if max_age

    max_age = max_age.to_i if max_age

    return max_age > age if max_age
  end

  # check age: expires
  if @headers.key?("expires")
    begin
      expires = Time.httpdate(@headers["expires"])
    rescue ArgumentError
      return true
    end

    return (expires - Time.now).to_i.positive?
  end

  true
end

#varyObject



154
155
156
157
158
159
160
161
162
# File 'lib/httpx/plugins/response_cache.rb', line 154

def vary
  return @vary if defined?(@vary)

  @vary = begin
    return unless @headers.key?("vary")

    @headers["vary"].split(/ *, */)
  end
end