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 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. For now just grab the source from [github](http://github.com/mloughran/api_cache/tree/master) and `rake install`. I'll get a gem sorted soon.
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
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 1
2
+ :patch: 2
3
3
  :major: 0
4
4
  :minor: 1
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 = Net::HTTP.get_response(URI.parse(key))
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
@@ -2,7 +2,6 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe APICache do
4
4
  before :each do
5
- APICache.start(APICache::MemoryStore)
6
5
  @key = 'random_key'
7
6
  @encoded_key = "1520171c64bfb71a95c97d310eea3492"
8
7
  @data = 'some bit of data'
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
@@ -3,3 +3,5 @@ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
3
  require "rubygems"
4
4
  require "api_cache"
5
5
  require "spec"
6
+
7
+ APICache.start(APICache::MemoryStore)
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.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-02-17 00:00:00 -08:00
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