slimmer 3.2.0 → 3.3.0

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/lib/slimmer/skin.rb CHANGED
@@ -7,15 +7,20 @@ module Slimmer
7
7
  def initialize options = {}
8
8
  @options = options
9
9
  @asset_host = options[:asset_host]
10
- @template_cache = {}
10
+
11
11
  @use_cache = options[:use_cache] || false
12
+ @cache_ttl = options[:cache_ttl] || (15 * 60) # 15 mins
13
+ @template_cache = LRUCache.new(:ttl => @cache_ttl) if @use_cache
14
+
12
15
  @logger = options[:logger] || NullLogger.instance
13
16
  @strict = options[:strict] || (%w{development test}.include?(ENV['RACK_ENV']))
14
17
  end
15
18
 
16
19
  def template(template_name)
17
20
  if use_cache
18
- template_cache[template_name] ||= load_template(template_name)
21
+ template_cache.fetch(template_name) do
22
+ load_template(template_name)
23
+ end
19
24
  else
20
25
  load_template(template_name)
21
26
  end
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = '3.2.0'
2
+ VERSION = '3.3.0'
3
3
  end
data/test/skin_test.rb CHANGED
@@ -1,61 +1,122 @@
1
1
  require_relative "test_helper"
2
2
 
3
- class SkinTest < MiniTest::Unit::TestCase
4
- def test_template_can_be_loaded
5
- skin = Slimmer::Skin.new asset_host: "http://example.local/"
6
- expected_url = "http://example.local/templates/example.html.erb"
7
- stub_request(:get, expected_url).to_return :body => "<foo />"
8
-
9
- template = skin.template 'example'
3
+ describe Slimmer::Skin do
10
4
 
11
- assert_requested :get, "http://example.local/templates/example.html.erb"
12
- assert_equal "<foo />", template
13
- end
5
+ describe "loading templates" do
6
+ it "should be able to load the template" do
7
+ skin = Slimmer::Skin.new asset_host: "http://example.local/"
8
+ expected_url = "http://example.local/templates/example.html.erb"
9
+ stub_request(:get, expected_url).to_return :body => "<foo />"
14
10
 
15
- def test_templates_can_be_cached
16
- skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: true
17
- expected_url = "http://example.local/templates/example.html.erb"
18
- stub_request(:get, expected_url).to_return :body => "<foo />"
11
+ template = skin.template 'example'
19
12
 
20
- first_access = skin.template 'example'
21
- second_access = skin.template 'example'
13
+ assert_requested :get, "http://example.local/templates/example.html.erb"
14
+ assert_equal "<foo />", template
15
+ end
22
16
 
23
- assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
24
- assert_same first_access, second_access
25
- end
17
+ describe "template caching" do
18
+ it "should not cache the template by default" do
19
+ skin = Slimmer::Skin.new asset_host: "http://example.local/"
20
+ expected_url = "http://example.local/templates/example.html.erb"
21
+ stub_request(:get, expected_url).to_return :body => "<foo />"
22
+
23
+ first_access = skin.template 'example'
24
+ second_access = skin.template 'example'
25
+
26
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 2
27
+ end
28
+
29
+ it "should cache the template when requested" do
30
+ skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: true
31
+ expected_url = "http://example.local/templates/example.html.erb"
32
+ stub_request(:get, expected_url).to_return :body => "<foo />"
33
+
34
+ first_access = skin.template 'example'
35
+ second_access = skin.template 'example'
36
+
37
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
38
+ assert_same first_access, second_access
39
+ end
40
+
41
+ it "should only cache the template for 15 mins by default" do
42
+ skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: true
43
+ expected_url = "http://example.local/templates/example.html.erb"
44
+ stub_request(:get, expected_url).to_return :body => "<foo />"
45
+
46
+ first_access = skin.template 'example'
47
+ second_access = skin.template 'example'
48
+
49
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
50
+ assert_same first_access, second_access
51
+
52
+ Timecop.travel( 15 * 60 - 30) do # now + 14 mins 30 secs
53
+ third_access = skin.template 'example'
54
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
55
+ assert_same first_access, third_access
56
+ end
57
+
58
+ Timecop.travel( 15 * 60 + 30) do # now + 15 mins 30 secs
59
+ fourth_access = skin.template 'example'
60
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 2
61
+ assert_equal first_access, fourth_access
62
+ end
63
+ end
64
+
65
+ it "should allow overriding the cache ttl" do
66
+ skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: true, cache_ttl: 5 * 60
67
+ expected_url = "http://example.local/templates/example.html.erb"
68
+ stub_request(:get, expected_url).to_return :body => "<foo />"
69
+
70
+ first_access = skin.template 'example'
71
+ second_access = skin.template 'example'
72
+
73
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
74
+ assert_same first_access, second_access
75
+
76
+ Timecop.travel( 5 * 60 - 30) do # now + 4 mins 30 secs
77
+ third_access = skin.template 'example'
78
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
79
+ assert_same first_access, third_access
80
+ end
81
+
82
+ Timecop.travel( 5 * 60 + 30) do # now + 5 mins 30 secs
83
+ fourth_access = skin.template 'example'
84
+ assert_requested :get, "http://example.local/templates/example.html.erb", times: 2
85
+ assert_equal first_access, fourth_access
86
+ end
87
+ end
88
+ end
26
89
 
