I want to download a file on the Internet using Ruby and save it locally (with caution)

Task

What if I want to use Ruby to download a file on the Internet and save it locally?

solution

First is an example of a text file. If you want to download Wikipedia --HyperText Markup Language as an HTML file, write as follows.

require 'open-uri'

uri_str = 'https://ja.wikipedia.org/wiki/HyperText_Markup_Language'
URI.open(uri_str) do |res|
  IO.copy_stream(res, 'HyperText_Markup_Language.html')
end

The same is true for binaries like images. Download the image given as an example PNG file in Wikipedia --Portable Network Graphics.

require 'open-uri'

uri_str = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'
URI.open(uri_str) do |res|
  IO.copy_stream(res, 'PNG_transparency_demonstration_1.png')
end

Caution

The ʻopen-uri library is a wrapper such as Net :: HTTP Net :: HTTPS Net :: FTPthat allows you to treat http and https URLs like regular files. This library redefinesKernel # open`, so you can also write:

require 'open-uri'

uri_str = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'
open(uri_str) do |res|
  IO.copy_stream(res, 'PNG_transparency_demonstration_1.png')
end

However, starting with Ruby 2.7, opening URIs using Kernel # open, which is extended by ʻopen-uri`, has been deprecated. When I run the above code on Ruby 2.7, I get the following warning:

warning: calling URI.open via Kernel#open is deprecated, call URI.open directly or use URI#open

It doesn't mean that it doesn't stop just because of the warning, but it is recommended to use ʻURI # open or ʻOpenURI # open_uri.

Environmental information

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin18

Recommended Posts

I want to download a file on the Internet using Ruby and save it locally (with caution)
I want to add a browsing function with ruby on rails
I want to create a Servlet war file with OpenJDK on CentOS7. Without mvn. With no internet connection.
I want to monitor a specific file with WatchService
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I want to hit the API with Rails on multiple docker-composes set up locally
I want to implement it additionally while using kotlin on a site running Java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to create a Parquet file even in Ruby
I want to get a list of the contents of a zip file and its uncompressed size
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
Create a large number of records with one command using the Ruby on Rails seeds.rb file
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
I made an app to scribble with PencilKit on a PDF file
[Ruby] I want to extract only the value of the hash and only the key
If you want to make a zip file with Ruby, it's rubyzip.
UnsupportedClassVersionError was output when I placed the .war file built with Eclipse on EC2 and executed it.
Memorandum: When I tried TensorFlow with Tribuo, it didn't work, so I went on a journey to find the head family and lost.
When installing a gem with C extension in Ruby, I want to finish it quickly using multiple CPU cores like make -j4
If it is Ruby, it is efficient to make it a method and stock the processing.
I want to control the start / stop of servers and databases with Alexa
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
I want to play a GIF image on the Andorid app (Java, Kotlin)
[Ruby] I want to do a method jump!
I want to recursively get the superclass and interface of a certain class
I want to get the value in Ruby
I made a portfolio with Ruby On Rails
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
When I call the file with Class # getResource from the jar file, it becomes Not Found and it is a crappy memorandum
[Ruby] I want to make a program that displays today's day of the week!
I want to go back to a specific VC by tapping the back button on the NavigationBar!
I want to issue a connection when a database is created using Spring and MyBatis
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
[AWS Lambda] Resize the image saved in S3 and save it in another S3 (using Ruby)
How to save a file with the specified extension under the directory specified in Java to the list
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
The CSV file that I was able to download suddenly started to appear on the page.
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I want to use NetBeans on Mac → I can use it!
I want to judge the range using the monthly degree
[Ruby] I made a crawler with anemone and nokogiri.
I want to transition screens with kotlin and java!
21 Load the script from a file and execute it
I want to call the main method using reflection
I want to simplify the log output on Android
I want to add a delete function to the comment function
If hash [: a] [: b] [: c] = 0 in Ruby, I want you to extend it recursively even if the key does not exist.
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to use Java Applet easily on the command line without using an IDE
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to find a relative path in a situation using Path
I want to use screen sharing on the login screen on Ubuntu 18
I want to bring Tomcat to the server and start the application
I want to create a form to select the [Rails] category
Even in Java, I want to output true with a == 1 && a == 2 && a == 3