slimmer 1.1.40 → 1.1.41
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/app.rb +1 -1
- data/lib/slimmer/body_inserter.rb +5 -4
- data/lib/slimmer/skin.rb +9 -5
- data/lib/slimmer/version.rb +1 -1
- data/test/processors/body_inserter_test.rb +4 -4
- data/test/skin_test.rb +4 -4
- data/test/test_helper.rb +7 -2
- data/test/typical_usage_test.rb +15 -0
- metadata +4 -4
data/lib/slimmer/app.rb
CHANGED
@@ -20,7 +20,7 @@ module Slimmer
|
|
20
20
|
options[:asset_host] = Plek.current.find("assets")
|
21
21
|
end
|
22
22
|
|
23
|
-
@skin = Skin.new options
|
23
|
+
@skin = Skin.new options.merge(logger: logger)
|
24
24
|
end
|
25
25
|
|
26
26
|
def call(env)
|
@@ -1,12 +1,13 @@
|
|
1
1
|
module Slimmer
|
2
2
|
class BodyInserter
|
3
|
-
def initialize(
|
4
|
-
@
|
3
|
+
def initialize(source_id='wrapper', destination_id='wrapper')
|
4
|
+
@source_selector = '#' + source_id
|
5
|
+
@destination_selector = '#' + destination_id
|
5
6
|
end
|
6
7
|
|
7
8
|
def filter(src,dest)
|
8
|
-
body = Nokogiri::HTML.fragment(src.at_css(@
|
9
|
-
dest.at_css(@
|
9
|
+
body = Nokogiri::HTML.fragment(src.at_css(@source_selector).to_html)
|
10
|
+
dest.at_css(@destination_selector).replace(body)
|
10
11
|
end
|
11
12
|
end
|
12
13
|
end
|
data/lib/slimmer/skin.rb
CHANGED
@@ -18,13 +18,17 @@ module Slimmer
|
|
18
18
|
attr_accessor :strict
|
19
19
|
private :strict=, :strict
|
20
20
|
|
21
|
+
attr_accessor :options
|
22
|
+
private :options=, :options
|
23
|
+
|
21
24
|
# TODO: Extract the cache to something we can pass in instead of using
|
22
25
|
# true/false and an in-memory cache.
|
23
|
-
def initialize
|
24
|
-
self.
|
26
|
+
def initialize options = {}
|
27
|
+
self.options = options
|
28
|
+
self.asset_host = options[:asset_host]
|
25
29
|
self.templated_cache = {}
|
26
|
-
self.prefix = prefix
|
27
|
-
self.use_cache = use_cache
|
30
|
+
self.prefix = options[:prefix]
|
31
|
+
self.use_cache = options[:use_cache] || false
|
28
32
|
self.logger = options[:logger] || NullLogger.instance
|
29
33
|
self.strict = options[:strict] || (%w{development test}.include?(ENV['RACK_ENV']))
|
30
34
|
end
|
@@ -168,7 +172,7 @@ module Slimmer
|
|
168
172
|
processors = [
|
169
173
|
TitleInserter.new(),
|
170
174
|
TagMover.new(),
|
171
|
-
BodyInserter.new(),
|
175
|
+
BodyInserter.new(options[:wrapper_id] || 'wrapper'),
|
172
176
|
BodyClassCopier.new,
|
173
177
|
HeaderContextInserter.new(),
|
174
178
|
SectionInserter.new(),
|
data/lib/slimmer/version.rb
CHANGED
@@ -26,18 +26,18 @@ class BodyInserterTest < MiniTest::Unit::TestCase
|
|
26
26
|
assert_equal unicode_endash, template.at_css("#wrapper p").inner_text
|
27
27
|
end
|
28
28
|
|
29
|
-
def
|
29
|
+
def test_should_allow_replacement_of_arbitrary_segments_into_wrapper
|
30
30
|
template = as_nokogiri %{
|
31
31
|
<html><body><div>
|
32
32
|
<div id="wrapper">don't touch this</div>
|
33
|
-
|
33
|
+
</body></html>
|
34
34
|
}
|
35
35
|
source = as_nokogiri %{
|
36
36
|
<html><body><div id="some_other_id"><p>this should be moved</p></div></body></html>
|
37
37
|
}
|
38
38
|
|
39
|
-
Slimmer::BodyInserter.new("
|
40
|
-
|
39
|
+
Slimmer::BodyInserter.new("some_other_id").filter(source, template)
|
40
|
+
assert_not_in template, "#wrapper"
|
41
41
|
assert_in template, "#some_other_id", %{<p>this should be moved</p>}
|
42
42
|
end
|
43
43
|
end
|
data/test/skin_test.rb
CHANGED
@@ -2,7 +2,7 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class SkinTest < MiniTest::Unit::TestCase
|
4
4
|
def test_template_can_be_loaded
|
5
|
-
skin = Slimmer::Skin.new "http://example.local/"
|
5
|
+
skin = Slimmer::Skin.new asset_host: "http://example.local/"
|
6
6
|
expected_url = "http://example.local/templates/example.html.erb"
|
7
7
|
stub_request(:get, expected_url).to_return :body => "<foo />"
|
8
8
|
|
@@ -13,7 +13,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_interpolate_values_for_prefix
|
16
|
-
skin = Slimmer::Skin.new "http://example.local/", false, "this-is-the-prefix"
|
16
|
+
skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: false, prefix: "this-is-the-prefix"
|
17
17
|
expected_url = "http://example.local/templates/example.html.erb"
|
18
18
|
stub_request(:get, expected_url).to_return :body => "<p><%= prefix %></p>"
|
19
19
|
|
@@ -22,7 +22,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_should_raise_appropriate_exception_when_template_not_found
|
25
|
-
skin = Slimmer::Skin.new "http://example.local/"
|
25
|
+
skin = Slimmer::Skin.new asset_host: "http://example.local/"
|
26
26
|
expected_url = "http://example.local/templates/example.html.erb"
|
27
27
|
stub_request(:get, expected_url).to_return(:status => '404')
|
28
28
|
|
@@ -32,7 +32,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_should_raise_appropriate_exception_when_cant_reach_template_host
|
35
|
-
skin = Slimmer::Skin.new "http://example.local/"
|
35
|
+
skin = Slimmer::Skin.new asset_host: "http://example.local/"
|
36
36
|
expected_url = "http://example.local/templates/example.html.erb"
|
37
37
|
stub_request(:get, expected_url).to_raise(Errno::ECONNREFUSED)
|
38
38
|
|
data/test/test_helper.rb
CHANGED
@@ -27,12 +27,12 @@ end
|
|
27
27
|
class SlimmerIntegrationTest < MiniTest::Unit::TestCase
|
28
28
|
include Rack::Test::Methods
|
29
29
|
|
30
|
-
def self.given_response(code, body, headers={})
|
30
|
+
def self.given_response(code, body, headers={}, app_options={})
|
31
31
|
define_method(:app) do
|
32
32
|
inner_app = proc { |env|
|
33
33
|
[code, headers.merge("Content-Type" => "text/html"), body]
|
34
34
|
}
|
35
|
-
Slimmer::App.new inner_app, :
|
35
|
+
Slimmer::App.new inner_app, {asset_host: "http://template.local"}.merge(app_options)
|
36
36
|
end
|
37
37
|
|
38
38
|
define_method :teardown do
|
@@ -91,4 +91,9 @@ class SlimmerIntegrationTest < MiniTest::Unit::TestCase
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|
94
|
+
|
95
|
+
def assert_no_selector(selector, message=nil)
|
96
|
+
message ||= "Expected not to find #{selector.inspect}, but did"
|
97
|
+
assert_nil Nokogiri::HTML.parse(last_response.body).at_css(selector), message
|
98
|
+
end
|
94
99
|
end
|
data/test/typical_usage_test.rb
CHANGED
@@ -308,6 +308,21 @@ module TypicalUsage
|
|
308
308
|
def test_should_return_503_if_an_API_call_times_out
|
309
309
|
assert_equal 503, last_response.status
|
310
310
|
end
|
311
|
+
end
|
312
|
+
|
313
|
+
class ArbitraryWrapperIdTest < SlimmerIntegrationTest
|
314
|
+
|
315
|
+
given_response 200, %{
|
316
|
+
<html>
|
317
|
+
<body>
|
318
|
+
<div id="custom_wrapper">The body of the page</div>
|
319
|
+
</body>
|
320
|
+
</html>
|
321
|
+
}, {}, {wrapper_id: "custom_wrapper"}
|
311
322
|
|
323
|
+
def test_should_replace_wrapper_with_custom_wrapper
|
324
|
+
assert_rendered_in_template "body .content #custom_wrapper", "The body of the page"
|
325
|
+
assert_no_selector "#wrapper"
|
326
|
+
end
|
312
327
|
end
|
313
328
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: slimmer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.1.
|
5
|
+
version: 1.1.41
|
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-05-
|
14
|
+
date: 2012-05-29 00:00:00 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: nokogiri
|
@@ -204,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
204
204
|
requirements:
|
205
205
|
- - ">="
|
206
206
|
- !ruby/object:Gem::Version
|
207
|
-
hash:
|
207
|
+
hash: -3341972131141185344
|
208
208
|
segments:
|
209
209
|
- 0
|
210
210
|
version: "0"
|
@@ -213,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
213
213
|
requirements:
|
214
214
|
- - ">="
|
215
215
|
- !ruby/object:Gem::Version
|
216
|
-
hash:
|
216
|
+
hash: -3341972131141185344
|
217
217
|
segments:
|
218
218
|
- 0
|
219
219
|
version: "0"
|