slimmer 3.7.0 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,6 +29,7 @@ module Slimmer
29
29
  autoload :BodyClassCopier, 'slimmer/processors/body_class_copier'
30
30
  autoload :BodyInserter, 'slimmer/processors/body_inserter'
31
31
  autoload :ConditionalCommentMover, 'slimmer/processors/conditional_comment_mover'
32
+ autoload :CampaignNotificationInserter, 'slimmer/processors/campaign_notification_inserter'
32
33
  autoload :FooterRemover, 'slimmer/processors/footer_remover'
33
34
  autoload :GoogleAnalyticsConfigurator, 'slimmer/processors/google_analytics_configurator'
34
35
  autoload :HeaderContextInserter, 'slimmer/processors/header_context_inserter'
@@ -5,6 +5,7 @@ module Slimmer
5
5
  HEADER_PREFIX = "X-Slimmer"
6
6
 
7
7
  SLIMMER_HEADER_MAPPING = {
8
+ campaign_notification:"Campaign-Notification",
8
9
  format: "Format",
9
10
  need_id: "Need-ID",
10
11
  proposition: "Proposition",
@@ -16,13 +17,14 @@ module Slimmer
16
17
  }
17
18
 
18
19
  ARTEFACT_HEADER = "#{HEADER_PREFIX}-Artefact"
19
- FORMAT_HEADER = "#{HEADER_PREFIX}-Format"
20
- REMOVE_META_VIEWPORT = "#{HEADER_PREFIX}-Remove-Meta-Viewport"
21
- RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-Result-Count"
20
+ CAMPAIGN_NOTIFICATION = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:campaign_notification]}"
21
+ FORMAT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:format]}"
22
+ REMOVE_META_VIEWPORT = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:remove_meta_viewport]}"
23
+ RESULT_COUNT_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:result_count]}"
22
24
  SEARCH_INDEX_HEADER = "#{HEADER_PREFIX}-Search-Index"
23
25
  SEARCH_PATH_HEADER = "#{HEADER_PREFIX}-Search-Path"
24
- SKIP_HEADER = "#{HEADER_PREFIX}-Skip"
25
- TEMPLATE_HEADER = "#{HEADER_PREFIX}-Template"
26
+ SKIP_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:skip]}"
27
+ TEMPLATE_HEADER = "#{HEADER_PREFIX}-#{SLIMMER_HEADER_MAPPING[:template]}"
26
28
 
27
29
  def set_slimmer_headers(hash)
28
30
  raise InvalidHeader if (hash.keys - SLIMMER_HEADER_MAPPING.keys).any?
@@ -0,0 +1,27 @@
1
+ module Slimmer::Processors
2
+ class CampaignNotificationInserter
3
+ def initialize(skin, headers)
4
+ @headers = headers
5
+ @skin = skin
6
+ @old_campaign_selector = ".main-campaign"
7
+ @new_campaign_selector = "#campaign-notification"
8
+ end
9
+
10
+ def filter(content_document, page_template)
11
+ if @headers[Slimmer::Headers::CAMPAIGN_NOTIFICATION] == "true" &&
12
+ old_campaign = page_template.at_css(@old_campaign_selector)
13
+ new_campaign_block = campaign_notification_block
14
+ if new_campaign_block.at_css(@new_campaign_selector)
15
+ old_campaign.replace(new_campaign_block)
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def campaign_notification_block
23
+ html = @skin.template('campaign')
24
+ Nokogiri::HTML.fragment(html)
25
+ end
26
+ end
27
+ end
@@ -5,7 +5,7 @@ module Slimmer::Processors
5
5
  end
6
6
 
7
7
  def filter(src, dest)
8
- if @response.headers["X-Slimmer-Remove-Meta-Viewport"] == "true"
8
+ if @response.headers[Slimmer::Headers::REMOVE_META_VIEWPORT] == "true"
9
9
  dest.at_xpath('//head//meta[@name="viewport"]').remove
10
10
  end
11
11
  end
