cache_rules 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +0 -1
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/cache_rules.gemspec +1 -1
- data/lib/cache_rules.rb +5 -12
- data/lib/helpers.rb +23 -0
- data/test/helper.rb +1 -1
- data/test/test_helpers.rb +22 -0
- metadata +2 -2
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
## What is CacheRules
|
2
2
|
|
3
|
-
_CacheRules_ is a well-behaved HTTP caching library aimed at being [RFC 7234](https://tools.ietf.org/html/rfc7234)
|
3
|
+
_CacheRules_ is a well-behaved HTTP caching library aimed at being [RFC 7234](https://tools.ietf.org/html/rfc7234) compliant.
|
4
4
|
|
5
5
|
This library does **not actually _cache_ anything**, and it is **not a _proxy_**.
|
6
6
|
It validates HTTP headers and returns the appropriate response to determine
|
data/Rakefile
CHANGED
data/cache_rules.gemspec
CHANGED
data/lib/cache_rules.rb
CHANGED
@@ -120,22 +120,15 @@ module CacheRules
|
|
120
120
|
end
|
121
121
|
|
122
122
|
# Revalidates a response by fetching headers from the origin server
|
123
|
-
def revalidate_response(
|
124
|
-
|
125
|
-
|
126
|
-
response_date = Time.now.gmtime.httpdate
|
127
|
-
response_headers = normalize.(response.to_hash.map &:flatten)
|
128
|
-
|
129
|
-
response_headers['Date'] = response_date if response_headers['Date']
|
130
|
-
response_headers['X-Cache-Req-Date'] = request_date
|
131
|
-
response_headers['X-Cache-Res-Date'] = response_date
|
132
|
-
response_headers['Status'] = response.code
|
123
|
+
def revalidate_response(*args)
|
124
|
+
url, request, cached = *args
|
125
|
+
res_headers = helper_response_headers.(helper_make_request_timer.(args))
|
133
126
|
|
134
127
|
# 1. get the column
|
135
|
-
column = RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions],
|
128
|
+
column = RESPONSE_MAP[helper_run_validate.call(RESPONSE_TABLE[:conditions], request, cached, res_headers).join]
|
136
129
|
|
137
130
|
# 2. return the response
|
138
|
-
helper_response url, RESPONSE_TABLE[:actions], column,
|
131
|
+
helper_response url, RESPONSE_TABLE[:actions], column, cached, res_headers
|
139
132
|
rescue => error
|
140
133
|
{:code => 504, :body => 'Gateway Timeout', :headers => [], :error => error.message, :debug => error}
|
141
134
|
end
|
data/lib/helpers.rb
CHANGED
@@ -335,4 +335,27 @@ module CacheRules
|
|
335
335
|
Proc.new { http.request request }
|
336
336
|
end
|
337
337
|
|
338
|
+
def helper_make_request_timer
|
339
|
+
Proc.new {|url, request, cached|
|
340
|
+
{
|
341
|
+
:req_date => Time.now.gmtime.httpdate,
|
342
|
+
:res => make_request.call(url, request, cached).call,
|
343
|
+
:res_date => Time.now.gmtime.httpdate
|
344
|
+
}
|
345
|
+
}
|
346
|
+
end
|
347
|
+
|
348
|
+
def helper_response_headers
|
349
|
+
Proc.new {|result|
|
350
|
+
res_headers = normalize.(result[:res].to_hash.map &:flatten)
|
351
|
+
|
352
|
+
res_headers['Date'] = result[:res_date] if res_headers['Date']
|
353
|
+
res_headers['X-Cache-Req-Date'] = result[:req_date]
|
354
|
+
res_headers['X-Cache-Res-Date'] = result[:res_date]
|
355
|
+
res_headers['Status'] = result[:res].code
|
356
|
+
|
357
|
+
res_headers
|
358
|
+
}
|
359
|
+
end
|
360
|
+
|
338
361
|
end
|
data/test/helper.rb
CHANGED
data/test/test_helpers.rb
CHANGED
@@ -407,4 +407,26 @@ class TestHelpers < MiniTest::Test
|
|
407
407
|
assert_equal result.arity, 0
|
408
408
|
end
|
409
409
|
|
410
|
+
def test_make_request_timer
|
411
|
+
result = CacheRules.helper_make_request_timer
|
412
|
+
|
413
|
+
assert_kind_of Proc, result
|
414
|
+
assert_equal result.arity, 3
|
415
|
+
end
|
416
|
+
|
417
|
+
def test_response_headers
|
418
|
+
http = Net::HTTPResponse.new(1.1, 200, "OK")
|
419
|
+
http.add_field "Warning", "299 - \"Hello World\""
|
420
|
+
|
421
|
+
response_no_date = {:req_date=>"Mon, 02 Feb 2015 04:21:37 GMT", :res=>http, :res_date=>"Mon, 02 Feb 2015 04:21:40 GMT"}
|
422
|
+
result_no_date = CacheRules.helper_response_headers.call response_no_date
|
423
|
+
|
424
|
+
http.add_field "Date", "Fri, 02 Jan 2015 11:03:45 GMT"
|
425
|
+
response_with_date = {:req_date=>"Mon, 02 Feb 2015 04:21:37 GMT", :res=>http, :res_date=>"Mon, 02 Feb 2015 04:21:40 GMT"}
|
426
|
+
result_with_date = CacheRules.helper_response_headers.call response_with_date
|
427
|
+
|
428
|
+
assert_equal result_no_date, {"Warning"=>["299 - \"Hello World\""], "X-Cache-Req-Date"=>"Mon, 02 Feb 2015 04:21:37 GMT", "X-Cache-Res-Date"=>"Mon, 02 Feb 2015 04:21:40 GMT", "Status"=>200}
|
429
|
+
assert_equal result_with_date, {"Warning"=>["299 - \"Hello World\""], "Date"=>"Mon, 02 Feb 2015 04:21:40 GMT", "X-Cache-Req-Date"=>"Mon, 02 Feb 2015 04:21:37 GMT", "X-Cache-Res-Date"=>"Mon, 02 Feb 2015 04:21:40 GMT", "Status"=>200}
|
430
|
+
end
|
431
|
+
|
410
432
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_rules
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-02-
|
12
|
+
date: 2015-02-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: fakeweb
|