slimmer 10.0.0 → 10.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -0
  3. data/README.md +6 -31
  4. data/Rakefile +0 -6
  5. data/lib/slimmer.rb +12 -2
  6. data/lib/slimmer/app.rb +1 -4
  7. data/lib/slimmer/component_resolver.rb +2 -6
  8. data/lib/slimmer/govuk_components.rb +12 -0
  9. data/lib/slimmer/govuk_request_id.rb +0 -5
  10. data/lib/slimmer/headers.rb +42 -0
  11. data/lib/slimmer/http_client.rb +16 -0
  12. data/lib/slimmer/i18n_backend.rb +3 -10
  13. data/lib/slimmer/skin.rb +3 -7
  14. data/lib/slimmer/version.rb +1 -1
  15. metadata +20 -72
  16. data/lib/slimmer/cache.rb +0 -46
  17. data/test/cache_test.rb +0 -53
  18. data/test/changelog_test.rb +0 -10
  19. data/test/component_resolver_test.rb +0 -41
  20. data/test/fixtures/404.html.erb +0 -13
  21. data/test/fixtures/410.html.erb +0 -13
  22. data/test/fixtures/500.html.erb +0 -13
  23. data/test/fixtures/core_layout.html.erb +0 -19
  24. data/test/fixtures/proposition_menu.html.erb +0 -7
  25. data/test/fixtures/report_a_problem.raw.html.erb +0 -23
  26. data/test/headers_test.rb +0 -57
  27. data/test/processors/body_inserter_test.rb +0 -43
  28. data/test/processors/header_context_inserter_test.rb +0 -51
  29. data/test/processors/inside_header_inserter_test.rb +0 -35
  30. data/test/processors/metadata_inserter_test.rb +0 -86
  31. data/test/processors/navigation_mover_test.rb +0 -53
  32. data/test/processors/report_a_problem_inserter_test.rb +0 -75
  33. data/test/processors/search_parameter_inserter_test.rb +0 -39
  34. data/test/processors/search_path_setter_test.rb +0 -39
  35. data/test/processors/search_remover_test.rb +0 -62
  36. data/test/processors/tag_mover_test.rb +0 -95
  37. data/test/skin_test.rb +0 -99
  38. data/test/test_helper.rb +0 -136
  39. data/test/test_helpers/govuk_components_test.rb +0 -19
  40. data/test/test_template_dependency_on_static_test.rb +0 -25
  41. data/test/typical_usage_test.rb +0 -358
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b7b991ffcdf6cdee0c893b13695537a444d9272a
4
- data.tar.gz: 06bfc18db4b43fd71a9348ea4d2bf0b24113311a
3
+ metadata.gz: 8d8d3bf6c4191d2dbb60bdb4859ae313ddc7e25a
4
+ data.tar.gz: 3ad0fc97a4e40aa30de1911e50668ec1d364c828
5
5
  SHA512:
6
- metadata.gz: 9edba40f3ccaf964d893580121058a6b5aeba3d7d0112b068006c15251d941ac4960efeebeaec8347550b02364935daa82c602c1102951c47ac8e5cb7b5558dd
7
- data.tar.gz: 46e4d0f6bab847439aca310d7e00d4b080e3b0ee2d6d44afc321b0d973550af13c7f4eedaaa3139b487810b3d10c50b812a4fee003868598d8185c7cadd68231
6
+ metadata.gz: 846af91deeafea3f421ee027ba1a8fafdb3986009ce5ebf26ac3aa3a16051d935fe5231cae0e86ccbf15875c3ee68fdc94fbaf65439904e875949eb9b1a25c00
7
+ data.tar.gz: fa20c579b4a3dcf79242f350fb0a270d0fd2bcdcc25bf577202689266fd9fee941b82496093baf19bae97b6ccd762089844d449ae4c44338fc1f1ab4886d5d70
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 10.1.0
2
+
3
+ * Use `Rails.cache` as the cache for templates, locales and components. You can
4
+ remove `config.slimmer.use_cache` for your application, as you can no longer
5
+ opt-out of caching.
6
+ * Add a `User-Agent` header to all outgoing API requests
7
+
1
8
  # 10.0.0
2
9
 
3
10
  * Removes the need_id meta tag, which is no longer used.
data/README.md CHANGED
@@ -7,35 +7,16 @@ It does this by taking the page rendered by the application, extracting the cont
7
7
  a `div` with id 'wrapper' and inserting that into a `div` with the same id in one of its
8
8
  templates. It also transfers various other details, such as `meta`, `script`, and `style` tags.
