CacheBar 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{CacheBar}
8
- s.version = "1.0.1"
8
+ s.version = "1.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Landau", "David Eisinger"]
12
- s.date = %q{2011-06-01}
12
+ s.date = %q{2011-11-17}
13
13
  s.description = %q{A simple API caching layer built on top of HTTParty and Redis}
14
14
  s.email = %q{brian.landau@viget.com}
15
15
  s.extra_rdoc_files = [
@@ -26,11 +26,11 @@ Gem::Specification.new do |s|
26
26
  "Rakefile",
27
27
  "VERSION",
28
28
  "lib/cachebar.rb",
29
- "lib/core_ext/hash/require_keys.rb",
30
29
  "lib/httparty/httpcache.rb",
31
30
  "test/fixtures/user_timeline.json",
32
31
  "test/fixtures/vcr_cassettes/bad_response.yml",
33
32
  "test/fixtures/vcr_cassettes/good_response.yml",
33
+ "test/fixtures/vcr_cassettes/status_update_post.yml",
34
34
  "test/fixtures/vcr_cassettes/unparsable.yml",
35
35
  "test/helper.rb",
36
36
  "test/test_cachebar.rb",
@@ -50,7 +50,6 @@ Gem::Specification.new do |s|
50
50
  s.add_runtime_dependency(%q<redis-namespace>, [">= 0"])
51
51
  s.add_runtime_dependency(%q<httparty>, ["~> 0.7.7"])
52
52
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
53
- s.add_runtime_dependency(%q<SystemTimer>, [">= 0"])
54
53
  s.add_development_dependency(%q<shoulda>, [">= 0"])
55
54
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
56
55
  s.add_development_dependency(%q<jeweler>, ["~> 1.6"])
@@ -59,12 +58,12 @@ Gem::Specification.new do |s|
59
58
  s.add_development_dependency(%q<vcr>, [">= 0"])
60
59
  s.add_development_dependency(%q<mocha>, [">= 0"])
61
60
  s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
61
+ s.add_development_dependency(%q<SystemTimer>, [">= 0"])
62
62
  else
63
63
  s.add_dependency(%q<redis>, [">= 0"])
64
64
  s.add_dependency(%q<redis-namespace>, [">= 0"])
65
65
  s.add_dependency(%q<httparty>, ["~> 0.7.7"])
66
66
  s.add_dependency(%q<activesupport>, [">= 0"])
67
- s.add_dependency(%q<SystemTimer>, [">= 0"])
68
67
  s.add_dependency(%q<shoulda>, [">= 0"])
69
68
  s.add_dependency(%q<bundler>, ["~> 1.0"])
70
69
  s.add_dependency(%q<jeweler>, ["~> 1.6"])
@@ -73,13 +72,13 @@ Gem::Specification.new do |s|
73
72
  s.add_dependency(%q<vcr>, [">= 0"])
74
73
  s.add_dependency(%q<mocha>, [">= 0"])
75
74
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
75
+ s.add_dependency(%q<SystemTimer>, [">= 0"])
76
76
  end
77
77
  else
78
78
  s.add_dependency(%q<redis>, [">= 0"])
79
79
  s.add_dependency(%q<redis-namespace>, [">= 0"])
80
80
  s.add_dependency(%q<httparty>, ["~> 0.7.7"])
81
81
  s.add_dependency(%q<activesupport>, [">= 0"])
82
- s.add_dependency(%q<SystemTimer>, [">= 0"])
83
82
  s.add_dependency(%q<shoulda>, [">= 0"])
84
83
  s.add_dependency(%q<bundler>, ["~> 1.0"])
85
84
  s.add_dependency(%q<jeweler>, ["~> 1.6"])
@@ -88,6 +87,7 @@ Gem::Specification.new do |s|
88
87
  s.add_dependency(%q<vcr>, [">= 0"])
89
88
  s.add_dependency(%q<mocha>, [">= 0"])
90
89
  s.add_dependency(%q<rake>, ["~> 0.8.7"])
90
+ s.add_dependency(%q<SystemTimer>, [">= 0"])
91
91
  end
92
92
  end
93
93
 
data/Gemfile CHANGED
@@ -4,7 +4,6 @@ gem "redis"
4
4
  gem "redis-namespace"
5
5
  gem 'httparty', '~> 0.7.7'
6
6
  gem 'activesupport'
7
- gem 'SystemTimer', :platforms => :ruby_18
8
7
 
9
8
  group :development do
10
9
  gem "shoulda"
@@ -15,4 +14,5 @@ group :development do
15
14
  gem 'vcr'
16
15
  gem 'mocha'
17
16
  gem 'rake', '~> 0.8.7'
17
+ gem 'SystemTimer', :platforms => :ruby_18
18
18
  end
data/README.md CHANGED
@@ -64,7 +64,7 @@ If you already have HTTParty included then you just need to use the `caches_api_
64
64
 
65
65
  * `host`* *optional*:
66
66
  * This is used internally to decide which requests to try to cache responses for.
67
- If it's not provided it will use the `base_uri` specified on the class/module that HTTParty is included into.
67
+ If you've defined `base_uri` on the class/module that HTTParty is included into then this option is not needed.
68
68
  * `key_name`:
69
69
  * This is the name used in Redis to create a part of the cache key to easily differentiate it from other API caches.
70
70
  * `expire_in`:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.0.2
@@ -5,7 +5,6 @@ require 'active_support'
5
5
  require 'active_support/core_ext/module/aliasing'
6
6
  require 'active_support/core_ext/module/attribute_accessors'
7
7
  require 'active_support/core_ext/object/blank'
8
- require 'core_ext/hash/require_keys'
9
8
  if RUBY_VERSION.split('.')[1].to_i < 9
10
9
  begin
11
10
  require 'system_timer'
@@ -18,7 +17,12 @@ require 'httparty/httpcache'
18
17
  module CacheBar
19
18
  def self.register_api_to_cache(host, options)
20
19
  raise ArgumentError, "You must provide a host that you are caching API responses for." if host.blank?
21
- options.require_keys(:expire_in, :key_name)
20
+
21
+ missing_options = ([:expire_in, :key_name] - options.keys)
22
+ if missing_options.present?
23
+ raise(ArgumentError, "Missing some required options: #{missing_options.join(", ")}")
24
+ end
25
+
22
26
  HTTParty::HTTPCache.apis[host] = options
23
27
  end
24
28
 
@@ -32,6 +36,7 @@ module CacheBar
32
36
  CacheBar.register_api_to_cache(host, options)
33
37
  end
34
38
  end
39
+
35
40
  end
36
41
 
37
42
  HTTParty::ClassMethods.send(:include, CacheBar::ClassMethods)
@@ -21,11 +21,12 @@ module HTTParty
21
21
  end
22
22
 
23
23
  def perform_with_caching
24
- if HTTPCache.perform_caching && HTTPCache.apis.keys.include?(uri.host)
24
+ if cacheable?
25
25
  if response_in_cache?
26
26
  log_message("Retrieving response from cache")
27
27
  response_from(response_body_from_cache)
28
28
  else
29
+ validate
29
30
  begin
30
31
  httparty_response = timeout(timeout_length) do
31
32
  perform_without_caching
@@ -49,6 +50,11 @@ module HTTParty
49
50
  end
50
51
 
51
52
  protected
53
+
54
+ def cacheable?
55
+ HTTPCache.perform_caching && HTTPCache.apis.keys.include?(uri.host) &&
56
+ http_method == Net::HTTP::Get
57
+ end
52
58
 
53
59
  def response_from(response_body)
54
60
  HTTParty::Response.new(self, OpenStruct.new(:body => response_body), parse_response(response_body))
@@ -0,0 +1,49 @@
1
+ ---
2
+ - !ruby/struct:VCR::HTTPInteraction
3
+ request: !ruby/struct:VCR::Request
4
+ method: :post
5
+ uri: https://api.twitter.com:443/1/statuses/update.json?screen_name=viget&status=My%20new%20status.
6
+ body: !!null
7
+ headers: !!null
8
+ response: !ruby/struct:VCR::Response
9
+ status: !ruby/struct:VCR::ResponseStatus
10
+ code: 200
11
+ message: OK
12
+ headers:
13
+ date:
14
+ - Wed, 01 Jun 2011 20:37:34 GMT
15
+ server:
16
+ - hi
17
+ status:
18
+ - 200 OK
19
+ x-transaction:
20
+ - 1306960654-78174-14919
21
+ x-frame-options:
22
+ - SAMEORIGIN
23
+ last-modified:
24
+ - Wed, 01 Jun 2011 20:37:34 GMT
25
+ content-type:
26
+ - application/json; charset=utf-8
27
+ content-length:
28
+ - '1'
29
+ pragma:
30
+ - no-cache
31
+ x-revision:
32
+ - DEV
33
+ expires:
34
+ - Tue, 31 Mar 1981 05:00:00 GMT
35
+ cache-control:
36
+ - no-cache, no-store, must-revalidate, pre-check=0, post-check=0
37
+ x-mid:
38
+ - 174ecf14d1392c67f19109aa27b707fad326ca40
39
+ set-cookie:
40
+ - _twitter_sess=BAh7CToMY3NyZl9pZCIlYTdjZTM5MGNiYjM3MjY4ZWRhM2FhNGYwODAxMDk0%250AMjU6D2NyZWF0ZWRfYXRsKwiR8OxMMAE6B2lkIiVhMWRhMmE2M2QzMjNhMDU5%250ANjg1OTNjODkyNTFmMmYwMSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6%250ARmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--200a7dfe09fd1b68cc9e4924ce8af6fc7278d954;
41
+ domain=.twitter.com; path=/; HttpOnly
42
+ - auth_token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
43
+ - guest_id=130696065447966883; path=/; expires=Fri, 01 Jul 2011 20:37:34 GMT
44
+ - k=70.182.186.100.1306960654472912; path=/; expires=Wed, 08-Jun-11 20:37:34
45
+ GMT; domain=.twitter.com
46
+ vary:
47
+ - Accept-Encoding
48
+ body: ! ""
49
+ http_version: '1.1'
@@ -154,6 +154,25 @@ class TestCacheBar < Test::Unit::TestCase
154
154
  end
155
155
  end
156
156
  end
157
+
158
+ context 'with a post' do
159
+ setup do
160
+ VCR.insert_cassette('status_update_post')
161
+ end
162
+
163
+ should 'never try to cache' do
164
+ @redis.expects(:exists).never
165
+ @redis.expects(:set).never
166
+ @redis.expects(:expires).never
167
+ @redis.expects(:hset).never
168
+ @redis.expects(:get).never
169
+ TwitterAPI.update_status('viget', 'My new status.')
170
+ end
171
+
172
+ teardown do
173
+ VCR.eject_cassette
174
+ end
175
+ end
157
176
  end
158
177
 
159
178
  context 'with caching off' do
@@ -17,6 +17,14 @@ module TwitterAPI
17
17
  def bogus_resource
18
18
  get '/user_timeline.json'
19
19
  end
20
+
21
+ def update_status(username, new_status)
22
+ # this is just for testing as this isn't really complient with API
23
+ post '/statuses/update.json', :query => {
24
+ :screen_name => username,
25
+ :status => new_status
26
+ }
27
+ end
20
28
  end
21
29
 
22
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: CacheBar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,12 +10,12 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2011-06-01 00:00:00.000000000 -04:00
13
+ date: 2011-11-17 00:00:00.000000000 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: redis
18
- requirement: &2152494480 !ruby/object:Gem::Requirement
18
+ requirement: &2152173840 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ! '>='
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *2152494480
26
+ version_requirements: *2152173840
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: redis-namespace
29
- requirement: &2152494000 !ruby/object:Gem::Requirement
29
+ requirement: &2152173280 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ! '>='
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: '0'
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *2152494000
37
+ version_requirements: *2152173280
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: httparty
40
- requirement: &2152493520 !ruby/object:Gem::Requirement
40
+ requirement: &2152172740 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,10 +45,10 @@ dependencies:
45
45
  version: 0.7.7
46
46
  type: :runtime
47
47
  prerelease: false
48
- version_requirements: *2152493520
48
+ version_requirements: *2152172740
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: activesupport
51
- requirement: &2152493040 !ruby/object:Gem::Requirement
51
+ requirement: &2152172200 !ruby/object:Gem::Requirement
52
52
  none: false
53
53
  requirements:
54
54
  - - ! '>='
@@ -56,21 +56,10 @@ dependencies:
56
56
  version: '0'
57
57
  type: :runtime
58
58
  prerelease: false
59
- version_requirements: *2152493040
60
- - !ruby/object:Gem::Dependency
61
- name: SystemTimer
62
- requirement: &2152492560 !ruby/object:Gem::Requirement
63
- none: false
64
- requirements:
65
- - - ! '>='
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- type: :runtime
69
- prerelease: false
70
- version_requirements: *2152492560
59
+ version_requirements: *2152172200
71
60
  - !ruby/object:Gem::Dependency
72
61
  name: shoulda
73
- requirement: &2152492080 !ruby/object:Gem::Requirement
62
+ requirement: &2152171680 !ruby/object:Gem::Requirement
74
63
  none: false
75
64
  requirements:
76
65
  - - ! '>='
@@ -78,10 +67,10 @@ dependencies:
78
67
  version: '0'
79
68
  type: :development
80
69
  prerelease: false
81
- version_requirements: *2152492080
70
+ version_requirements: *2152171680
82
71
  - !ruby/object:Gem::Dependency
83
72
  name: bundler
84
- requirement: &2152491600 !ruby/object:Gem::Requirement
73
+ requirement: &2152171160 !ruby/object:Gem::Requirement
85
74
  none: false
86
75
  requirements:
87
76
  - - ~>
@@ -89,10 +78,10 @@ dependencies:
89
78
  version: '1.0'
90
79
  type: :development
91
80
  prerelease: false
92
- version_requirements: *2152491600
81
+ version_requirements: *2152171160
93
82
  - !ruby/object:Gem::Dependency
94
83
  name: jeweler
95
- requirement: &2152491120 !ruby/object:Gem::Requirement
84
+ requirement: &2152170620 !ruby/object:Gem::Requirement
96
85
  none: false
97
86
  requirements:
98
87
  - - ~>
@@ -100,10 +89,10 @@ dependencies:
100
89
  version: '1.6'
101
90
  type: :development
102
91
  prerelease: false
103
- version_requirements: *2152491120
92
+ version_requirements: *2152170620
104
93
  - !ruby/object:Gem::Dependency
105
94
  name: rcov
106
- requirement: &2152490640 !ruby/object:Gem::Requirement
95
+ requirement: &2152170120 !ruby/object:Gem::Requirement
107
96
  none: false
108
97
  requirements:
109
98
  - - ! '>='
@@ -111,10 +100,10 @@ dependencies:
111
100
  version: '0'
112
101
  type: :development
113
102
  prerelease: false
114
- version_requirements: *2152490640
103
+ version_requirements: *2152170120
115
104
  - !ruby/object:Gem::Dependency
116
105
  name: webmock
117
- requirement: &2152490160 !ruby/object:Gem::Requirement
106
+ requirement: &2152169620 !ruby/object:Gem::Requirement
118
107
  none: false
119
108
  requirements:
120
109
  - - ! '>='
@@ -122,10 +111,10 @@ dependencies:
122
111
  version: '0'
123
112
  type: :development
124
113
  prerelease: false
125
- version_requirements: *2152490160
114
+ version_requirements: *2152169620
126
115
  - !ruby/object:Gem::Dependency
127
116
  name: vcr
128
- requirement: &2152489680 !ruby/object:Gem::Requirement
117
+ requirement: &2152169080 !ruby/object:Gem::Requirement
129
118
  none: false
130
119
  requirements:
131
120
  - - ! '>='
@@ -133,10 +122,10 @@ dependencies:
133
122
  version: '0'
134
123
  type: :development
135
124
  prerelease: false
136
- version_requirements: *2152489680
125
+ version_requirements: *2152169080
137
126
  - !ruby/object:Gem::Dependency
138
127
  name: mocha
139
- requirement: &2152489200 !ruby/object:Gem::Requirement
128
+ requirement: &2152168540 !ruby/object:Gem::Requirement
140
129
  none: false
141
130
  requirements:
142
131
  - - ! '>='
@@ -144,10 +133,10 @@ dependencies:
144
133
  version: '0'
145
134
  type: :development
146
135
  prerelease: false
147
- version_requirements: *2152489200
136
+ version_requirements: *2152168540
148
137
  - !ruby/object:Gem::Dependency
149
138
  name: rake
150
- requirement: &2152488720 !ruby/object:Gem::Requirement
139
+ requirement: &2152168020 !ruby/object:Gem::Requirement
151
140
  none: false
152
141
  requirements:
153
142
  - - ~>
@@ -155,7 +144,18 @@ dependencies:
155
144
  version: 0.8.7
156
145
  type: :development
157
146
  prerelease: false
158
- version_requirements: *2152488720
147
+ version_requirements: *2152168020
148
+ - !ruby/object:Gem::Dependency
149
+ name: SystemTimer
150
+ requirement: &2152167520 !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ type: :development
157
+ prerelease: false
158
+ version_requirements: *2152167520
159
159
  description: A simple API caching layer built on top of HTTParty and Redis
160
160
  email: brian.landau@viget.com
161
161
  executables: []
@@ -173,11 +173,11 @@ files:
173
173
  - Rakefile
174
174
  - VERSION
175
175
  - lib/cachebar.rb
176
- - lib/core_ext/hash/require_keys.rb
177
176
  - lib/httparty/httpcache.rb
178
177
  - test/fixtures/user_timeline.json
179
178
  - test/fixtures/vcr_cassettes/bad_response.yml
180
179
  - test/fixtures/vcr_cassettes/good_response.yml
180
+ - test/fixtures/vcr_cassettes/status_update_post.yml
181
181
  - test/fixtures/vcr_cassettes/unparsable.yml
182
182
  - test/helper.rb
183
183
  - test/test_cachebar.rb
@@ -198,7 +198,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
198
198
  version: '0'
199
199
  segments:
200
200
  - 0
201
- hash: 1378991500197492114
201
+ hash: 2868285217308708845
202
202
  required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  none: false
204
204
  requirements:
@@ -1,6 +0,0 @@
1
- class Hash
2
- def require_keys(*valid_keys)
3
- missing_keys = [valid_keys].flatten - keys
4
- raise(ArgumentError, "Missing some required keys: #{missing_keys.join(", ")}") unless missing_keys.empty?
5
- end
6
- end