sleepy 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +26 -0
- data/VERSION +1 -1
- data/lib/sleepy.rb +62 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,5 +1,31 @@
|
|
1
1
|
= sleepy
|
2
2
|
|
3
|
+
Tired of API requests failing or taking a long time on your mashup?
|
4
|
+
|
5
|
+
Sleepy uses memcached to cache API responses for a set time to reduce load on external services, and falls back to the last working response for a request on timeout or failure.
|
6
|
+
|
7
|
+
Based on: http://github.com/mwunsch/weary
|
8
|
+
|
9
|
+
Example:
|
10
|
+
|
11
|
+
class TwitterUser < Weary::Base
|
12
|
+
|
13
|
+
domain "http://twitter.com/users/"
|
14
|
+
|
15
|
+
get "show" do |resource|
|
16
|
+
resource.with = [:id, :user_id, :screen_name]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
user = TwitterUser.new
|
21
|
+
me = user.show(:id => "markwunsch").perform_sleepily
|
22
|
+
puts me["name"]
|
23
|
+
|
24
|
+
(turn off your internet connection)
|
25
|
+
|
26
|
+
me = user.show(:id => "markwunsch").perform_sleepily
|
27
|
+
puts me["name"]
|
28
|
+
|
3
29
|
Description goes here.
|
4
30
|
|
5
31
|
== Note on Patches/Pull Requests
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/lib/sleepy.rb
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'memcached'
|
2
|
+
require 'weary'
|
3
|
+
|
4
|
+
# This requires Memcached to be running on your system.
|
5
|
+
module Weary
|
6
|
+
|
7
|
+
class Request
|
8
|
+
|
9
|
+
def self.sleepy
|
10
|
+
@@sleepy ||= Memcached.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def sleepy
|
14
|
+
self.class.sleepy
|
15
|
+
end
|
16
|
+
|
17
|
+
def round_time(integer, factor)
|
18
|
+
return integer if(integer % factor == 0)
|
19
|
+
return integer - (integer % factor)
|
20
|
+
end
|
21
|
+
|
22
|
+
def perform_sleepily(timeout=60*60*1000, &block)
|
23
|
+
@on_complete = block if block_given?
|
24
|
+
response = perform_sleepily!(timeout)
|
25
|
+
response.value
|
26
|
+
end
|
27
|
+
|
28
|
+
# Redefine the perform method
|
29
|
+
def perform_sleepily!(timeout=60*60*1000, &block)
|
30
|
+
@on_complete = block if block_given?
|
31
|
+
Thread.new {
|
32
|
+
STDERR.puts "Before send: #{uri}"
|
33
|
+
before_send.call(self) if before_send
|
34
|
+
|
35
|
+
STDERR.puts "try nap"
|
36
|
+
nap = sleepy.get("#{round_time(Time.new.to_i, timeout)}:#{uri}") rescue nil
|
37
|
+
|
38
|
+
unless nap.blank?
|
39
|
+
STDERR.puts "Return cached result #{nap.inspect}"
|
40
|
+
nap
|
41
|
+
else
|
42
|
+
req = http.request(request)
|
43
|
+
|
44
|
+
response = Response.new(req, self)
|
45
|
+
begin
|
46
|
+
if response.redirected? && follows?
|
47
|
+
response.follow_redirect
|
48
|
+
else
|
49
|
+
on_complete.call(response) if on_complete
|
50
|
+
response
|
51
|
+
end
|
52
|
+
sleepy.set("#{round_time(Time.new.to_i, timeout)}:#{uri}", response)
|
53
|
+
sleepy.set("0:#{uri}", response)
|
54
|
+
rescue
|
55
|
+
sleepy.get("0:#{uri}") rescue nil
|
56
|
+
end
|
57
|
+
response
|
58
|
+
end
|
59
|
+
}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sleepy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- steflewandowski
|