9
9
 
10
- ## Use in a Rails app
11
-
12
- Slimmer provides a Railtie so no configuration is necessary. By default it will use the
13
- Plek gem to look for the 'static' (previously 'assets') host for the current environment.
14
-
15
- If you want to use your own set of templates you will need to specify the appropriate host
16
- eg.
17
-
18
- ```rb
19
- YourApp::Application.configure do
20
- config.slimmer.asset_host = 'http://your.server.somewhere'
21
- end
22
- ```
23
-
24
- it expects to find templates in a folder called 'templates' on that host.
10
+ [View documentation](http://www.rubydoc.info/gems/slimmer)
25
11
 
26
- ## Use elsewhere
27
-
28
- Slimmer will work as standard rack middleware:
12
+ ## Use in a Rails app
29
13
 
30
- ```rb
31
- use Slimmer::App
32
- ```
14
+ Slimmer provides a Railtie so no configuration is necessary.
33
15
 
34
- or
16
+ ## Caching
35
17
 
36
- ```rb
37
- use Slimmer::App, :asset_host => "http://my.alternative.host"
38
- ```
18
+ Slimmer makes HTTP requests to `static` for templates, components and locales. These
19
+ are cached for 15 minutes. Slimmer uses `Rails.cache` for this.
39
20
 
40
21
  ## Asset tag helpers
41
22
 
@@ -164,9 +145,3 @@ Add the following code to spec/spec_helper:
164
145
  ```rb
165
146
  require 'slimmer/rspec'
166
147
  ```
167
-
168
- ## The name
169
-
170
- Slimmer was extracted from a much larger project called 'skinner'. 'slimmer' referred to the size
171
- of its code compared to skinner (which also acted as an HTTP proxy and mixed in a few other
172
- concerns). Over time the codebase has grown a little, but the name stuck.
data/Rakefile CHANGED
@@ -18,10 +18,4 @@ Rake::TestTask.new("test") do |t|
18
18
  t.verbose = true
19
19
  end
20
20
 
21
- require "gem_publisher"
22
- task :publish_gem do |t|
23
- gem = GemPublisher.publish_if_updated("slimmer.gemspec", :rubygems)
24
- puts "Published #{gem}" if gem
25
- end
26
-
27
21
  task :default => [:test, :lint]
data/lib/slimmer.rb CHANGED
@@ -7,15 +7,25 @@ require 'slimmer/version'
7
7
  require 'slimmer/railtie' if defined? Rails
8
8
 
9
9
  module Slimmer
10
+ CACHE_TTL = 900
11
+
12
+ def self.cache
13
+ @cache ||= defined?(Rails) ? Rails.cache : NoCache.new
14
+ end
15
+
16
+ class NoCache
17
+ def fetch(*)
18
+ yield
19
+ end
20
+ end
10
21
 
11
22
  autoload :Railtie, 'slimmer/railtie'
12
23
  autoload :Skin, 'slimmer/skin'
13
24
 
14
- autoload :Cache, 'slimmer/cache'
15
-
16
25
  autoload :Template, 'slimmer/template'
17
26
  autoload :App, 'slimmer/app'
18
27
  autoload :Headers, 'slimmer/headers'
28
+ autoload :HTTPClient, 'slimmer/http_client'
19
29
 
20
30
  autoload :GovukComponents, 'slimmer/govuk_components'
21
31
  autoload :ComponentResolver, 'slimmer/component_resolver'
data/lib/slimmer/app.rb CHANGED
@@ -28,10 +28,7 @@ module Slimmer
28
28
  options[:asset_host] = Plek.current.find("static")
29
29
  end
30
30
 
31
- cache = Cache.instance
32
- cache.use_cache = options[:use_cache] if options[:use_cache]
33
-
34
- @skin = Skin.new options.merge(logger: self.logger, cache: cache)
31
+ @skin = Skin.new options.merge(logger: self.logger)
35
32
  end
36
33
 
37
34
  def call(env)
@@ -13,13 +13,12 @@ module Slimmer
13
13
 
14
14
  def find_templates(name, prefix, partial, details, outside_app_allowed = false)
15
15
  return [] unless prefix == 'govuk_component'
16
- cache = Cache.instance
17
16
 
18
17
  template_path = [prefix, name].join('/')
19
18
  if test?
20
19
  template_body = test_body(template_path)
21
20
  else
22
- template_body = cache.fetch(template_path) do
21
+ template_body = Slimmer.cache.fetch(template_path, expires_in: Slimmer::CACHE_TTL) do
23
22
  fetch(template_url(template_path))
24
23
  end
25
24
  end
@@ -43,10 +42,7 @@ module Slimmer
43
42
  end
44
43
 
45
44
  def fetch(template_url)
46
- headers = {}
47
- headers[:govuk_request_id] = GovukRequestId.value if GovukRequestId.set?
48
- response = RestClient.get(template_url, headers)
49
- response.body
45
+ HTTPClient.get(template_url)
50
46
  rescue RestClient::Exception => e
51
47
  raise TemplateNotFoundException, "Unable to fetch: '#{template_url}' because #{e}", caller
52
48
  rescue Errno::ECONNREFUSED => e
@@ -1,9 +1,21 @@
1
1
  module Slimmer
2
+ # @api public
3
+ #
4
+ # Include this module to add the GOV.UK Components to your app.
5
+ # @example
6
+ # class ApplicationController < ActionController::Base
7
+ # include Slimmer::GovukComponents
8
+ # end
9
+ #
10
+ # # In your views:
11
+ #
12
+ # <%= render partial: 'govuk_component/example_component' %>
2
13
  module GovukComponents
3
14
  def self.included into
4
15
  into.before_action :add_govuk_components
5
16
  end
6
17
 
18
+ # @private
7
19
  def add_govuk_components
8
20
  append_view_path Slimmer::ComponentResolver.new
9
21
 
@@ -1,10 +1,6 @@
1
1
  module Slimmer
2
2
  class GovukRequestId
3
3
  class << self
4
- def set?
5
- !(value.nil? || value.empty?)
6
- end
7
-
8
4
  def value
9
5
  Thread.current[:slimmer_govuk_request_id]
10
6
  end
@@ -15,4 +11,3 @@ module Slimmer
15
11
  end
16
12
  end
17
13
  end
18
-
@@ -1,9 +1,13 @@
1
1
  module Slimmer
2
+ # @api public
2
3
  module Headers
4
+ # @private
3
5
  InvalidHeader = Class.new(RuntimeError)
4
6
 
7
+ # @private
5
8
  HEADER_PREFIX = "X-Slimmer"
6
9
 
10
+ # @private
7
11
  SLIMMER_HEADER_MAPPING = {
8
12
  application_name: "Application-Name",
9
13
  format: "Format",
@@ -19,19 +23,57 @@ module Slimmer
19
23
  remove_search: "Remove-Search",
20
24
  }
21
25
 
26
+ # @private
22
27
  APPLICATION_NAME_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:application_name]}"
