Skip to content

Commit

Permalink
Section 15.4.3: Experimenting with LinkJumbler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Apr 6, 2015
1 parent 9e64501 commit 05d2d75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ticketee/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ class Application < Rails::Application

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true

#require "link_jumbler"
#config.middleware.use LinkJumbler, { "e" => "a" }
end
end
24 changes: 24 additions & 0 deletions ticketee/lib/link_jumbler.rb
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

0 comments on commit 05d2d75

Please sign in to comment.