mloughran-api_cache 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +25 -4
- data/VERSION.yml +1 -1
- data/lib/api_cache/api.rb +9 -4
- data/lib/api_cache.rb +4 -3
- data/spec/api_cache_spec.rb +0 -1
- data/spec/api_spec.rb +10 -0
- data/spec/spec_helper.rb +2 -0
- metadata +3 -2
data/README.markdown
CHANGED
@@ -1,5 +1,26 @@
|
|
1
|
-
api_cache
|
2
|
-
|
1
|
+
APICache (aka api_cache)
|
2
|
+
========================
|
3
|
+
|
4
|
+
For the impatient
|
5
|
+
-----------------
|
6
|
+
|
7
|
+
# Install
|
8
|
+
sudo gem install mloughran-api_cache -s http://gems.github.com
|
9
|
+
|
10
|
+
# Require
|
11
|
+
require 'rubygems'
|
12
|
+
gem 'mloughran-api_cache'
|
13
|
+
require 'api_cache'
|
14
|
+
|
15
|
+
# Configure
|
16
|
+
APICache.start(APICache::MemoryStore)
|
17
|
+
|
18
|
+
# Use
|
19
|
+
APICache.get("http://twitter.com/statuses/public_timeline.rss")
|
20
|
+
|
21
|
+
For everyone else
|
22
|
+
-----------------
|
23
|
+
|
3
24
|
You want to use the Twitter API but you don't want to die? I have the solution to API caching:
|
4
25
|
|
5
26
|
APICache.get("http://twitter.com/statuses/public_timeline.rss")
|
@@ -49,9 +70,9 @@ Currently there are two stores available: `MemcacheStore` and `MemoryStore`. `Me
|
|
49
70
|
|
50
71
|
APICache.start(APICache::MemoryStore)
|
51
72
|
|
52
|
-
I suppose you'll want to get your hands on this magic
|
73
|
+
I suppose you'll want to get your hands on this magic! Just take a look at the instructions above for the impatient. Well done for reading this first!
|
53
74
|
|
54
|
-
Please send feedback if you think of any other functionality that would be handy.
|
75
|
+
Please send feedback to me [at] mloughran [dot] com if you think of any other functionality that would be handy.
|
55
76
|
|
56
77
|
Copyright
|
57
78
|
=========
|
data/VERSION.yml
CHANGED
data/lib/api_cache/api.rb
CHANGED
@@ -46,20 +46,25 @@ class APICache::API
|
|
46
46
|
get_via_http(key, timeout)
|
47
47
|
end
|
48
48
|
end
|
49
|
-
rescue Timeout::Error, APICache::Invalid
|
50
|
-
raise APICache::CannotFetch
|
49
|
+
rescue Timeout::Error, APICache::Invalid => e
|
50
|
+
raise APICache::CannotFetch, e.message
|
51
51
|
end
|
52
52
|
|
53
53
|
private
|
54
54
|
|
55
55
|
def get_via_http(key, timeout)
|
56
|
-
response =
|
56
|
+
response = redirecting_get(key)
|
57
57
|
case response
|
58
58
|
when Net::HTTPSuccess
|
59
59
|
# 2xx response code
|
60
60
|
response.body
|
61
61
|
else
|
62
|
-
raise APICache::Invalid
|
62
|
+
raise APICache::Invalid, "Invalid http response: #{response.code}"
|
63
63
|
end
|
64
64
|
end
|
65
|
+
|
66
|
+
def redirecting_get(url)
|
67
|
+
r = Net::HTTP.get_response(URI.parse(url))
|
68
|
+
r.header['location'] ? redirecting_get(r.header['location']) : r
|
69
|
+
end
|
65
70
|
end
|
data/lib/api_cache.rb
CHANGED
@@ -51,13 +51,14 @@ class APICache
|
|
51
51
|
value = api.get(key, options[:timeout], &block)
|
52
52
|
cache.set(key, value)
|
53
53
|
value
|
54
|
-
rescue APICache::CannotFetch
|
55
|
-
APICache.logger.log "Failed to fetch new data from API"
|
54
|
+
rescue APICache::CannotFetch => e
|
55
|
+
APICache.logger.log "Failed to fetch new data from API because " \
|
56
|
+
"#{e.class}: #{e.message}"
|
56
57
|
if cache_state == :refetch
|
57
58
|
cache.get(key)
|
58
59
|
else
|
59
60
|
APICache.logger.log "Data not available in the cache or from API"
|
60
|
-
raise APICache::NotAvailableError
|
61
|
+
raise APICache::NotAvailableError, e.message
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
data/spec/api_cache_spec.rb
CHANGED
data/spec/api_spec.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe APICache::API do
|
4
|
+
it "should handle redirecting get requests" do
|
5
|
+
api = APICache::API.new
|
6
|
+
lambda {
|
7
|
+
api.get('http://froogle.google.com', 5)
|
8
|
+
}.should_not raise_error(APICache::CannotFetch)
|
9
|
+
end
|
10
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mloughran-api_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martyn Loughran
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-03-30 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/api_cache/memory_store.rb
|
34
34
|
- lib/api_cache.rb
|
35
35
|
- spec/api_cache_spec.rb
|
36
|
+
- spec/api_spec.rb
|
36
37
|
- spec/spec_helper.rb
|
37
38
|
has_rdoc: true
|
38
39
|
homepage: http://github.com/mloughran/api_cache
|