28
+
29
+ # @private
23
30
  FORMAT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}"
31
+
32
+ # @private
24
33
  ORGANISATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:organisations]}"
34
+
35
+ # @private
25
36
  REPORT_A_PROBLEM_FORM = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:report_a_problem]}"
37
+
38
+ # @private
26
39
  WORLD_LOCATIONS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:world_locations]}"
40
+
41
+ # @private
27
42
  PAGE_OWNER_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:page_owner]}"
43
+
44
+ # @private
28
45
  RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}"
46
+
47
+ # @private
29
48
  SEARCH_PATH_HEADER = "#{HEADER_PREFIX}-Search-Path"
49
+
50
+ # @private
30
51
  SEARCH_PARAMETERS_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:search_parameters]}"
52
+
53
+ # @private
31
54
  SKIP_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}"
55
+
56
+ # @private
32
57
  TEMPLATE_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}"
58
+
59
+ # @private
33
60
  REMOVE_SEARCH_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_search]}"
34
61
 
62
+ # Set the "slimmer headers" to configure the page
63
+ #
64
+ # @param hash [Hash] the options
65
+ # @option hash [String] application_name
66
+ # @option hash [String] format
67
+ # @option hash [String] organisations
68
+ # @option hash [String] page_owner
69
+ # @option hash [String] remove_search
70
+ # @option hash [String] report_a_problem
71
+ # @option hash [String] result_count
72
+ # @option hash [String] search_parameters
73
+ # @option hash [String] section
74
+ # @option hash [String] skip
75
+ # @option hash [String] template
76
+ # @option hash [String] world_locations
35
77
  def set_slimmer_headers(hash)
36
78
  raise InvalidHeader if (hash.keys - SLIMMER_HEADER_MAPPING.keys).any?
37
79
  SLIMMER_HEADER_MAPPING.each do |hash_key, header_suffix|
