Class: Weary::Request
- Inherits:
-
Object
- Object
- Weary::Request
- Defined in:
- lib/sleepy.rb
Class Method Summary collapse
Instance Method Summary collapse
- #perform_sleepily(timeout = 60*60*1000, &block) ⇒ Object
-
#perform_sleepily!(timeout = 60*60*1000, &block) ⇒ Object
Redefine the perform method.
- #round_time(integer, factor) ⇒ Object
- #sleepy ⇒ Object
Class Method Details
.sleepy ⇒ Object
16 17 18 |
# File 'lib/sleepy.rb', line 16 def self.sleepy @@sleepy ||= Memcached.new end |
Instance Method Details
#perform_sleepily(timeout = 60*60*1000, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/sleepy.rb', line 29 def perform_sleepily(timeout=60*60*1000, &block) @on_complete = block if block_given? if HAS_MEMCACHE response = perform_sleepily!(timeout) else response = perform end response.value end |
#perform_sleepily!(timeout = 60*60*1000, &block) ⇒ Object
Redefine the perform method
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sleepy.rb', line 40 def perform_sleepily!(timeout=60*60*1000, &block) @on_complete = block if block_given? Thread.new { STDERR.puts "Before send: #{uri}" before_send.call(self) if before_send STDERR.puts "try nap" nap = sleepy.get("#{round_time(Time.new.to_i, timeout)}:#{uri}") rescue nil unless nap.blank? STDERR.puts "Return cached result #{nap.inspect}" nap else req = http.request(request) response = Response.new(req, self) begin if response.redirected? response = response.follow_redirect else on_complete.call(response) if on_complete response end sleepy.set("#{round_time(Time.new.to_i, timeout)}:#{uri}", response) sleepy.set("0:#{uri}", response) rescue sleepy.get("0:#{uri}") rescue nil end response end } end |
#round_time(integer, factor) ⇒ Object
24 25 26 27 |
# File 'lib/sleepy.rb', line 24 def round_time(integer, factor) return integer if(integer % factor == 0) return integer - (integer % factor) end |
#sleepy ⇒ Object
20 21 22 |
# File 'lib/sleepy.rb', line 20 def sleepy self.class.sleepy end |