Get a web page with open-uri

Hello !

I’m trying to get a web page width open-uri width this code :

require ‘open-uri’
open(“http://www.google.com”) {|src|
open(“test.htm”,“wb”) {|dst|
dst.write(src.read)
}
}

But it returns me :
cannot convert File into String

src should be a Tempfile.
I think there is an other method called open, what is the full name of
the open uri method ? How could I do ?

Thanks.

On Wed, Jul 8, 2009 at 11:52 AM, Andy P.[email protected]
wrote:

require ‘open-uri’
open(“http://www.google.com”) {|src|
open(“test.htm”,“wb”) {|dst|
dst.write(src.read)
}
}

Andy

Pasted the code directly into irb and it worked perfectly.

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings

At 2009-07-08 05:52AM, “Andy P.” wrote:

Hello !

I’m trying to get a web page width open-uri width this code :

require ‘open-uri’
open(“http://www.google.com”) {|src|
open(“test.htm”,“wb”) {|dst|

You’re looking for the class method in File:

 File.open("test.htm","wb") {|dst|

Glenn J. wrote:

At 2009-07-08 05:52AM, “Andy P.” wrote:

Hello !

I’m trying to get a web page width open-uri width this code :

require ‘open-uri’
open(“http://www.google.com”) {|src|
open(“test.htm”,“wb”) {|dst|

You’re looking for the class method in File:

 File.open("test.htm","wb") {|dst|

Yes but it don’t work with :
open(“www.google.com”) {|src|

I’m not trying that in irb, I imported all the files of uri, open-uri,
but it don’t work. What is the full name of the open uri method who
creates a TempFile ?
Must I import other files ?

Thanks.

Robert K. wrote:

2009/7/8 Andy P. [email protected]:

You’re looking for the class method in File:
Must I import other files ?
Can you show specific code and errors? As for Andrew, this worked for
me out of the box:

09:26:33 Temp$ irb19
Ruby version 1.9.1
irb(main):001:0> require ‘open-uri’
=> true
irb(main):002:0> open(“http://www.google.com”) {|src|
irb(main):003:1* open(“test.htm”,“wb”) {|dst|
irb(main):004:2* dst.write(src.read)
irb(main):005:2> }
irb(main):006:1> }
=> 5337
irb(main):007:0> exit
09:26:54 Temp$ wc test.htm
9 190 5337 test.htm
09:27:02 Temp$

Kind regards

robert

Sorry for the delay.

Yes, it works !

Now, I would want to download an image file using sockets.

require ‘socket’
host = “www.google.fr
port = 80
path = “/intl/fr_fr/images/logo.gif” #google logo
request = “GET #{path} GIF\r\n\r\n”
socket = TCPSocket.open(host, port)
socket.print(request)
response = socket.read
img = response.split(“\r\n\r\n”, 2)
puts img

It doesn’t works : it returns :
“HTTP/1.1 400 Bad Request”

How to do ?

request = “GET #{path} GIF\r\n\r\n”

It doesn’t works : it returns :
“HTTP/1.1 400 Bad Request”

How to do ?

http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html

7stud – wrote:

request = “GET #{path} GIF\r\n\r\n”

It doesn’t works : it returns :
“HTTP/1.1 400 Bad Request”

How to do ?

HTTP/1.1: Request

Yes it works, thanks all !

2009/7/8 Andy P. [email protected]:

You’re looking for the class method in File:
Must I import other files ?
Can you show specific code and errors? As for Andrew, this worked for
me out of the box:

09:26:33 Temp$ irb19
Ruby version 1.9.1
irb(main):001:0> require ‘open-uri’
=> true
irb(main):002:0> open(“http://www.google.com”) {|src|
irb(main):003:1* open(“test.htm”,“wb”) {|dst|
irb(main):004:2* dst.write(src.read)
irb(main):005:2> }
irb(main):006:1> }
=> 5337
irb(main):007:0> exit
09:26:54 Temp$ wc test.htm
9 190 5337 test.htm
09:27:02 Temp$

Kind regards

robert