@@ -0,0 +1,16 @@
1
+ require "slimmer/govuk_request_id"
2
+ require "restclient"
3
+
4
+ module Slimmer
5
+ class HTTPClient
6
+ def self.get(url)
7
+ headers = {
8
+ user_agent: "slimmer/#{Slimmer::VERSION} (#{ENV['GOVUK_APP_NAME']})",
9
+ govuk_request_id: GovukRequestId.value,
10
+ }
11
+
12
+ response = RestClient.get(url, headers)
13
+ response.body
14
+ end
15
+ end
16
+ end
@@ -5,7 +5,7 @@ module Slimmer
5
5
  include I18n::Backend::Base, I18n::Backend::Flatten
6
6
 
7
7
  def available_locales
8
- cache.fetch("available_locales") do
8
+ Slimmer.cache.fetch(template_path, expires_in: Slimmer::CACHE_TTL) do
9
9
  locale_json = fetch(static_locales_url)
10
10
  locales = JSON.parse(locale_json).map(&:to_sym)
11
11
  end
@@ -19,12 +19,8 @@ module Slimmer
19
19
 
20
20
  private
21
21
 
22
- def cache
23
- Cache.instance
24
- end
25
-
26
22
  def translations(locale)
27
- cache.fetch("translations/#{locale}") do
23
+ Slimmer.cache.fetch("translations/#{locale}", expires_in: Slimmer::CACHE_TTL) do
28
24
  fetch_translations(locale)
29
25
  end
30
26
  end
@@ -47,10 +43,7 @@ module Slimmer
47
43
  end
48
44
 
49
45
  def fetch(url)
50
- headers = {}
51
- headers[:govuk_request_id] = GovukRequestId.value if GovukRequestId.set?
52
- response = RestClient.get(url, headers)
53
- response.body
46
+ HTTPClient.get(url)
54
47
  rescue RestClient::Exception => e
55
48
  raise TemplateNotFoundException, "Unable to fetch: '#{url}' because #{e}", caller
56
49
  rescue Errno::ECONNREFUSED => e
data/lib/slimmer/skin.rb CHANGED
@@ -3,29 +3,25 @@ require 'slimmer/govuk_request_id'
3
3
 
4
4
  module Slimmer
5
5
  class Skin
6
- attr_accessor :template_cache, :asset_host, :logger, :strict, :options
6
+ attr_accessor :asset_host, :logger, :strict, :options
7
7
 
8
8
  def initialize options = {}
9
9
  @options = options
10
10
  @asset_host = options[:asset_host]
11
- @template_cache = options[:cache]
12
11
 
13
12
  @logger = options[:logger] || NullLogger.instance
14
13
  @strict = options[:strict] || (%w{development test}.include?(ENV['RACK_ENV']))
15
14
  end
16
15
 
17
16
  def template(template_name)
18
- template_cache.fetch(template_name) do
17
+ Slimmer.cache.fetch(template_name, expires_in: Slimmer::CACHE_TTL) do
19
18
  load_template(template_name)
20
19
  end
21
20
  end
22
21
 
23
22
  def load_template(template_name)
24
23
  url = template_url(template_name)
25
- headers = {}
26
- headers[:govuk_request_id] = GovukRequestId.value if GovukRequestId.set?
27
- response = RestClient.get(url, headers)
28
- response.body
24
+ HTTPClient.get(url)
29
25
  rescue RestClient::Exception => e
30
26
  raise TemplateNotFoundException, "Unable to fetch: '#{template_name}' from '#{url}' because #{e}", caller
31
27
  rescue Errno::ECONNREFUSED, SocketError, OpenSSL::SSL::SSLError => e
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = '10.0.0'
2
+ VERSION = '10.1.0'
3
3
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slimmer
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.0.0
4
+ version: 10.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Ben Griffiths
8
- - James Stewart
7
+ - GOV.UK Dev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2016-11-15 00:00:00.000000000 Z
11
+ date: 2017-02-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: nokogiri
@@ -115,6 +114,20 @@ dependencies:
115
114
  - - ">="
116
115
  - !ruby/object:Gem::Version
117
116
  version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: yard
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '='
122
+ - !ruby/object:Gem::Version
123
+ version: 0.8.7.6
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '='
129
+ - !ruby/object:Gem::Version
130
+ version: 0.8.7.6
118
131
  - !ruby/object:Gem::Dependency
119
132
  name: minitest
120
133
  requirement: !ruby/object:Gem::Requirement
@@ -185,20 +198,6 @@ dependencies:
185
198
  - - '='
186
199
  - !ruby/object:Gem::Version
187
200
  version: 1.18.0
