-
Notifications
You must be signed in to change notification settings - Fork 4
/
wget.rb
executable file
·49 lines (42 loc) · 1.17 KB
/
wget.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
########################################################################
# wget.rb: Simple Ruby-based File Downloader
#
# Description:
# This Ruby script downloads a file from a given URL and saves it
# locally. It is a basic implementation similar to the wget command.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.1 2023-12-06
# Refactored for improved readability and added detailed comments.
# v1.0 2012-02-29
# Initial release.
#
# Usage:
# ruby wget.rb <URL>
# Example: ruby wget.rb http://example.com/file.txt
#
########################################################################
require 'open-uri'
# Prints the usage message and exits the script
def usage
prog = __FILE__
puts "Usage: #{prog} <URL>"
exit 1
end
# Check if URL is provided
url = ARGV.shift
usage unless url
# Extract filename from URL
filename = url.split(/\//).last
# Download and save the file
open(url) do |source|
open(filename, "w+b") do |o|
o.print source.read
end
end