class RecursiveHTTPFetcher
Attributes
quiet[RW]
Public Class Methods
new(urls_to_fetch, cwd = ".")
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 6 def initialize(urls_to_fetch, cwd = ".") @cwd = cwd @urls_to_fetch = urls_to_fetch.to_a @quiet = false end
Public Instance Methods
download(link)
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 43 def download(link) puts "+ #{File.join(@cwd, File.basename(link))}" unless @quiet open(link) do |stream| File.open(File.join(@cwd, File.basename(link)), "wb") do |file| file.write(stream.read) end end end
fetch(links = @urls_to_fetch)
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 52 def fetch(links = @urls_to_fetch) links.each do |l| (l =~ /\/$/ || links == @urls_to_fetch) ? fetch_dir(l) : download(l) end end
fetch_dir(url)
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 58 def fetch_dir(url) push_d(File.basename(url)) open(url) do |stream| contents = stream.read fetch(links(url, contents)) end pop_d end
links(base_url, contents)
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 33 def links(base_url, contents) links = [] contents.scan(/href\s*=\s*\"*[^\">]*/i) do |link| link = link.sub(/href="/i, "") next if link =~ /^http/i || link =~ /^\./ links << File.join(base_url, link) end links end
ls()
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 12 def ls @urls_to_fetch.collect do |url| if url =~ /^svn:\/\/.*/ %x`svn ls #{url}`.split("\n").map {|entry| "/#{entry}"} rescue nil else open(url) do |stream| links("", stream.read) end rescue nil end end.flatten end
pop_d()
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 29 def pop_d @cwd = File.dirname(@cwd) end
push_d(dir)
click to toggle source
# File lib/commands/plugin/recursive_http_fetcher.rb, line 24 def push_d(dir) @cwd = File.join(@cwd, dir) FileUtils.mkdir_p(@cwd) end