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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sishen (Guest)
on 2007-06-15 12:15
(Received via mailing list)
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.
Pat M. (Guest)
on 2007-06-15 12:27
(Received via mailing list)
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
sishen (Guest)
on 2007-06-15 16:32
(Received via mailing list)
Yes, i know that. Thanks. :)

But i confused by the read_timeout option of the open method.
Roseanne Z. (Guest)
on 2007-06-15 17:41
sishen wrote:
> Yes, i know that. Thanks. :)
>
> 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
Rick D. (Guest)
on 2007-06-16 19:49
(Received via mailing list)
On 6/15/07, sishen <removed_email_address@domain.invalid> wrote:
> Yes, i know that. Thanks. :)
>
> 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/
sishen (Guest)
on 2007-06-16 21:20
(Received via mailing list)
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: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'
Rick D. (Guest)
on 2007-06-17 01:24
(Received via mailing list)
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. <G>
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/
This topic is locked and can not be replied to.