My First Ruby Proggie
by David Kowis on Jul.29, 2008, under Ruby
I wrote this to provide me a simple wget type program.
require 'net/http'
require 'uri'
proxy_addr = 'localhost'
proxy_port = 8118
url = URI.parse('http://shlrm.org/baconflowchartvj8.jpg')
Net::HTTP::Proxy(proxy_addr, proxy_port).start(url.host) do |request|
puts "Connected to teh proxy"
# connected to mah proxy
to_save = url.path.split(/\//)[-1]
File.open("c:\\" + to_save, 'wb') do |f|
puts "opened file"
request.request_get(url.path) do |response|
content_length = Integer(response['content-length'])
total_count = 0
response.read_body do |str|
if str.length > 0 then
total_count += str.length
print "Complete: #{total_count} of #{content_length}: "
puts "%4f" % (1.0*total_count/content_length*100)
end
f.write str
end
end
puts "Written file"
end
puts "done connecting"
end
puts "all done"
So that’s it.