Class: Irc::Utils::HttpUtil::CachedObject

Inherits:
Object
  • Object
show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) count

Returns the value of attribute count



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def count
  @count
end

- (Object) date

Returns the value of attribute date



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def date
  @date
end

- (Object) expires

Returns the value of attribute expires



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def expires
  @expires
end

- (Object) first_used

Returns the value of attribute first_used



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def first_used
  @first_used
end

- (Object) last_used

Returns the value of attribute last_used



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def last_used
  @last_used
end

- (Object) response

Returns the value of attribute response



215
216
217
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 215

def response
  @response
end

Class Method Details

+ (Object) maybe_new(resp)



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 217

def self.maybe_new(resp)
  debug "maybe new #{resp}"
  return nil if resp.no_cache
  return nil unless Net::HTTPOK === resp ||
  Net::HTTPMovedPermanently === resp ||
  Net::HTTPFound === resp ||
  Net::HTTPPartialContent === resp

  cc = resp['cache-control']
  return nil if cc && (cc =~ /no-cache/i)

  date = Time.now
  if d = resp['date']
    date = Time.httpdate(d)
  end

  return nil if resp['expires'] && (Time.httpdate(resp['expires']) < date)

  debug "creating cache obj"

  self.new(resp)
end

Instance Method Details

- (Boolean) expired?

Returns:

  • (Boolean)


247
248
249
250
251
252
253
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 247

def expired?
  debug "checking expired?"
  if cc = self.response['cache-control'] && cc =~ /must-revalidate/
    return true
  end
  return self.expires < Time.now
end

- (Object) revalidate(resp = self.response)



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 266

def revalidate(resp = self.response)
  @count = 0
  self.use
  self.date = resp.key?('date') ? Time.httpdate(resp['date']) : Time.now

  cc = resp['cache-control']
  if cc && (cc =~ /max-age=(\d+)/)
    self.expires = self.date + $1.to_i
  elsif resp.key?('expires')
    self.expires = Time.httpdate(resp['expires'])
  elsif lm = resp['last-modified']
    delta = self.date - Time.httpdate(lm)
    delta = 10 if delta <= 0
    delta /= 5
    self.expires = self.date + delta
  else
    self.expires = self.date + 300
  end
  # self.expires = Time.now + 10 # DEBUG
  debug "expires on #{self.expires}"

  return true
end

- (Object) setup_headers(hdr)



255
256
257
258
259
260
261
262
263
264
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 255

def setup_headers(hdr)
  hdr['if-modified-since'] = self.date.rfc2822

  debug "ims == #{hdr['if-modified-since']}"

  if etag = self.response['etag']
    hdr['if-none-match'] = etag
    debug "etag: #{etag}"
  end
end

- (Object) use



240
241
242
243
244
245
# File '/home/apoc/projects/ruby/rbot/lib/rbot/core/utils/httputil.rb', line 240

def use
  now = Time.now
  @first_used = now if @count == 0
  @last_used = now
  @count += 1
end