188
- - !ruby/object:Gem::Dependency
189
- name: gem_publisher
190
- requirement: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: 1.5.0
195
- type: :development
196
- prerelease: false
197
- version_requirements: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - "~>"
200
- - !ruby/object:Gem::Version
201
- version: 1.5.0
202
201
  - !ruby/object:Gem::Dependency
203
202
  name: timecop
204
203
  requirement: !ruby/object:Gem::Requirement
@@ -229,8 +228,7 @@ dependencies:
229
228
  version: 0.5.1
230
229
  description: Rack middleware for skinning pages using a specific template
231
230
  email:
232
- - bengriffiths@gmail.com
233
- - james.stewart@digital.cabinet-office.gov.uk
231
+ - govuk-dev@digital.cabinet-office.gov.uk
234
232
  executables:
235
233
  - render_slimmer_error
236
234
  extensions: []
@@ -242,12 +240,12 @@ files:
242
240
  - bin/render_slimmer_error
243
241
  - lib/slimmer.rb
244
242
  - lib/slimmer/app.rb
245
- - lib/slimmer/cache.rb
246
243
  - lib/slimmer/component_resolver.rb
247
244
  - lib/slimmer/cucumber.rb
248
245
  - lib/slimmer/govuk_components.rb
249
246
  - lib/slimmer/govuk_request_id.rb
250
247
  - lib/slimmer/headers.rb
248
+ - lib/slimmer/http_client.rb
251
249
  - lib/slimmer/i18n_backend.rb
252
250
  - lib/slimmer/processors/body_class_copier.rb
253
251
  - lib/slimmer/processors/body_inserter.rb
@@ -274,31 +272,6 @@ files:
274
272
  - lib/slimmer/version.rb
275
273
  - lib/tasks/lint.rake
276
274
  - lib/tasks/slimmer.rake
277
- - test/cache_test.rb
278
- - test/changelog_test.rb
279
- - test/component_resolver_test.rb
280
- - test/fixtures/404.html.erb
281
- - test/fixtures/410.html.erb
282
- - test/fixtures/500.html.erb
283
- - test/fixtures/core_layout.html.erb
284
- - test/fixtures/proposition_menu.html.erb
285
- - test/fixtures/report_a_problem.raw.html.erb
286
- - test/headers_test.rb
287
- - test/processors/body_inserter_test.rb
288
- - test/processors/header_context_inserter_test.rb
289
- - test/processors/inside_header_inserter_test.rb
290
- - test/processors/metadata_inserter_test.rb
291
- - test/processors/navigation_mover_test.rb
292
- - test/processors/report_a_problem_inserter_test.rb
293
- - test/processors/search_parameter_inserter_test.rb
294
- - test/processors/search_path_setter_test.rb
295
- - test/processors/search_remover_test.rb
296
- - test/processors/tag_mover_test.rb
297
- - test/skin_test.rb
298
- - test/test_helper.rb
299
- - test/test_helpers/govuk_components_test.rb
300
- - test/test_template_dependency_on_static_test.rb
301
- - test/typical_usage_test.rb
302
275
  homepage: http://github.com/alphagov/slimmer
303
276
  licenses:
304
277
  - MIT
@@ -323,29 +296,4 @@ rubygems_version: 2.5.1
323
296
  signing_key:
324
297
  specification_version: 4
325
298
  summary: Thinner than the skinner
326
- test_files:
327
- - test/headers_test.rb
328
- - test/processors/body_inserter_test.rb
329
- - test/processors/search_path_setter_test.rb
330
- - test/processors/navigation_mover_test.rb
331
- - test/processors/report_a_problem_inserter_test.rb
332
- - test/processors/inside_header_inserter_test.rb
333
- - test/processors/tag_mover_test.rb
334
- - test/processors/search_parameter_inserter_test.rb
335
- - test/processors/search_remover_test.rb
336
- - test/processors/metadata_inserter_test.rb
337
- - test/processors/header_context_inserter_test.rb
338
- - test/test_template_dependency_on_static_test.rb
339
- - test/test_helper.rb
340
- - test/typical_usage_test.rb
341
- - test/skin_test.rb
342
- - test/component_resolver_test.rb
343
- - test/fixtures/core_layout.html.erb
344
- - test/fixtures/report_a_problem.raw.html.erb
345
- - test/fixtures/410.html.erb
346
- - test/fixtures/404.html.erb
347
- - test/fixtures/500.html.erb
348
- - test/fixtures/proposition_menu.html.erb
349
- - test/test_helpers/govuk_components_test.rb
350
- - test/cache_test.rb
351
- - test/changelog_test.rb
299
+ test_files: []