10

Is there is a way to convert a Mechanize relative-link object to another one which contains the absolute URL.

Mechanize must know the absolute link, because I can call the click method on relative links too.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Konstantin
  • 2,983
  • 3
  • 33
  • 55

3 Answers3

23

You can just merge the page uri (which is always absolute) with the link uri:

page.uri.merge link.uri
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

This is not specific to Mechanize, but an easy way would be to use the base URL in the <base> tag and add it to the relative URL to use for whatever purpose you want. This generally works.

But, then I'm not sure if you could call the click method on that since I don't know Mechanize that well.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
woofmeow
  • 2,370
  • 16
  • 13
0

You can also use resolve

Example:

require 'mechanize'
agent = Mechanize.new

page = agent.get(url)

some_rel_url = '/something'

url = agent.resolve(some_rel_url)

Keep in mind that the other answers provided do not take into account all the possibilities to get the base url as described here

Basically this:

Image from that link

Redithion
  • 986
  • 1
  • 19
  • 32