27
- def test_should_raise_appropriate_exception_when_template_not_found
28
- skin = Slimmer::Skin.new asset_host: "http://example.local/"
29
- expected_url = "http://example.local/templates/example.html.erb"
30
- stub_request(:get, expected_url).to_return(:status => '404')
90
+ it "should raise appropriate exception when template not found" do
91
+ skin = Slimmer::Skin.new asset_host: "http://example.local/"
92
+ expected_url = "http://example.local/templates/example.html.erb"
93
+ stub_request(:get, expected_url).to_return(:status => '404')
31
94
 
32
- assert_raises(Slimmer::TemplateNotFoundException) do
33
- skin.template 'example'
95
+ assert_raises(Slimmer::TemplateNotFoundException) do
96
+ skin.template 'example'
97
+ end
34
98
  end
35
- end
36
99
 
37
- def test_should_raise_appropriate_exception_when_cant_reach_template_host
38
- skin = Slimmer::Skin.new asset_host: "http://example.local/"
39
- expected_url = "http://example.local/templates/example.html.erb"
40
- stub_request(:get, expected_url).to_raise(Errno::ECONNREFUSED)
100
+ it "should raise appropriate exception when cant reach template host" do
101
+ skin = Slimmer::Skin.new asset_host: "http://example.local/"
102
+ expected_url = "http://example.local/templates/example.html.erb"
103
+ stub_request(:get, expected_url).to_raise(Errno::ECONNREFUSED)
41
104
 
42
- assert_raises(Slimmer::CouldNotRetrieveTemplate) do
43
- skin.template 'example'
105
+ assert_raises(Slimmer::CouldNotRetrieveTemplate) do
106
+ skin.template 'example'
107
+ end
44
108
  end
45
- end
46
109
 
47
- def test_should_raise_appropriate_exception_when_hostname_cannot_be_resolved
48
- skin = Slimmer::Skin.new asset_host: "http://non-existent.domain/"
49
- expected_url = "http://non-existent.domain/templates/example.html.erb"
50
- stub_request(:get, expected_url).to_raise(SocketError)
110
+ it "should raise appropriate exception when hostname cannot be resolved" do
111
+ skin = Slimmer::Skin.new asset_host: "http://non-existent.domain/"
112
+ expected_url = "http://non-existent.domain/templates/example.html.erb"
113
+ stub_request(:get, expected_url).to_raise(SocketError)
51
114
 
52
- assert_raises(Slimmer::CouldNotRetrieveTemplate) do
53
- skin.template 'example'
115
+ assert_raises(Slimmer::CouldNotRetrieveTemplate) do
116
+ skin.template 'example'
117
+ end
54
118
  end
55
119
  end
56
- end
57
-
58
- describe Slimmer::Skin do
59
120
 
60
121
  describe "parsing artefact from header" do
