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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject

Returns the value of attribute count.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def count
  @count
end

#dateObject

Returns the value of attribute date.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def date
  @date
end

#expiresObject

Returns the value of attribute expires.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def expires
  @expires
end

#first_usedObject

Returns the value of attribute first_used.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def first_used
  @first_used
end

#last_usedObject

Returns the value of attribute last_used.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def last_used
  @last_used
end

#responseObject

Returns the value of attribute response.



201
202
203
# File 'lib/rbot/core/utils/httputil.rb', line 201

def response
  @response
end

Class Method Details

.maybe_new(resp) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/rbot/core/utils/httputil.rb', line 203

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

#expired?Boolean

Returns:

  • (Boolean)


233
234
235
236
237
238
239
# File 'lib/rbot/core/utils/httputil.rb', line 233

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

#revalidate(resp = self.response) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rbot/core/utils/httputil.rb', line 252

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

#setup_headers(hdr) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/rbot/core/utils/httputil.rb', line 241

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

#useObject



226
227
228
229
230
231
# File 'lib/rbot/core/utils/httputil.rb', line 226

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