Creating a Mechanize redirects log0
In this post, we will see how to create a redirects log to mechanize library. Mechanize is following redirects, but what if I want to know which pages it visited? It is simple and easy to add redirects logs to mechanize.
Net::HTTP is a library provides your program functions to access WWW documents. You can use it to send GET or POST requests. You can read the response and take an action depending on the response.
The problem you may face is that if the response is 301 Redirect, then you should follow redirects yourself. The response body will be like the following:
<html><body>You are being <a href="http://localhost:3000/user_sessions/new">redirected</a>.</body></html>
A lot of work :(
Mechanize library is used for automating interaction with websites. It follows redirects, can follow links, and submit forms. You can also define the redirection_limits in case it will get in a too long redirections path.
While I was using mechanize, I needed to know the pages it visited while redirects. Unfortunately it wasn't supported. However, it wasn't hard to add it.
Now, lets add our sugar... I will add it to mechanize version 0.9.3. We will play around lib/www/mechanize.rb file
First, lets add redirects_log attribute accessor around line#85
attr_accessor :scheme_handlers attr_accessor :redirection_limit attr_accessor :redirects_log
And lets initialize this array in Mechanize initialize method
@redirects_log = []
Then modify around line#550 to add the visited page to our redirects_log array
elsif res_klass <= Net::HTTPRedirection
return page unless follow_redirect?
log.info("follow redirect to: #{ response['Location'] }") if log
@redirects_log << response['Location']
from_uri = page.uri
Finally, we can read the redirects logs
agent.redirects_log
It is simple and easy. That is the honey of the open source community :)

0 Comments
No comments have been made on this post.
Leave a Comment...