Module: Net
- Defined in:
- lib/darkext/net.rb
Class Method Summary collapse
Class Method Details
.download(url, limit = 10) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/darkext/net.rb', line 4 def self.download(url,limit = 10) raise ArgumentError, 'HTTP redirect too deep' if limit == 0 #url = URI.parse(url) #req = Net::HTTP::Get.new(url.path) #req['User-Agent'] = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009030422 Ubuntu/8.10 (intrepid) Firefox/3.0.7" #resp = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) } resp = Net::HTTP.get_response(URI.parse(url)) case resp when Net::HTTPSuccess then resp when Net::HTTPRedirection then download(resp['location'], limit - 1) else resp.error! end end |
.download_and_save(url, path = nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/darkext/net.rb', line 18 def self.download_and_save(url,path = nil) if path.nil? path = File.(url.split('/').last) else path = File.(path) end raise ArgumentError.new('Save path is a directory') if File.directory?(path) resp = download(url) open(path,'w') { |file| file.write(resp.body) } if resp.is_a?(Net::HTTPSuccess) end |