61
122
  before do
data/test/test_helper.rb CHANGED
@@ -2,13 +2,12 @@ require_relative '../lib/slimmer'
2
2
  require 'minitest/autorun'
3
3
  require 'minitest/unit'
4
4
  require 'rack/test'
5
- require 'webmock/minitest'
6
5
  require 'json'
7
6
  require 'logger'
8
7
  require 'mocha'
8
+ require 'timecop'
9
9
  require 'gds_api/test_helpers/content_api'
10
10
 
11
- WebMock.disable_net_connect!
12
11
  ENV['FACTER_govuk_platform'] = 'test'
13
12
 
14
13
  class MiniTest::Unit::TestCase
@@ -36,8 +35,15 @@ class MiniTest::Unit::TestCase
36
35
  WebMock.disable_net_connect!
37
36
  result
38
37
  end
38
+
39
+ def teardown
40
+ Timecop.return
41
+ end
39
42
  end
40
43
 
44
+ require 'webmock/minitest'
45
+ WebMock.disable_net_connect!
46
+
41
47
  class SlimmerIntegrationTest < MiniTest::Unit::TestCase
42
48
  include Rack::Test::Methods
43
49
  include GdsApi::TestHelpers::ContentApi
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: slimmer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.2.0
5
+ version: 3.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Griffiths
@@ -69,8 +69,19 @@ dependencies:
69
69
  prerelease: false
70
70
  version_requirements: *id005
71
71
  - !ruby/object:Gem::Dependency
72
- name: rake
72
+ name: lrucache
73
73
  requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 0.1.3
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rake
84
+ requirement: &id007 !ruby/object:Gem::Requirement
74
85
  none: false
75
86
  requirements:
76
87
  - - ~>
@@ -78,10 +89,10 @@ dependencies:
78
89
  version: 0.9.2.2
79
90
  type: :development
80
91
  prerelease: false
81
- version_requirements: *id006
92
+ version_requirements: *id007
82
93
  - !ruby/object:Gem::Dependency
83
94
  name: rack-test
84
- requirement: &id007 !ruby/object:Gem::Requirement
95
+ requirement: &id008 !ruby/object:Gem::Requirement
85
96
  none: false
86
97
  requirements:
87
98
  - - ~>
@@ -89,10 +100,10 @@ dependencies:
89
100
  version: 0.6.1
90
101
  type: :development
91
102
  prerelease: false
92
- version_requirements: *id007
103
+ version_requirements: *id008
93
104
  - !ruby/object:Gem::Dependency
94
105
  name: mocha
95
- requirement: &id008 !ruby/object:Gem::Requirement
106
+ requirement: &id009 !ruby/object:Gem::Requirement
96
107
  none: false
97
108
  requirements:
98
109
  - - ~>
@@ -100,10 +111,10 @@ dependencies:
100
111
  version: 0.12.4
101
112
  type: :development
102
113
  prerelease: false
103
- version_requirements: *id008
114
+ version_requirements: *id009
104
115
  - !ruby/object:Gem::Dependency
105
116
  name: webmock
106
- requirement: &id009 !ruby/object:Gem::Requirement
117
+ requirement: &id010 !ruby/object:Gem::Requirement
107
118
  none: false
108
119
  requirements:
109
120
  - - ~>
@@ -111,10 +122,10 @@ dependencies:
111
122
  version: "1.7"
112
123
  type: :development
113
124
  prerelease: false
114
- version_requirements: *id009
125
+ version_requirements: *id010
115
126
  - !ruby/object:Gem::Dependency
116
127
  name: therubyracer
117
- requirement: &id010 !ruby/object:Gem::Requirement
128
+ requirement: &id011 !ruby/object:Gem::Requirement
118
129
  none: false
119
130
  requirements:
120
131
  - - ">="
@@ -122,10 +133,10 @@ dependencies:
122
133
  version: "0"
123
134
  type: :development
124
135
  prerelease: false
125
- version_requirements: *id010
136
+ version_requirements: *id011
126
137
  - !ruby/object:Gem::Dependency
