-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Section 15.4.3: Experimenting with LinkJumbler middleware
- Loading branch information
1 parent
9e64501
commit 05d2d75
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "nokogiri" | ||
|
||
class LinkJumbler | ||
def initialize(app, letters) | ||
@app = app | ||
@letters = letters | ||
end | ||
|
||
def call(env) | ||
status, headers, response = @app.call(env) | ||
if headers['Content-Type'].include?("text/html") | ||
body = Nokogiri::HTML(response.body) | ||
body.css("a").each do |a| | ||
@letters.each do |find, replace| | ||
a.content = a.content.gsub(find.to_s, replace.to_s) | ||
end | ||
end | ||
else | ||
body = response.body | ||
end | ||
|
||
[status, headers, Rack::Response.new(body.to_s)] | ||
end | ||
end |