faraday-http-cache 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/faraday/http_cache/cache_control.rb +11 -0
- data/lib/faraday/http_cache/response.rb +15 -1
- data/spec/response_spec.rb +21 -1
- data/spec/storage_spec.rb +34 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75eed5459ea58ad83130acdab34d7acf40967eaa
|
4
|
+
data.tar.gz: f5847df239db40584db36489818143e31d821222
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f95af2c373abe3e7b3055c10fcd92b2208b01c3725da597e9b07d9e56c0ee477e7028852f883b6a7870f02ba8a65bcbd8c338e05a0b2535cd9093a63cfdc7682
|
7
|
+
data.tar.gz: 8abb220a303052b25b83c4e16f40c77c4dbc6e4bd87ee33e18be28d108fde5ef2600e2baf5f5802a37e30650a89f1e8f35c7d63ffc67799ce49b5b0dc961b89f
|
@@ -38,6 +38,17 @@ module Faraday
|
|
38
38
|
@directives['max-age'].to_i if @directives.key?('max-age')
|
39
39
|
end
|
40
40
|
|
41
|
+
# Internal: Gets the 'max-age' directive as an Integer.
|
42
|
+
#
|
43
|
+
# takes the age header integer value and reduces the max-age and s-maxage
|
44
|
+
# if present to account for having to remove static age header when caching responses
|
45
|
+
def normalize_max_ages(age)
|
46
|
+
if age > 0
|
47
|
+
@directives['max-age'] = @directives['max-age'].to_i - age if @directives.key?('max-age')
|
48
|
+
@directives['s-maxage'] = @directives['s-maxage'].to_i - age if @directives.key?('s-maxage')
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
41
52
|
# Internal: Gets the 's-maxage' directive as an Integer.
|
42
53
|
#
|
43
54
|
# Returns nil if the 's-maxage' directive isn't present.
|
@@ -106,7 +106,7 @@ module Faraday
|
|
106
106
|
def max_age
|
107
107
|
cache_control.shared_max_age ||
|
108
108
|
cache_control.max_age ||
|
109
|
-
(expires && (expires -
|
109
|
+
(expires && (expires - @now))
|
110
110
|
end
|
111
111
|
|
112
112
|
# Internal: Creates a new 'Faraday::Response', merging the stored
|
@@ -123,6 +123,7 @@ module Faraday
|
|
123
123
|
#
|
124
124
|
# Returns a 'Hash'.
|
125
125
|
def serializable_hash
|
126
|
+
prepare_to_cache
|
126
127
|
@payload.slice(:status, :body, :response_headers)
|
127
128
|
end
|
128
129
|
|
@@ -175,6 +176,19 @@ module Faraday
|
|
175
176
|
def headers
|
176
177
|
@payload[:response_headers]
|
177
178
|
end
|
179
|
+
|
180
|
+
# Internal: prepares the response headers ready to be cached
|
181
|
+
#
|
182
|
+
# removes the age header if present to allow cached responses to continue aging while cached
|
183
|
+
# also normalizes the max age headers if age header provided to ensure accuracy once age header removed
|
184
|
+
def prepare_to_cache
|
185
|
+
if headers.key? 'Age'
|
186
|
+
cache_control.normalize_max_ages(headers['Age'].to_i)
|
187
|
+
headers.delete 'Age'
|
188
|
+
headers['Cache-Control'] = cache_control.to_s
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
178
192
|
end
|
179
193
|
end
|
180
194
|
end
|
data/spec/response_spec.rb
CHANGED
@@ -90,7 +90,8 @@ describe Faraday::HttpCache::Response do
|
|
90
90
|
it "fallsback to the expiration date leftovers" do
|
91
91
|
headers = { "Expires" => (Time.now + 100).httpdate, 'Date' => Time.now.httpdate }
|
92
92
|
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
93
|
-
response.max_age.should
|
93
|
+
response.max_age.should < 100
|
94
|
+
response.max_age.should > 98
|
94
95
|
end
|
95
96
|
|
96
97
|
it "returns nil when there's no information to calculate the max age" do
|
@@ -152,4 +153,23 @@ describe Faraday::HttpCache::Response do
|
|
152
153
|
response.body.should == "Hi!"
|
153
154
|
end
|
154
155
|
end
|
156
|
+
|
157
|
+
describe 'remove age before caching and normalize max-age if non-zero age present' do
|
158
|
+
it 'is fresh if the response still has some time to live' do
|
159
|
+
headers = {
|
160
|
+
'Age' => 6,
|
161
|
+
'Cache-Control' => 'public, max-age=40',
|
162
|
+
'Date' => 38.seconds.ago.httpdate,
|
163
|
+
'Expires' => 37.seconds.from_now.httpdate,
|
164
|
+
'Last-Modified' => 300.seconds.ago.httpdate
|
165
|
+
}
|
166
|
+
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
167
|
+
response.should be_fresh
|
168
|
+
|
169
|
+
response.serializable_hash
|
170
|
+
response.max_age.should == 34
|
171
|
+
response.should_not be_fresh
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
155
175
|
end
|
data/spec/storage_spec.rb
CHANGED
@@ -38,4 +38,38 @@ describe Faraday::HttpCache::Storage do
|
|
38
38
|
subject.read(request).should be_a(Faraday::HttpCache::Response)
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
describe 'remove age before caching and normalize max-age if non-zero age present' do
|
43
|
+
it 'is fresh if the response still has some time to live' do
|
44
|
+
headers = {
|
45
|
+
'Age' => 6,
|
46
|
+
'Cache-Control' => 'public, max-age=40',
|
47
|
+
'Date' => 38.seconds.ago.httpdate,
|
48
|
+
'Expires' => 37.seconds.from_now.httpdate,
|
49
|
+
'Last-Modified' => 300.seconds.ago.httpdate
|
50
|
+
}
|
51
|
+
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
52
|
+
response.should be_fresh
|
53
|
+
subject.write(request, response)
|
54
|
+
|
55
|
+
cached_response = subject.read(request)
|
56
|
+
cached_response.max_age.should == 34
|
57
|
+
cached_response.should_not be_fresh
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'is fresh until cached and that 1 second elapses then the response is no longer fresh' do
|
61
|
+
headers = {
|
62
|
+
'Date' => 39.seconds.ago.httpdate,
|
63
|
+
'Expires' => 40.seconds.from_now.httpdate,
|
64
|
+
}
|
65
|
+
response = Faraday::HttpCache::Response.new(:response_headers => headers)
|
66
|
+
response.should be_fresh
|
67
|
+
subject.write(request, response)
|
68
|
+
|
69
|
+
sleep(1)
|
70
|
+
cached_response = subject.read(request)
|
71
|
+
cached_response.should_not be_fresh
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
41
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-http-cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Mazza
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
95
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.0.
|
96
|
+
rubygems_version: 2.0.2
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: A Faraday middleware that stores and validates cache expiration.
|