From: Rick DeNatale Date: 2007-06-17T06:24:04+09:00 Subject: Re: How to set the timeout attr when using open-uri to open a url. On 6/16/07, sishen wrote: > On 6/16/07, Rick DeNatale wrote: > > > > On 6/15/07, sishen 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. > > > 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, > } 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/