cache_rules 0.1.2 → 0.1.3

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/.travis.yml CHANGED
@@ -5,4 +5,6 @@ rvm:
5
5
  - 2.0.0
6
6
  - 2.1.5
7
7
  - 2.2.0
8
- script: "bundle exec rake test"
8
+ script: "bundle exec rake test"
9
+ notifications:
10
+ email: false
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 complaint.
3
+ _CacheRules_ is a well-behaved HTTP caching library aimed at being [RFC 7234](https://tools.ietf.org/html/rfc7234) complaint.
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
@@ -9,7 +9,7 @@ if a request can be served from the cache.
9
9
  It is up to the HTTP Cache implementation to store the cached results
10
10
  and serve responses from the cache if necessary.
11
11
 
12
- [![Build Status](https://travis-ci.org/aw/CacheRules.svg?branch=master)](https://travis-ci.org/aw/CacheRules) [![Coverage Status](https://coveralls.io/repos/aw/CacheRules/badge.svg?branch=master)](https://coveralls.io/r/aw/CacheRules?branch=master)
12
+ [![Build Status](https://travis-ci.org/aw/CacheRules.svg?branch=master)](https://travis-ci.org/aw/CacheRules) [![Coverage Status](https://coveralls.io/repos/aw/CacheRules/badge.svg?branch=master)](https://coveralls.io/r/aw/CacheRules?branch=master) [![Gem Version](https://badge.fury.io/rb/cache_rules.svg)](http://badge.fury.io/rb/cache_rules)
13
13
 
14
14
  ## Getting started
15
15
 
@@ -119,7 +119,7 @@ These are somewhat based on [CloudFlare's](https://support.cloudflare.com/hc/en-
119
119
 
120
120
  ## Tests
121
121
 
122
- Tests must cover 100% in order to pass. To run the tests, type:
122
+ To run the tests, type:
123
123
 
124
124
  `bundle exec rake test`
125
125
 
@@ -136,6 +136,13 @@ C.R.E.A.M. is an influencial lyrical masterpiece from the 90s performed by the [
136
136
 
137
137
  It's also the premise of this [troll video](http://cacheruleseverythingaround.me/)
138
138
 
139
+ ## Further reading
140
+
141
+ Some useful articles explaining HTTP Caching:
142
+
143
+ * [Caching is hard, draw me a picture](http://www.bizcoder.com/caching-is-hard-draw-me-a-picture)
144
+ * [A Beginner's Guide to HTTP Cache Headers](http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/)
145
+
139
146
  ## LICENSE
140
147
 
141
148
  This Source Code Form is subject to the terms of the Mozilla Public
data/cache_rules.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'date'
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = 'cache_rules'
9
- s.version = '0.1.2'
9
+ s.version = '0.1.3'
10
10
 
11
11
  s.date = Date.today.to_s
12
12
 
data/lib/validations.rb CHANGED
@@ -16,8 +16,12 @@ module CacheRules
16
16
  # The If-Match and If-Unmodified-Since conditional header fields are not applicable to a cache.
17
17
  # source: https://tools.ietf.org/html/rfc7234#section-4.3.2
18
18
 
19
+ def to_bit(&predicate)
20
+ predicate.call ? 1 : 0
21
+ end
22
+
19
23
  def validate_cached?(headers)
20
- headers[:cached].length > 0 ? 1 : 0
24
+ to_bit { headers[:cached].length > 0 }
21
25
  end
22
26
 
23
27
  # Precedence: If-None-Match (ETag), then If-Modified-Since (Last-Modified)
@@ -29,9 +33,9 @@ module CacheRules
29
33
  # Return when the If-None-Match header exists, ignore If-Modified-Since
30
34
  # source: https://tools.ietf.org/html/rfc7232#section-3.3
31
35
  etag_match = helper_etag(request, cached)
32
- return etag_match ? 1 : 0 unless etag_match.nil?
36
+ return to_bit { etag_match } unless etag_match.nil?
33
37
 
34
- helper_last_modified(request, cached) ? 1 : 0
38
+ to_bit { helper_last_modified(request, cached) }
35
39
  end
36
40
 
37
41
  # Compare headers to see if the cached request is expired (Freshness)
@@ -43,11 +47,11 @@ module CacheRules
43
47
 
44
48
  response_is_fresh = freshness_lifetime.to_i > current_age
45
49
 
46
- response_is_fresh ? 0 : 1
50
+ to_bit { response_is_fresh != true }
47
51
  end
48
52
 
49
53
  def validate_only_if_cached?(headers)
50
- headers[:request]['Cache-Control'] && headers[:request]['Cache-Control']['only-if-cached'] ? 1 : 0
54
+ to_bit { headers[:request]['Cache-Control'] && headers[:request]['Cache-Control']['only-if-cached'] }
51
55
  end
52
56
 
53
57
  # Serving Stale Responses
@@ -61,7 +65,7 @@ module CacheRules
61
65
  max_stale = helper_max_stale.call request['Cache-Control'], freshness_lifetime, current_age
62
66
  min_fresh = helper_min_fresh.call request['Cache-Control'], freshness_lifetime, current_age
63
67
 
64
- (max_stale && min_fresh != false) || (max_stale.nil? && min_fresh) ? 1 : 0
68
+ to_bit { (max_stale && min_fresh != false) || (max_stale.nil? && min_fresh) }
65
69
  end
66
70
 
67
71
  # Response Cache-Control Directives
@@ -71,7 +75,7 @@ module CacheRules
71
75
 
72
76
  # source: https://tools.ietf.org/html/rfc7234#section-5.2.2.1
73
77
  # source: https://tools.ietf.org/html/rfc7234#section-5.2.2.7
74
- (( cached = headers[:cached]['Cache-Control'] )) && ( cached['must-revalidate'] || cached['proxy-revalidate'] ) ? 1 : 0
78
+ to_bit { (( cached = headers[:cached]['Cache-Control'] )) && ( cached['must-revalidate'] || cached['proxy-revalidate'] ) }
75
79
  end
76
80
 
77
81
  # Verify if we're explicitly told not to cache the response
@@ -100,12 +104,12 @@ module CacheRules
100
104
  end
101
105
 
102
106
  def validate_is_error?(headers)
103
- headers[:response]['Status'].to_i.between?(500,599) ? 1 : 0
107
+ to_bit { headers[:response]['Status'].to_i.between?(500,599) }
104
108
  end
105
109
 
106
110
  def validate_validator_match?(headers)
107
111
  request, response = headers.values_at :request, :response
108
- response['ETag'] && request['If-None-Match'] && request['If-None-Match'].include?(response['ETag']) ? 1 : 0
112
+ to_bit { response['ETag'] && request['If-None-Match'] && request['If-None-Match'].include?(response['ETag']) }
109
113
  end
110
114
 
111
115
  end
data/test/helper.rb CHANGED
@@ -2,6 +2,8 @@ require 'coveralls'
2
2
  require 'fakeweb'
3
3
  require 'simplecov'
4
4
 
5
+ Coveralls.wear!
6
+
5
7
  SimpleCov.start do
6
8
  add_filter '/test/'
7
9
  add_filter '.bundle'
@@ -14,5 +16,3 @@ require 'minitest/unit'
14
16
  require 'minitest/reporters'
15
17
 
16
18
  Minitest::Reporters.use! [Minitest::Reporters::SpecReporter.new]
17
-
18
- Coveralls.wear!
@@ -70,6 +70,14 @@ class TestValidations < MiniTest::Test
70
70
  }
71
71
  end
72
72
 
73
+ def test_to_bit
74
+ one = CacheRules.to_bit { true }
75
+ zero = CacheRules.to_bit { false }
76
+
77
+ assert_equal one, 1
78
+ assert_equal zero, 0
79
+ end
80
+
73
81
  def test_cached
74
82
  one = CacheRules.validate_cached? @headers
75
83
  zero = CacheRules.validate_cached? @no_headers
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.2
4
+ version: 0.1.3
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-01-31 00:00:00.000000000 Z
12
+ date: 2015-02-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fakeweb