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


#1

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.


#2

On 6/15/07, sishen removed_email_address@domain.invalid 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


#3

Yes, i know that. Thanks. :slight_smile:

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


#4

On 6/15/07, sishen removed_email_address@domain.invalid 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:inopen’
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:inopen’
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:inopen_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:ineach’
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:inopen_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:inopen’
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/


#5

On 6/16/07, Rick DeNatale removed_email_address@domain.invalid 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:inopen’
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’


#6

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


#7

On 6/16/07, sishen removed_email_address@domain.invalid 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/