27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

@app = app<br />

@letters = letters<br />

end<br />

Middleware<br />

def call(env)<br />

status, headers, response = @app.call(env)<br />

body = Nokogiri::HTML(response.body)<br />

body.css("a").each do |a|<br />

@letters.each do |find, replace|<br />

a.content = a.content.gsub(find.to_s, replace.to_s)<br />

end<br />

end<br />

[status, headers, body.to_s]<br />

end<br />

end<br />

In this file you’ve defined the LinkJumbler class, which contains an initialize and<br />

a call method. The initialize method sets the stage, setting up the @app and<br />

@letters variables you’ll use in your call method.<br />

In the call method, you make a call down the middleware stack in order to set up<br />

your status, headers, and body values. You can do this because the @app.call(env)<br />

call will always return a three-element array. Each element of this array will be<br />

assigned to its respective variable. In a Rails application’s middleware stack, the third<br />

element isn’t an array but rather an instance of ActionDispatch::Response. To get to<br />

the good part of this response you can use the body method, like you do on the second<br />

line of your call method.<br />

With this body you use the Nokogiri::HTML method (provided by the require<br />

'nokogiri' line at the top of this file) to parse the body returned by the application<br />

into a Nokogiri::HTML::Document object. This will allow you to parse the page more<br />

easily than if you used regular expressions. With this object, you call the css method<br />

and pass it the "a" argument, which finds all a tags in the response body. You then<br />

iterate through each of these tags and go through all of your letters from @letters,<br />

using the keys of the hash as the find argument and the values as the replace argument.<br />

You then set the content of each of the a tags to be the substituted result.<br />

Finally, you return a three-element array using your new body, resulting in links<br />

being jumbled. To see this middleware in action, you’ll need to add it to the middleware<br />

stack in your application. To do that, put these two lines inside the Ticketee<br />

::Application class definition in config/application.rb:<br />

require 'link_jumbler'<br />

config.middleware.use LinkJumbler, { "e" => "a" }<br />

The config.middleware.use method will add your middleware to the end of the<br />

middleware stack, making it the last piece of middleware to be processed before a<br />

request hits your application. 8 Any additional arguments passed to the use method<br />

8 For more methods for config.middleware look at the “Configuring Middleware” section of the Configuring<br />

official guide: http://guides.rubyonrails.org/configuring.html#configuring-middleware.<br />

539

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!