Skip to content

Instantly share code, notes, and snippets.

@cheald
Last active August 29, 2015 13:56
Show Gist options
  • Save cheald/9201500 to your computer and use it in GitHub Desktop.
Save cheald/9201500 to your computer and use it in GitHub Desktop.
require 'manticore'
require 'awesome_print'
class MyRedirectHandler < org.apache.http.impl.client::DefaultRedirectStrategy
def getRedirect(request, response, context)
super.tap do |next_request|
logRedirect context, response.getStatusLine.getStatusCode, next_request.getURI.to_s
end
end
private
def logRedirect(context, code, uri)
redirects = context.getAttribute("redirects")
if redirects.nil?
redirects = []
context.setAttribute("redirects", redirects)
end
redirects << [code, uri]
end
end
client = Manticore::Client.new(cookies: true) do |builder, request_config|
builder.set_redirect_strategy MyRedirectHandler.new
end
responses = [
client.async_get("http://mogotest.com"),
client.async_get("http://google.com"),
client.async_get("http://www.nytimes.com/2014/02/23/magazine/how-obamacare-could-unlock-job-opportunities.html")
]
client.execute!
redirects = responses.map do |response|
{
uri: response.request.getURI.to_s,
final_uri: response.final_url.to_s,
redirects: response.context.get_attribute("redirects")
}
end
ap redirects
[
[0] {
:uri => "http://mogotest.com",
:final_uri => "http://mogotest.com/",
:redirects => nil
},
[1] {
:uri => "http://google.com",
:final_uri => "http://www.google.com/",
:redirects => [
[0] [
[0] 301,
[1] "http://www.google.com/"
]
]
},
[2] {
:uri => "http://www.nytimes.com/2014/02/23/magazine/how-obamacare-could-unlock-job-opportunities.html",
:final_uri => "http://www.nytimes.com/2014/02/23/magazine/how-obamacare-could-unlock-job-opportunities.html?_r=0",
:redirects => [
[0] [
[0] 303,
[1] "http://www.nytimes.com/glogin?URI=http%3A%2F%2Fwww.nytimes.com%2F2014%2F02%2F23%2Fmagazine%2Fhow-obamacare-could-unlock-job-opportunities.html%3F_r%3D0"
],
[1] [
[0] 302,
[1] "http://www.nytimes.com/2014/02/23/magazine/how-obamacare-could-unlock-job-opportunities.html?_r=0"
]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment