Assess potential relative URI in context of another in Ruby

I have two URIs in a Ruby program. One of them, of course, is an absolute URI, and the other can be absolute or relative. I would like to include the second in the absolute URI in the context of the first, so if the first is http://pupeno.com/blog and the second is /, the result should be http://pupeno.com/about . Any ideas how to do this?

+5
source share
2 answers

Both Ruby's built-in URI and Addressable do a short job. I prefer Addressable because it is more fully functional, but the URI is built-in.

require 'uri'

URI.join('http://pupeno.com/blog', '/about') # => #<URI::HTTP:0x00000101098538 URL:http://pupeno.com/about>

or

require 'addressable/uri'

uri = Addressable::URI.parse('http://pupeno.com/blog')
uri.join('/about') # => #<Addressable::URI:0x806704a0 URI:http://pupeno.com/about>

join, , , . , URL- . join , , .

+11

:

require 'uri'
url=URI.parse('http://pupeno.com/blog')
=> #<URI::HTTP:0x00000100e35368 URL:http://pupeno.com/blog> 
ruby-1.9.2-p0 > url.path="/about"
=> "/about" 
ruby-1.9.2-p0 > url
=> #<URI::HTTP:0x00000100e35368 URL:http://pupeno.com/about> 
+1

All Articles