Project

General

Profile

Backport #3505

URI.join and Kernel::URI should accept URI objects

Added by marcandre (Marc-Andre Lafortune) over 8 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal

Description

=begin
URI.join accepts strings or URI objects, except for the first parameter which must be a string:

rubydev -r uri -e 'p URI.join("http://ruby-lang.org", URI("/foo"))'
#

rubydev -r uri -e 'p URI.join(URI("http://ruby-lang.org"), "/foo")'
/usr/local/rubydev/lib/ruby/1.9.1/uri/common.rb:156:in `split': bad URI(is not URI?): http://ruby-lang.org (URI::InvalidURIError)

I believe it should accept URI object as first parameter too.

Also, Kernel::URI accept uri strings, but not URI objects:

rubydev -r uri -e 'URI(URI("http://ruby-lang.org"))'
/usr/local/rubydev/lib/ruby/1.9.1/uri/common.rb:156:in `split': bad URI(is not URI?): http://ruby-lang.org (URI::InvalidURIError)

This corresponds to the documentation, but it is contrary to Kernel::String, Integer, Float, etc... that all accept their own type. The error message is clearly buggy (an URI object is not an invalid uri!).

I believe that Kernel::URI should accept URI objects (and return them).

The patch below corrects both and has no impact on test-all nor rubyspec.

I plan to commit it unless there is objection.

Yugui: should I commit it in the 1.9.2 branch too?

Thanks

diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index 58fd422..bda6718 100644
--- a/lib/uri/common.rb
+++ b/lib/uri/common.rb
@@ -184,12 +184,16 @@ module URI
end
end

  • def join(*str)
  • u = self.parse(str[0])
  • str[1 .. -1].each do |x|
  • u = u.merge(x)
  • def join(*uris)
  • case uris[0]
  • when Generic
  • when String
  • uris[0] = self.parse(uris[0])
  • else
  • raise ArgumentError,
  • "bad argument (expected URI object or URI string)" end
  • u
  •  uris.inject :merge
    

    end

    def extract(str, schemes = nil, &block)
    @@ -837,11 +841,20 @@ module URI
    end

module Kernel

  • # alias for URI.parse. + #
  • # This method is introduced at 1.8.2.
  • def URI(uri_str) # :doc:
  • URI.parse(uri_str)
  • # Returns +uri+ converted to a URI object.
  • #
  • def URI(uri)
  • case uri
  • when URI::Generic
  • uri
  • when String
  • URI.parse(uri)
  • else
  • raise ArgumentError,
  • "bad argument (expected URI object or URI string)"
  • end end module_function :URI end =end

History

#1

Updated by naruse (Yui NARUSE) over 8 years ago

=begin
I agree with this idea, but the patch has a problem; it breaks objects which have to_str.

irb(main):001:0> class Foo; def to_str; "foo"; end; end
=> nil
irb(main):002:0> f=Foo.new
=> #Foo:0x00000801952848
irb(main):003:0> case f; when /foo/; p $&; end
"foo"
=> "foo"
irb(main):004:0> case f; when String; p $&; end
=> nil
=end

#2

Updated by marcandre (Marc-Andre Lafortune) over 8 years ago

  • Category set to lib
  • Assignee changed from marcandre (Marc-Andre Lafortune) to yugui (Yuki Sonoda)

=begin
Fixed in trunk with r28697.
Yugui/Yusuke: Let me know if you'd like me to backport it to 1.9.2
=end

#3

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

  • Status changed from Open to Assigned

=begin

=end

Updated by marcandre (Marc-Andre Lafortune) almost 7 years ago

  • Description updated (diff)
  • Status changed from Assigned to Closed

Also available in: Atom PDF