@@ -141,6 +141,7 @@ module Slimmer
141
141
  Processors::ReportAProblemInserter.new(self, source_request.url),
142
142
  Processors::SearchIndexSetter.new(response),
143
143
  Processors::MetaViewportRemover.new(response),
144
+ Processors::CampaignNotificationInserter.new(self, response.headers),
144
145
  ]
145
146
 
146
147
  template_name = response.headers[Headers::TEMPLATE_HEADER] || 'wrapper'
@@ -7,6 +7,8 @@ module Slimmer
7
7
  logger.debug "Slimmer: TEST MODE - Loading fixture template from #{__FILE__}"
8
8
  if name =~ /\A(.*)\.raw\z/
9
9
  %{<div id="test-#{$1}"></div>}
10
+ elsif name == "campaign"
11
+ '<section class="black" id="campaign-notification"><div><p>Notifications!</p></div></section>'
10
12
  else
11
13
  Slimmer::TestTemplate::TEMPLATE
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module Slimmer
2
- VERSION = '3.7.0'
2
+ VERSION = '3.8.0'
3
3
  end
@@ -0,0 +1 @@
1
+ <section id="campaign-notification"><p>testing...</p></section>
@@ -63,6 +63,15 @@ class HeadersTest < MiniTest::Unit::TestCase
63
63
  set_slimmer_headers remove_meta_viewport: true
64
64
  assert_equal "true", headers["X-Slimmer-Remove-Meta-Viewport"]
65
65
  end
66
+
67
+ def test_should_not_have_campaign_notification_set
68
+ assert_equal nil, headers["X-Slimmer-Campaign-Notification"]
69
+ end
70
+
71
+ def test_should_set_campaign_notification_header
72
+ set_slimmer_headers campaign_notification: true
73
+ assert_equal "true", headers["X-Slimmer-Campaign-Notification"]
74
+ end
66
75
  end
67
76
 
68
77
  describe Slimmer::Headers do
@@ -0,0 +1,49 @@
1
+ require "test_helper"
2
+
3
+ class CampaignNotificationInserterTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ @skin = stub("Skin", :template => nil)
6
+ end
7
+
8
+ def test_should_not_replace_campaign_if_header_not_set
9
+ @skin.expects(:template).with("campaign").never
10
+ campaign_inserter = Slimmer::Processors::CampaignNotificationInserter.new(@skin, {})
11
+
12
+ source = as_nokogiri %{
13
+ <html>
14
+ <body>
15
+ <section class="main-campaign group"><a href="/tour">A tour!</a></section>
16
+ </body>
17
+ </html>
18
+ }
19
+ expected = as_nokogiri %{
20
+ <html>
21
+ <body>
22
+ <section class="main-campaign group"><a href="/tour">A tour!</a></section>
23
+ </body>
24
+ </html>
25
+ }
26
+
27
+ campaign_inserter.filter(:any_source, source)
28
+ assert_equal expected.to_html, source.to_html
29
+ end
30
+
31
+ def test_should_replace_campaign_with_notification_if_header_set
32
+ campaign = '<section id="campaign-notification"><p>testing...</p></section>'
33
+ @skin.expects(:template).with('campaign').returns(campaign)
34
+
35
+ headers = {Slimmer::Headers::CAMPAIGN_NOTIFICATION => "true"}
36
+ campaign_inserter = Slimmer::Processors::CampaignNotificationInserter.new(@skin, headers)
37
+
38
+ source = as_nokogiri %{
39
+ <html><body><section class="main-campaign group"><a href="/tour">A tour!</a></section></body></html>
40
+ }
41
+
42
+ expected = as_nokogiri %{
43
+ <html><body><section id="campaign-notification"><p>testing...</p></section></body></html>
44
+ }
45
+
46
+ campaign_inserter.filter(:any_source, source)
47
+ assert_equal expected.to_html.gsub(/\n/," ").strip, source.to_html.gsub(/\n/," ").strip
48
+ end
49
+ end
@@ -77,6 +77,7 @@ class SlimmerIntegrationTest < MiniTest::Unit::TestCase
77
77
  use_template(template_name)