127
138
  name: gem_publisher
128
- requirement: &id011 !ruby/object:Gem::Requirement
139
+ requirement: &id012 !ruby/object:Gem::Requirement
129
140
  none: false
130
141
  requirements:
131
142
  - - ~>
@@ -133,10 +144,10 @@ dependencies:
133
144
  version: 1.1.1
134
145
  type: :development
135
146
  prerelease: false
136
- version_requirements: *id011
147
+ version_requirements: *id012
137
148
  - !ruby/object:Gem::Dependency
138
149
  name: gds-api-adapters
139
- requirement: &id012 !ruby/object:Gem::Requirement
150
+ requirement: &id013 !ruby/object:Gem::Requirement
140
151
  none: false
141
152
  requirements:
142
153
  - - "="
@@ -144,7 +155,18 @@ dependencies:
144
155
  version: 1.9.2
145
156
  type: :development
146
157
  prerelease: false
147
- version_requirements: *id012
158
+ version_requirements: *id013
159
+ - !ruby/object:Gem::Dependency
160
+ name: timecop
161
+ requirement: &id014 !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: 0.5.1
167
+ type: :development
168
+ prerelease: false
169
+ version_requirements: *id014
148
170
  description: Rack middleware for skinning pages using a specific template
149
171
  email:
150
172
  - bengriffiths@gmail.com
@@ -159,49 +181,49 @@ files:
159
181
  - README.md
160
182
  - CHANGELOG.md
161
183
  - lib/tasks/slimmer.rake
162
- - lib/slimmer/version.rb
184
+ - lib/slimmer.rb
185
+ - lib/slimmer/skin.rb
186
+ - lib/slimmer/railtie.rb
187
+ - lib/slimmer/template.rb
188
+ - lib/slimmer/test_template.rb
189
+ - lib/slimmer/test.rb
190
+ - lib/slimmer/app.rb
191
+ - lib/slimmer/processors/logo_class_inserter.rb
163
192
  - lib/slimmer/processors/body_class_copier.rb
193
+ - lib/slimmer/processors/tag_mover.rb
194
+ - lib/slimmer/processors/search_path_setter.rb
195
+ - lib/slimmer/processors/title_inserter.rb
164
196
  - lib/slimmer/processors/google_analytics_configurator.rb
197
+ - lib/slimmer/processors/conditional_comment_mover.rb
165
198
  - lib/slimmer/processors/related_items_inserter.rb
199
+ - lib/slimmer/processors/header_context_inserter.rb
166
200
  - lib/slimmer/processors/body_inserter.rb
167
- - lib/slimmer/processors/logo_class_inserter.rb
168
- - lib/slimmer/processors/report_a_problem_inserter.rb
169
- - lib/slimmer/processors/footer_remover.rb
170
- - lib/slimmer/processors/conditional_comment_mover.rb
171
201
  - lib/slimmer/processors/admin_title_inserter.rb
202
+ - lib/slimmer/processors/report_a_problem_inserter.rb
172
203
  - lib/slimmer/processors/section_inserter.rb
173
- - lib/slimmer/processors/search_path_setter.rb
174
- - lib/slimmer/processors/tag_mover.rb
175
- - lib/slimmer/processors/title_inserter.rb
176
- - lib/slimmer/processors/header_context_inserter.rb
177
- - lib/slimmer/template.rb
178
- - lib/slimmer/railtie.rb
179
- - lib/slimmer/test.rb
180
- - lib/slimmer/artefact.rb
181
- - lib/slimmer/skin.rb
182
- - lib/slimmer/app.rb
204
+ - lib/slimmer/processors/footer_remover.rb
183
205
  - lib/slimmer/headers.rb
184
- - lib/slimmer/test_template.rb
185
- - lib/slimmer.rb
206
+ - lib/slimmer/version.rb
207
+ - lib/slimmer/artefact.rb
186
208
  - Rakefile
