Class ::Utils::HttpUtil::CachedObject
In: lib/rbot/core/utils/httputil.rb
Parent: Object
User HTTPResponse BasicUserMessage Bot\n[lib/rbot/core/remote.rb\nlib/rbot/core/utils/extends.rb\nlib/rbot/core/utils/filters.rb\nlib/rbot/core/utils/wordlist.rb] HttpUtil lib/rbot/core/userdata.rb lib/rbot/core/utils/httputil.rb lib/rbot/core/utils/extends.rb lib/rbot/core/remote.rb lib/rbot/core/utils/httputil.rb ParseTime Utils (null) dot/m_15_0.png

Methods

expired?   maybe_new   new   revalidate   setup_headers   use  

Attributes

count  [RW] 
date  [RW] 
expires  [RW] 
first_used  [RW] 
last_used  [RW] 
response  [RW] 

Public Class methods

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 203
203:     def self.maybe_new(resp)
204:       debug "maybe new #{resp}"
205:       return nil if resp.no_cache
206:       return nil unless Net::HTTPOK === resp ||
207:       Net::HTTPMovedPermanently === resp ||
208:       Net::HTTPFound === resp ||
209:       Net::HTTPPartialContent === resp
210: 
211:       cc = resp['cache-control']
212:       return nil if cc && (cc =~ /no-cache/i)
213: 
214:       date = Time.now
215:       if d = resp['date']
216:         date = Time.httpdate(d)
217:       end
218: 
219:       return nil if resp['expires'] && (Time.httpdate(resp['expires']) < date)
220: 
221:       debug "creating cache obj"
222: 
223:       self.new(resp)
224:     end

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 277
277:     def initialize(resp)
278:       @response = resp
279:       begin
280:         self.revalidate
281:         self.response.raw_body
282:       rescue Exception => e
283:         error e
284:         raise e
285:       end
286:     end

Public Instance methods

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 233
233:     def expired?
234:       debug "checking expired?"
235:       if cc = self.response['cache-control'] && cc =~ /must-revalidate/
236:         return true
237:       end
238:       return self.expires < Time.now
239:     end

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 252
252:     def revalidate(resp = self.response)
253:       @count = 0
254:       self.use
255:       self.date = resp.key?('date') ? Time.httpdate(resp['date']) : Time.now
256: 
257:       cc = resp['cache-control']
258:       if cc && (cc =~ /max-age=(\d+)/)
259:         self.expires = self.date + $1.to_i
260:       elsif resp.key?('expires')
261:         self.expires = Time.httpdate(resp['expires'])
262:       elsif lm = resp['last-modified']
263:         delta = self.date - Time.httpdate(lm)
264:         delta = 10 if delta <= 0
265:         delta /= 5
266:         self.expires = self.date + delta
267:       else
268:         self.expires = self.date + 300
269:       end
270:       # self.expires = Time.now + 10 # DEBUG
271:       debug "expires on #{self.expires}"
272: 
273:       return true
274:     end

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 241
241:     def setup_headers(hdr)
242:       hdr['if-modified-since'] = self.date.rfc2822
243: 
244:       debug "ims == #{hdr['if-modified-since']}"
245: 
246:       if etag = self.response['etag']
247:         hdr['if-none-match'] = etag
248:         debug "etag: #{etag}"
249:       end
250:     end

[Source]

     # File lib/rbot/core/utils/httputil.rb, line 226
226:     def use
227:       now = Time.now
228:       @first_used = now if @count == 0
229:       @last_used = now
230:       @count += 1
231:     end

[Validate]