cypher-rack-lolspeak 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/README.markdown +7 -0
- data/Rakefile +53 -0
- data/VERSION.yml +4 -0
- data/lib/rack/contrib/icanhazlolcode.rb +50 -0
- data/test/spec_rack_icanhazlolcode.rb +72 -0
- metadata +57 -0
data/README.markdown
ADDED
data/Rakefile
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'jeweler'
|
|
6
|
+
Jeweler::Tasks.new do |gem|
|
|
7
|
+
gem.name = "rack-lolspeak"
|
|
8
|
+
gem.summary = %Q{TODO}
|
|
9
|
+
gem.email = "markus.prinz@nuclearsquid.com"
|
|
10
|
+
gem.homepage = "http://github.com/cypher/rack-lolspeak"
|
|
11
|
+
gem.authors = ["Markus Prinz"]
|
|
12
|
+
|
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
|
14
|
+
end
|
|
15
|
+
rescue LoadError
|
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
desc "Run all tests"
|
|
20
|
+
task :test do
|
|
21
|
+
sh "specrb -Ilib:test -w #{ENV['TEST'] || '-a'} #{ENV['TESTOPTS']}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
begin
|
|
25
|
+
require 'rcov/rcovtask'
|
|
26
|
+
Rcov::RcovTask.new do |test|
|
|
27
|
+
test.libs << 'test'
|
|
28
|
+
test.pattern = 'test/**/*_test.rb'
|
|
29
|
+
test.verbose = true
|
|
30
|
+
end
|
|
31
|
+
rescue LoadError
|
|
32
|
+
task :rcov do
|
|
33
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
task :default => :test
|
|
39
|
+
|
|
40
|
+
require 'rake/rdoctask'
|
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
|
42
|
+
if File.exist?('VERSION.yml')
|
|
43
|
+
config = YAML.load(File.read('VERSION.yml'))
|
|
44
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
|
45
|
+
else
|
|
46
|
+
version = ""
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
50
|
+
rdoc.title = "rack-lolspeak #{version}"
|
|
51
|
+
rdoc.rdoc_files.include('README*')
|
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
53
|
+
end
|
data/VERSION.yml
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'lolspeak'
|
|
2
|
+
require 'hpricot'
|
|
3
|
+
|
|
4
|
+
module Rack
|
|
5
|
+
module Contrib
|
|
6
|
+
class ICanHazLOLCode
|
|
7
|
+
def initialize(app)
|
|
8
|
+
@app = app
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def lolify(elem)
|
|
12
|
+
case elem
|
|
13
|
+
when Hpricot::Elem
|
|
14
|
+
elem.children.each { |e| lolify(e) } if elem.children
|
|
15
|
+
when Hpricot::Text
|
|
16
|
+
elem.content = elem.to_s.to_lolspeak.strip
|
|
17
|
+
else
|
|
18
|
+
puts "Don't know what that is"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def call(env)
|
|
23
|
+
status, headers, body = @app.call(env)
|
|
24
|
+
|
|
25
|
+
case headers['Content-Type']
|
|
26
|
+
when 'text/plain'
|
|
27
|
+
body = body.to_lolspeak
|
|
28
|
+
headers['Content-Length'] = body.size.to_s
|
|
29
|
+
when 'text/html'
|
|
30
|
+
doc = Hpricot(body)
|
|
31
|
+
|
|
32
|
+
# Swap out the title
|
|
33
|
+
title = doc.at('title')
|
|
34
|
+
title.inner_html = title.inner_html.to_lolspeak if title
|
|
35
|
+
|
|
36
|
+
# Swap out stuff inside the elements
|
|
37
|
+
elements = doc.search('body')
|
|
38
|
+
elements.each do |elem|
|
|
39
|
+
lolify(elem)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
body = doc.to_html
|
|
43
|
+
headers['Content-Length'] = body.size.to_s
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
[status, headers, body]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require 'test/spec'
|
|
2
|
+
require 'rack/mock'
|
|
3
|
+
require 'rack/contrib/icanhazlolcode'
|
|
4
|
+
|
|
5
|
+
context "Rack::Contrib::ICanHazLOLCode" do
|
|
6
|
+
context "text/plain" do
|
|
7
|
+
app = lambda {|env| [200, {'Content-Type' => 'text/plain'}, "Hello, kitty"] }
|
|
8
|
+
|
|
9
|
+
specify "should convert the body to lolspeak" do
|
|
10
|
+
status, headers, body = Rack::Contrib::ICanHazLOLCode.new(app).call({})
|
|
11
|
+
|
|
12
|
+
status.should.equal 200
|
|
13
|
+
body.should.equal "y halo thar, kitty"
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
context 'text/html' do
|
|
18
|
+
page_title = "This is the document's title"
|
|
19
|
+
title = "Glorious title of document!"
|
|
20
|
+
paragraph = "This is a paragraph<br />with multiple lines<br />in it."
|
|
21
|
+
app = lambda do |env|
|
|
22
|
+
body = <<"HTML"
|
|
23
|
+
<html>
|
|
24
|
+
<head>
|
|
25
|
+
<title>#{page_title}</title>
|
|
26
|
+
</head>
|
|
27
|
+
<body>
|
|
28
|
+
<h1>#{title}</h1>
|
|
29
|
+
<div>
|
|
30
|
+
<p>
|
|
31
|
+
#{paragraph}
|
|
32
|
+
</p>
|
|
33
|
+
</div>
|
|
34
|
+
</body>
|
|
35
|
+
</html>
|
|
36
|
+
HTML
|
|
37
|
+
|
|
38
|
+
[200, {'Content-Type' => 'text/html'}, body]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
specify "should convert the title to lolspeak" do
|
|
42
|
+
status, headers, body = Rack::Contrib::ICanHazLOLCode.new(app).call({})
|
|
43
|
+
|
|
44
|
+
status.should.equal 200
|
|
45
|
+
|
|
46
|
+
doc = Hpricot(body)
|
|
47
|
+
doc.search('title').inner_html.should.equal page_title.to_lolspeak
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
specify "should convert the body to lolspeak" do
|
|
51
|
+
status, headers, body = Rack::Contrib::ICanHazLOLCode.new(app).call({})
|
|
52
|
+
|
|
53
|
+
status.should.equal 200
|
|
54
|
+
|
|
55
|
+
doc = Hpricot(body)
|
|
56
|
+
doc.search('h1').inner_html.should.equal title.to_lolspeak
|
|
57
|
+
doc.search('p').inner_html.should.equal paragraph.to_lolspeak
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
specify "should preserve the HTML tags" do
|
|
61
|
+
status, headers, body = Rack::Contrib::ICanHazLOLCode.new(app).call({})
|
|
62
|
+
|
|
63
|
+
status.should.equal 200
|
|
64
|
+
|
|
65
|
+
doc = Hpricot(body)
|
|
66
|
+
doc.search('h1').should.not.equal nil
|
|
67
|
+
doc.search('div').should.not.equal nil
|
|
68
|
+
doc.search('p').should.not.equal nil
|
|
69
|
+
doc.search('br').should.not.equal nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: cypher-rack-lolspeak
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Markus Prinz
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2009-04-12 00:00:00 -07:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description:
|
|
17
|
+
email: markus.prinz@nuclearsquid.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.markdown
|
|
24
|
+
files:
|
|
25
|
+
- README.markdown
|
|
26
|
+
- Rakefile
|
|
27
|
+
- VERSION.yml
|
|
28
|
+
- lib/rack/contrib/icanhazlolcode.rb
|
|
29
|
+
- test/spec_rack_icanhazlolcode.rb
|
|
30
|
+
has_rdoc: true
|
|
31
|
+
homepage: http://github.com/cypher/rack-lolspeak
|
|
32
|
+
post_install_message:
|
|
33
|
+
rdoc_options:
|
|
34
|
+
- --charset=UTF-8
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: "0"
|
|
42
|
+
version:
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: "0"
|
|
48
|
+
version:
|
|
49
|
+
requirements: []
|
|
50
|
+
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 1.2.0
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 2
|
|
55
|
+
summary: TODO
|
|
56
|
+
test_files:
|
|
57
|
+
- test/spec_rack_icanhazlolcode.rb
|