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

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

Instance Method Summary collapse

Instance Method Details

#cache_controlObject



141
142
143
144
145
146
147
148
149
# File 'lib/httpx/plugins/response_cache.rb', line 141

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
# File 'lib/httpx/plugins/response_cache.rb', line 104

def copy_from_cached(other)
  @body = other.body.dup

  @body.rewind
end

#fresh?Boolean

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



111
112
113
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
# File 'lib/httpx/plugins/response_cache.rb', line 111

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



151
152
153
154
155
156
157
158
159
# File 'lib/httpx/plugins/response_cache.rb', line 151

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

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

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