Marty
Marty

Reputation: 2224

How to load file from the Internet into a method?

I'm trying to write a method that compares the content of two files. How can I load an external url / Internet file?

One of the files is in my app and I load it using the line below, which seems to work.

file1 = File.open('app/assets/files/example.html')

But the second file is on the Internet. The line below to load the file fails (error: No such file or directory @ rb_sysopen). How could I load the Internet file/page for my comparison method?

file2 = File.open('http://www.example.com/example.html')

Upvotes: 2

Views: 496

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

You can easily achieve this with OpenURI.

require 'open-uri'

file2 = open('http://www.example.com/example.html')

Alternatively, you can pass in an URI

file2 = open(URI.parse('http://www.example.com/example.html'))

Upvotes: 4

Related Questions