Module: Dl

Defined in:
lib/dl.rb,
lib/dl/dl.rb

Constant Summary collapse

VERSION =
"1.2.4"
@@be_quiet =
false
@@file =
""

Class Method Summary collapse

Class Method Details

.be_quietObject



19
20
21
# File 'lib/dl.rb', line 19

def self.be_quiet
  @@be_quiet
end

.be_quiet=(value) ⇒ Object



23
24
25
# File 'lib/dl.rb', line 23

def self.be_quiet=(value)
  @@be_quiet = value
end

.download(url, raw = false) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dl/dl.rb', line 2

def self.download(url, raw = false)
  if !(url =~ /^https?:/i)
    parsed_url = "http://" + url
  end

  output "Downloading #{url}..."
  parsed_url = URI.parse(parsed_url)
  req = Net::HTTP::Get.new(parsed_url.path.empty? ? '/' : parsed_url.path)
  res = Net::HTTP.start(parsed_url.host, parsed_url.port) {|http|
    http.request(req)
  }

  output "#{url} downloaded to the file #{url.split("/").last}"

  if raw == true
    puts res.body
  else
    File.new(url.split("/").last, "w").write(res.body)
  end
end

.fileObject



11
12
13
# File 'lib/dl.rb', line 11

def self.file
  @@file
end

.file=(value) ⇒ Object



15
16
17
# File 'lib/dl.rb', line 15

def self.file=(value)
  @@file = value
end