rack-plastic 0.0.3 → 0.1.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/CHANGELOG +3 -0
- data/LICENSE +1 -1
- data/README.rdoc +10 -6
- data/Rakefile +5 -3
- data/lib/plastic_test_helper.rb +82 -0
- metadata +67 -14
data/CHANGELOG
CHANGED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
== Description
|
4
4
|
|
5
5
|
If you are creating Rack middleware that changes the HTML response, use
|
6
|
-
Plastic to get a head start. Plastic takes care of the
|
6
|
+
Rack::Plastic to get a head start. Rack::Plastic takes care of the
|
7
7
|
boilerplate Rack glue so that you can focus on simply changing the HTML.
|
8
8
|
|
9
9
|
== Usage
|
@@ -26,18 +26,22 @@ the doc will be converted to an HTML string, then the string will be
|
|
26
26
|
passed to change_html_string.
|
27
27
|
|
28
28
|
Rack::Plastic also provides some convenience methods for interacting with
|
29
|
-
Rack and Nokogiri.
|
29
|
+
Rack and Nokogiri as well as some convenience methods for testing.
|
30
30
|
|
31
31
|
== Examples
|
32
32
|
|
33
33
|
The test/middlewares directory has examples of writing middleware using
|
34
|
-
Plastic.
|
34
|
+
Rack::Plastic.
|
35
35
|
|
36
36
|
== Testing
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
Rack::Plastic comes with some test helpers that make it easier for you to
|
39
|
+
write tests for your Rack middleware. Refer to the documentation for
|
40
|
+
PlasticTestHelper for more details.
|
41
|
+
|
42
|
+
If you want to test Rack::Plastic itself, this gem doesn't come with automated tests,
|
43
|
+
but it provides manual tests. Each of the example middlewares is inserted into a
|
44
|
+
Sinatra, Rails, and Rack test app.
|
41
45
|
|
42
46
|
To run the Rails test app:
|
43
47
|
* cd test/railsapp
|
data/Rakefile
CHANGED
@@ -13,17 +13,19 @@ begin
|
|
13
13
|
require 'jeweler'
|
14
14
|
Jeweler::Tasks.new do |s|
|
15
15
|
s.name = "rack-plastic"
|
16
|
-
s.version = "0.0
|
16
|
+
s.version = "0.1.0"
|
17
17
|
s.author = "Wyatt Greene"
|
18
18
|
s.email = "techiferous@gmail.com"
|
19
19
|
s.summary = "Helps you write Rack middleware using Nokogiri."
|
20
20
|
s.description = %Q{
|
21
21
|
If you are creating Rack middleware that changes the HTML response, use
|
22
|
-
Plastic to get a head start. Plastic takes care of the
|
22
|
+
Rack::Plastic to get a head start. Rack::Plastic takes care of the
|
23
23
|
boilerplate Rack glue so that you can focus on simply changing the HTML.
|
24
24
|
}
|
25
25
|
s.add_dependency('rack', '>= 1.0.0')
|
26
26
|
s.add_dependency('nokogiri', '>= 1.4.0')
|
27
|
+
s.add_development_dependency('dirb')
|
28
|
+
s.add_development_dependency('colored')
|
27
29
|
s.require_path = "lib"
|
28
30
|
s.files = []
|
29
31
|
s.files << "README.rdoc"
|
@@ -38,5 +40,5 @@ begin
|
|
38
40
|
end
|
39
41
|
Jeweler::GemcutterTasks.new
|
40
42
|
rescue LoadError
|
41
|
-
puts "Jeweler (or a dependency) not available. Install it with:
|
43
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
42
44
|
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rack/mock'
|
2
|
+
require 'dirb'
|
3
|
+
require 'colored'
|
4
|
+
|
5
|
+
# Mix this module into Test::Unit::TestCase to have access to these
|
6
|
+
# test helpers when using Test::Unit.
|
7
|
+
#
|
8
|
+
# Here's an example of how you can use these convenience methods:
|
9
|
+
#
|
10
|
+
# def test_basic_document
|
11
|
+
# before_html = %Q{
|
12
|
+
# <!DOCTYPE html
|
13
|
+
# PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
14
|
+
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
15
|
+
# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
16
|
+
# <head>
|
17
|
+
# <title>Testing Rack::SexChange</title>
|
18
|
+
# </head>
|
19
|
+
# <body>
|
20
|
+
# Hi, Mom!
|
21
|
+
# </body>
|
22
|
+
# </html>
|
23
|
+
# }
|
24
|
+
# expected_html = %Q{
|
25
|
+
# <!DOCTYPE html
|
26
|
+
# PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
27
|
+
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
28
|
+
# <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
29
|
+
# <head>
|
30
|
+
# <title>Testing Rack::SexChange</title>
|
31
|
+
# </head>
|
32
|
+
# <body>
|
33
|
+
# Hi, Dad!
|
34
|
+
# </body>
|
35
|
+
# </html>
|
36
|
+
# }
|
37
|
+
# after_html = process_html(before_html, Rack::SexChange)
|
38
|
+
# assert_html_equal expected_html, after_html
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
module PlasticTestHelper
|
42
|
+
|
43
|
+
# This takes care of the "plumbing" involved in testing your middleware.
|
44
|
+
# All you have to provide is an HTML string, the class of
|
45
|
+
# your middleware (not the name of your class or an object, but
|
46
|
+
# the class itself), and finally some optional options to use when
|
47
|
+
# instantiating your middleware class.
|
48
|
+
#
|
49
|
+
# This method will return the resulting HTML string (the body of the
|
50
|
+
# middleware's response).
|
51
|
+
#
|
52
|
+
# Examples:
|
53
|
+
# resulting_html = process_html(html, Rack::Linkify)
|
54
|
+
# resulting_html = process_html(html, Rack::Linkify, :twitter => true)
|
55
|
+
#
|
56
|
+
def process_html(html, middleware_class, options={})
|
57
|
+
app = lambda { |env| [200, {'Content-Type' => 'text/html'}, html] }
|
58
|
+
app2 = middleware_class.new(app, options)
|
59
|
+
Rack::MockRequest.new(app2).get('/', :lint => true).body
|
60
|
+
end
|
61
|
+
|
62
|
+
# this convenience method makes it easy to test changes to HTML strings
|
63
|
+
#
|
64
|
+
def assert_html_equal(expected_html_string, actual_html_string)
|
65
|
+
# Nokogiri does not preserve the same whitespace between tags when
|
66
|
+
# it processes HTML. This means we can't do a simple string comparison.
|
67
|
+
# However, if we run both the expected HTML string and the actual HTML
|
68
|
+
# string through Nokogiri, then the whitespace will be changed in
|
69
|
+
# the same way and we can do a simple string comparison.
|
70
|
+
expected = Nokogiri::HTML(expected_html_string).to_html
|
71
|
+
actual = Nokogiri::HTML(actual_html_string).to_html
|
72
|
+
preamble = "\n"
|
73
|
+
preamble = "*****************************************************\n"
|
74
|
+
preamble << "* The actual HTML does not match the expected HTML. *\n"
|
75
|
+
preamble << "* The differences are highlighted below. *\n"
|
76
|
+
preamble << "*****************************************************\n"
|
77
|
+
message = preamble.magenta
|
78
|
+
message << Dirb::Diff.new(expected, actual).to_s(:color)
|
79
|
+
assert_block(message) { expected == actual }
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-plastic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Wyatt Greene
|
@@ -9,30 +15,70 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-07-18 00:00:00 -04:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: rack
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 23
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 0
|
23
34
|
version: 1.0.0
|
24
|
-
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
38
|
name: nokogiri
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
32
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 7
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 4
|
49
|
+
- 0
|
33
50
|
version: 1.4.0
|
34
|
-
|
35
|
-
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: dirb
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: colored
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
description: "\n If you are creating Rack middleware that changes the HTML response, use\n Rack::Plastic to get a head start. Rack::Plastic takes care of the\n boilerplate Rack glue so that you can focus on simply changing the HTML.\n "
|
36
82
|
email: techiferous@gmail.com
|
37
83
|
executables: []
|
38
84
|
|
@@ -47,6 +93,7 @@ files:
|
|
47
93
|
- README.rdoc
|
48
94
|
- Rakefile
|
49
95
|
- lib/plastic_helper.rb
|
96
|
+
- lib/plastic_test_helper.rb
|
50
97
|
- lib/rack-plastic.rb
|
51
98
|
- test/middlewares/initial.rb
|
52
99
|
- test/middlewares/intro.rb
|
@@ -119,21 +166,27 @@ rdoc_options:
|
|
119
166
|
require_paths:
|
120
167
|
- lib
|
121
168
|
required_ruby_version: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
122
170
|
requirements:
|
123
171
|
- - ">="
|
124
172
|
- !ruby/object:Gem::Version
|
173
|
+
hash: 3
|
174
|
+
segments:
|
175
|
+
- 0
|
125
176
|
version: "0"
|
126
|
-
version:
|
127
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
|
+
none: false
|
128
179
|
requirements:
|
129
180
|
- - ">="
|
130
181
|
- !ruby/object:Gem::Version
|
182
|
+
hash: 3
|
183
|
+
segments:
|
184
|
+
- 0
|
131
185
|
version: "0"
|
132
|
-
version:
|
133
186
|
requirements:
|
134
187
|
- none
|
135
188
|
rubyforge_project:
|
136
|
-
rubygems_version: 1.3.
|
189
|
+
rubygems_version: 1.3.7
|
137
190
|
signing_key:
|
138
191
|
specification_version: 3
|
139
192
|
summary: Helps you write Rack middleware using Nokogiri.
|