187
- - test/processors/related_items_inserter_test.rb
188
- - test/processors/report_a_problem_inserter_test.rb
189
- - test/processors/section_inserter_test.rb
190
- - test/processors/google_analytics_test.rb
191
- - test/processors/header_context_inserter_test.rb
192
- - test/processors/body_inserter_test.rb
193
- - test/processors/logo_class_inserter_test.rb
194
- - test/processors/search_path_setter_test.rb
195
- - test/artefact_test.rb
196
- - test/skin_test.rb
197
209
  - test/headers_test.rb
198
- - test/test_template_dependency_on_static_test.rb
199
- - test/typical_usage_test.rb
210
+ - test/artefact_test.rb
211
+ - test/fixtures/related.raw.html.erb
200
212
  - test/fixtures/500.html.erb
201
213
  - test/fixtures/404.html.erb
202
- - test/fixtures/report_a_problem.raw.html.erb
203
- - test/fixtures/related.raw.html.erb
204
214
  - test/fixtures/wrapper.html.erb
215
+ - test/fixtures/report_a_problem.raw.html.erb
216
+ - test/skin_test.rb
217
+ - test/processors/logo_class_inserter_test.rb
218
+ - test/processors/google_analytics_test.rb
219
+ - test/processors/report_a_problem_inserter_test.rb
220
+ - test/processors/body_inserter_test.rb
221
+ - test/processors/search_path_setter_test.rb
222
+ - test/processors/section_inserter_test.rb
223
+ - test/processors/related_items_inserter_test.rb
224
+ - test/processors/header_context_inserter_test.rb
225
+ - test/typical_usage_test.rb
226
+ - test/test_template_dependency_on_static_test.rb
205
227
  - test/test_helper.rb
206
228
  - bin/render_slimmer_error
207
229
  homepage: http://github.com/alphagov/slimmer
@@ -217,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
217
239
  requirements:
218
240
  - - ">="
219
241
  - !ruby/object:Gem::Version
220
- hash: 3747362976319846846
242
+ hash: 3759142448918952269
221
243
  segments:
222
244
  - 0
223
245
  version: "0"
@@ -226,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
226
248
  requirements:
227
249
  - - ">="
228
250
  - !ruby/object:Gem::Version
229
- hash: 3747362976319846846
251
+ hash: 3759142448918952269
230
252
  segments:
231
253
  - 0
232
254
  version: "0"
@@ -238,22 +260,22 @@ signing_key:
238
260
  specification_version: 3
239
261
  summary: Thinner than the skinner
240
262
  test_files:
241
- - test/processors/related_items_inserter_test.rb
242
- - test/processors/report_a_problem_inserter_test.rb
243
- - test/processors/section_inserter_test.rb
244
- - test/processors/google_analytics_test.rb
245
- - test/processors/header_context_inserter_test.rb
246
- - test/processors/body_inserter_test.rb
247
- - test/processors/logo_class_inserter_test.rb
248
- - test/processors/search_path_setter_test.rb
249
- - test/artefact_test.rb
250
- - test/skin_test.rb
251
263
  - test/headers_test.rb
252
- - test/test_template_dependency_on_static_test.rb
253
- - test/typical_usage_test.rb
264
+ - test/artefact_test.rb
265
+ - test/fixtures/related.raw.html.erb
254
266
  - test/fixtures/500.html.erb
255
267
  - test/fixtures/404.html.erb
256
- - test/fixtures/report_a_problem.raw.html.erb
257
- - test/fixtures/related.raw.html.erb
258
268
  - test/fixtures/wrapper.html.erb
269
+ - test/fixtures/report_a_problem.raw.html.erb
270
+ - test/skin_test.rb
271
+ - test/processors/logo_class_inserter_test.rb
272
+ - test/processors/google_analytics_test.rb
273
+ - test/processors/report_a_problem_inserter_test.rb
274
+ - test/processors/body_inserter_test.rb
275
+ - test/processors/search_path_setter_test.rb
276
+ - test/processors/section_inserter_test.rb
277
+ - test/processors/related_items_inserter_test.rb
278
+ - test/processors/header_context_inserter_test.rb
279
+ - test/typical_usage_test.rb
280
+ - test/test_template_dependency_on_static_test.rb
259
281
  - test/test_helper.rb