* lib/open-uri.rb: dispatch code restructured to make it openable
that has `open' method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
5504f490a6
commit
da19d16b1c
|
@ -1,4 +1,7 @@
|
|||
Wed Feb 5 18:54:54 2003 Tanaka Akira <akr@m17n.org>
|
||||
Wed Feb 5 19:41:37 2003 Tanaka Akira <akr@m17n.org>
|
||||
|
||||
* lib/open-uri.rb: dispatch code restructured to make it openable
|
||||
that has `open' method.
|
||||
|
||||
* lib/open-uri.rb: Location: field may has a relative URI.
|
||||
pointed out by erik eriksson <ee@opera.com>.
|
||||
|
|
|
@ -58,29 +58,43 @@ require 'uri'
|
|||
require 'stringio'
|
||||
require 'time'
|
||||
|
||||
module OpenURI
|
||||
def OpenURI.open_dispatch(name, *rest, &block) #:nodoc:
|
||||
DispatchTable.each {|cond, meth|
|
||||
return meth.call(name, *rest, &block) if cond === name
|
||||
}
|
||||
return open_uri_original_open(name, *rest, &block)
|
||||
module Kernel
|
||||
private
|
||||
alias open_uri_original_open open # :nodoc:
|
||||
|
||||
# makes possible to open URIs.
|
||||
# If the first argument is URI::HTTP, URI::FTP or
|
||||
# String beginning with http:// or ftp://,
|
||||
# the URI is opened.
|
||||
# The opened file object is extended by OpenURI::Meta.
|
||||
def open(name, *rest, &block)
|
||||
if name.respond_to?("open")
|
||||
name.open(*rest, &block)
|
||||
elsif name.respond_to?("to_str") && %r{\A(http|ftp)://} =~ name
|
||||
OpenURI.open_uri(name, *rest, &block)
|
||||
else
|
||||
open_uri_original_open(name, *rest, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def OpenURI.open_uri(name, *rest) #:nodoc:
|
||||
uri = URI::Generic === name ? name : URI.parse(name)
|
||||
module OpenURI
|
||||
def OpenURI.scan_open_optional_arguments(*rest) # :nodoc:
|
||||
if !rest.empty? && (String === rest.first || Integer === rest.first)
|
||||
mode = rest.shift
|
||||
if !rest.empty? && Integer === rest.first
|
||||
perm = rest.shift
|
||||
end
|
||||
end
|
||||
if !rest.empty? && Hash === rest.first
|
||||
options = rest.shift
|
||||
end
|
||||
if !rest.empty?
|
||||
raise ArgumentError.new("extra arguments")
|
||||
return mode, perm, rest
|
||||
end
|
||||
|
||||
def OpenURI.open_uri(name, *rest) # :nodoc:
|
||||
uri = URI::Generic === name ? name : URI.parse(name)
|
||||
mode, perm, rest = OpenURI.scan_open_optional_arguments(*rest)
|
||||
options = rest.shift if !rest.empty? && Hash === rest.first
|
||||
raise ArgumentError.new("extra arguments") if !rest.empty?
|
||||
|
||||
unless mode == nil ||
|
||||
mode == 'r' || mode == 'rb' ||
|
||||
mode == O_RDONLY
|
||||
|
@ -146,12 +160,6 @@ module OpenURI
|
|||
io
|
||||
end
|
||||
|
||||
DispatchTable = [
|
||||
[URI::HTTP, method(:open_uri)],
|
||||
[URI::FTP, method(:open_uri)],
|
||||
[%r{\A(http|ftp)://}, method(:open_uri)],
|
||||
]
|
||||
|
||||
class Redirect < StandardError # :nodoc:
|
||||
def initialize(uri)
|
||||
@uri = uri
|
||||
|
@ -290,8 +298,8 @@ module OpenURI
|
|||
# Mixin for URIs.
|
||||
module OpenRead
|
||||
# opens the URI.
|
||||
def open(options={}, &block)
|
||||
OpenURI.open_uri(self, options, &block)
|
||||
def open(*rest, &block)
|
||||
OpenURI.open_uri(self, *rest, &block)
|
||||
end
|
||||
|
||||
# reads a content of the URI.
|
||||
|
@ -380,17 +388,3 @@ module URI
|
|||
include OpenURI::OpenRead
|
||||
end
|
||||
end
|
||||
|
||||
module Kernel
|
||||
private
|
||||
alias open_uri_original_open open
|
||||
|
||||
# makes possible to open URIs.
|
||||
# If the first argument is URI::HTTP, URI::FTP or
|
||||
# String beginning with http:// or ftp://,
|
||||
# the URI is opened.
|
||||
# The opened file object is extended by OpenURI::Meta.
|
||||
def open(name, *rest, &block)
|
||||
OpenURI.open_dispatch(name, *rest, &block)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue