Forum: Ruby Relative To Absolute Path Convertor

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ciocan (Guest)
on 2007-06-19 16:17
Is there a built in function/plugin that converts
relative paths from a html page to absolute ones?

Example:
target url: http://mysite.com/folder/url.html

url content:
...
<script src="/script.js"></script>
<a href="link.html">some link</a>
...

converted content:
...
<script src="http://mysite.com/script.js"></script>
< a href="http://mysite.com/folder/link.html">some link</ a>
...

How can I accomplish this using ruby ?
Axel E. (Guest)
on 2007-06-19 17:00
(Received via mailing list)
Dear Ciocan,

you could use something like

str="http://mysite.com/folder/url.html"
p File.dirname(str)
p File.basename(str)
p File.basename(str,".html")

Best regards,

Axel
Bob S. (Guest)
on 2007-06-20 17:03
(Received via mailing list)
On 6/19/07, ciocan <removed_email_address@domain.invalid> wrote:
> ...
>
> converted content:
> ...
> <script src="http://mysite.com/script.js"></script>
> < a href="http://mysite.com/folder/link.html">some link</ a>
> ...
>
> How can I accomplish this using ruby ?

Use URI#merge:

  >> require 'uri'
  => true
  >> base = URI.parse("http://mysite.com/folder/url.html")
  => #<URI::HTTP:0x4160a54 URL:http://mysite.com/folder/url.html>
  >> base.merge("/script.js").to_s
  => "http://mysite.com/script.js"
  >> base.merge("link.html").to_s
  => "http://mysite.com/folder/link.html"
This topic is locked and can not be replied to.