78
78
  use_template('related.raw')
79
79
  use_template('report_a_problem.raw')
80
+ use_template('campaign')
80
81
 
81
82
  fetch_page
82
83
  end
@@ -370,4 +370,21 @@ module TypicalUsage
370
370
  assert_equal "Something else", last_response.headers["X-Custom-Header"]
371
371
  end
372
372
  end
373
+
374
+ class CampaignNotificationInserterTest < SlimmerIntegrationTest
375
+ def test_should_update_the_campaign_with_a_notification
376
+ given_response 200, %{
377
+ <html>
378
+ <body>
379
+ <div id="wrapper">
380
+ <section class="main-campaign group"><a href="/tour">A tour!</a></section>
381
+ </div>
382
+ </body>
383
+ </html>
384
+ },
385
+ {Slimmer::Headers::CAMPAIGN_NOTIFICATION => "true"}
386
+
387
+ assert_rendered_in_template "#campaign-notification", "<p>testing...</p>"
388
+ end
389
+ end
373
390
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: slimmer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 3.7.0
5
+ version: 3.8.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Griffiths
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-10-09 00:00:00 Z
14
+ date: 2012-10-15 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: nokogiri
@@ -202,6 +202,7 @@ files:
202
202
  - lib/slimmer/processors/admin_title_inserter.rb
203
203
  - lib/slimmer/processors/report_a_problem_inserter.rb
204
204
  - lib/slimmer/processors/meta_viewport_remover.rb
205
+ - lib/slimmer/processors/campaign_notification_inserter.rb
205
206
  - lib/slimmer/processors/section_inserter.rb
206
207
  - lib/slimmer/processors/footer_remover.rb
207
208
  - lib/slimmer/headers.rb
@@ -211,11 +212,13 @@ files:
211
212
  - test/headers_test.rb
212
213
  - test/artefact_test.rb
213
214
  - test/fixtures/related.raw.html.erb
215
+ - test/fixtures/campaign.html.erb
214
216
  - test/fixtures/500.html.erb
215
217
  - test/fixtures/404.html.erb
216
218
  - test/fixtures/wrapper.html.erb
217
219
  - test/fixtures/report_a_problem.raw.html.erb
218
220
  - test/skin_test.rb
221
+ - test/processors/campaign_notification_inserter_test.rb
219
222
  - test/processors/meta_viewport_remover_test.rb
220
223
  - test/processors/logo_class_inserter_test.rb
221
224
  - test/processors/search_index_setter_test.rb
@@ -243,7 +246,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
243
246
  requirements:
244
247
  - - ">="
245
248
  - !ruby/object:Gem::Version
246
- hash: 1998055775306667395
249
+ hash: 4023505413084510363
247
250
  segments:
248
251
  - 0
249
252
  version: "0"
@@ -252,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
255
  requirements:
253
256
  - - ">="
254
257
  - !ruby/object:Gem::Version
255
- hash: 1998055775306667395
258
+ hash: 4023505413084510363
256
259
  segments:
257
260
  - 0
258
261
  version: "0"
@@ -267,11 +270,13 @@ test_files:
267
270
  - test/headers_test.rb
268
271
  - test/artefact_test.rb
269
272
  - test/fixtures/related.raw.html.erb
273
+ - test/fixtures/campaign.html.erb
270
274
  - test/fixtures/500.html.erb
271
275
  - test/fixtures/404.html.erb
272
276
  - test/fixtures/wrapper.html.erb
273
277
  - test/fixtures/report_a_problem.raw.html.erb
274
278
  - test/skin_test.rb
279
+ - test/processors/campaign_notification_inserter_test.rb
275
280
  - test/processors/meta_viewport_remover_test.rb
276
281
  - test/processors/logo_class_inserter_test.rb
277
282
  - test/processors/search_index_setter_test.rb