Severin
Severin

Reputation: 8588

Detect redirect to specific IP with Mechanize Ruby

I am using the Ruby Mechanize Gem in to fetch and parse websites and I need to detect redirects to a certain IP. Here is my basic setup:

agent = Mechanize.new
page = agent.get('http://www.example.com')

Now, its obvious how to detect the redirect as such:

is_redirect? = page.code[/30[12]/].present?

but I want to take it a step further and check what domain/IP it redirects to; so something along the lines of (pseudo-code):

if page.resolves_to(55.55.55.55)...

Any thoughts on how this can be achieved?

Upvotes: 0

Views: 316

Answers (1)

pguardiario
pguardiario

Reputation: 54984

The redirected url is in Page#uri:

require 'socket'
IPSocket::getaddress(page.uri.host)

Upvotes: 1

Related Questions