How to set the timeout attr when using open-uri to open a ur

I met a problem using open-uri.

I want to set the timeout of the open process. And i see the open method
has
a option named “read_timeout”.
So i just code as
open(“www.abc.com”, {:read_timeout => 10}).

But, i can’t see the effect. What’s wrong with that? Any help thanks.

On 6/15/07, sishen [email protected] wrote:

I met a problem using open-uri.

I want to set the timeout of the open process. And i see the open method has
a option named “read_timeout”.
So i just code as
open(“www.abc.com”, {:read_timeout => 10}).

But, i can’t see the effect. What’s wrong with that? Any help thanks.

You can use the timeout library.

timeout(10) do
open “www.abc.com
end

it raises a TimeoutError if the request doesn’t complete in 10 seconds.

Pat

Yes, i know that. Thanks. :slight_smile:

But i confused by the read_timeout option of the open method.

On 6/15/07, sishen [email protected] wrote:

Yes, i know that. Thanks. :slight_smile:

But i confused by the read_timeout option of the open method.

I want to set the timeout of the open process. And i see the open method
has
a option named “read_timeout”.
So i just code as
open(“www.abc.com”, {:read_timeout => 10}).

But, i can’t see the effect. What’s wrong with that? Any help thanks.

Where did you see a read_timeout option? I can’t find it in any
documentation, although I might be missing something.

Also you are opening “www.abc.com” which Kernel open is seeing as a
file name rather than a URI.

First without open-uri

irb(main):001:0> open(“www.abc.com”, {:read_timeout => 10})
TypeError: can’t convert Hash into String
from (irb):1:in initialize' from (irb):1:in open’
from (irb):1
irb(main):002:0> open(“http://www.abc.com”, {:read_timeout => 10})
TypeError: can’t convert Hash into String
from (irb):2:in initialize' from (irb):2:in open’
from (irb):2

Now with open-uri

irb(main):003:0> require ‘open-uri’
=> true
irb(main):004:0> open(“www.abc.com”, {:read_timeout => 10})
TypeError: can’t convert Hash into String
from /usr/local/lib/ruby/1.8/open-uri.rb:32:in initialize' from /usr/local/lib/ruby/1.8/open-uri.rb:32:in open_uri_original_open’
from /usr/local/lib/ruby/1.8/open-uri.rb:32:in `open’
from (irb):4

Note that there’s no change since “www.abc.com” isn’t a url.

irb(main):005:0> open(“http://www.abc.com”, {:read_timeout => 10})
ArgumentError: unrecognized option: read_timeout
from /usr/local/lib/ruby/1.8/open-uri.rb:103:in check_options' from /usr/local/lib/ruby/1.8/open-uri.rb:100:in each’
from /usr/local/lib/ruby/1.8/open-uri.rb:100:in check_options' from /usr/local/lib/ruby/1.8/open-uri.rb:124:in open_uri’
from /usr/local/lib/ruby/1.8/open-uri.rb:528:in open' from /usr/local/lib/ruby/1.8/open-uri.rb:30:in open’
from (irb):5
irb(main):006:0>

And :read_timeout isn’t a known option.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 6/16/07, Rick DeNatale [email protected] wrote:

So i just code as
open(“www.abc.com”, {:read_timeout => 10}).

But, i can’t see the effect. What’s wrong with that? Any help
thanks.

Where did you see a read_timeout option? I can’t find it in any
documentation, although I might be missing something.

Also you are opening “www.abc.com” which Kernel open is seeing as a
file name rather than a URI.

Oh, sorry, it’s my mistake of careless.

First without open-uri

irb(main):001:0> open(“www.abc.com”, {:read_timeout => 10})

TypeError: can’t convert Hash into String

    from (irb):1:in `initialize'
    from (irb):1:in `open'
    from (irb):1

irb(main):002:0> open(“http://www.abc.com”, {:read_timeout => 10})
TypeError: can’t convert Hash into String
from (irb):2:in initialize' from (irb):2:in open’
from (irb):2

Now with open-uri

What’s your version? No read_timeout option of the open-uri version in
Ruby
1.8. But 1.9 or openuri in rubygems has.

module OpenURI
Options = {
:proxy => true,
:proxy_http_basic_authentication => true,
:progress_proc => true,
:content_length_proc => true,
:http_basic_authentication => true,
:read_timeout => true,
:ssl_ca_cert => nil,
:ssl_verify_mode => nil,
}

irb(main):003:0> require ‘open-uri’

sishen wrote:

Yes, i know that. Thanks. :slight_smile:

But i confused by the read_timeout option of the open method.

Read here. it might help:

open(name, *rest, &block)

makes possible to open various resources including URIs. If the first
argument respond to `open’ method, the method is called with the rest
arguments.

If the first argument is a string which begins with xxx://, it is parsed
by URI.parse. If the parsed object respond to `open’ method, the method
is called with the rest arguments.

Otherwise original open is called.

Since open-uri.rb provides URI::HTTP#open, URI::HTTPS#open and
URI::FTP#open, Kernel[#.]open can accepts such URIs and strings which
begins with http://, https:// and ftp://. In these case, the opened file
object is extended by OpenURI::Meta.

copied from
http://www.ruby-doc.org/core/classes/Kernel.html#M005991

On 6/16/07, sishen [email protected] wrote:

a option named “read_timeout”.
file name rather than a URI.
from (irb):1:in `open’
1.8. But 1.9 or openuri in rubygems has.
:ssl_verify_mode => nil,
}

Well I must have missed where you said that you were using 1.9.
The general assumption is that folks are using 1.8.x which is the
stable branch, 1.9 is experimental.

In any case, if you’re trying to open “www.abc.com” instead of
http://www.abc.com” the OpenURI code isn’t going to come into play
anyway. I don’t think that 1.9 changed that. In fact here’s the
actual 1.9 code in lib/open-uri.rb which monkeypatches Kernel#open

def open(name, *rest, &block) # :doc:
if name.respond_to?(:open)
name.open(rest, &block)
elsif name.respond_to?(:to_str) &&
%r{\A[A-Za-z][A-Za-z0-9+-.]
://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
uri.open(*rest, &block)
else
open_uri_original_open(name, *rest, &block)
end
end


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/