Class: RecursiveHTTPFetcher

Inherits:
Object show all
Defined in:
lib/commands/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls_to_fetch, cwd = ".") ⇒ RecursiveHTTPFetcher

Returns a new instance of RecursiveHTTPFetcher.



809
810
811
812
813
# File 'lib/commands/plugin.rb', line 809

def initialize(urls_to_fetch, cwd = ".")
  @cwd = cwd
  @urls_to_fetch = urls_to_fetch.to_a
  @quiet = false
end

Instance Attribute Details

#quietObject

Returns the value of attribute quiet.



808
809
810
# File 'lib/commands/plugin.rb', line 808

def quiet
  @quiet
end

Instance Method Details

#download(link) ⇒ Object



846
847
848
849
850
851
852
853
# File 'lib/commands/plugin.rb', line 846

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) ⇒ Object



855
856
857
858
859
# File 'lib/commands/plugin.rb', line 855

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) ⇒ Object



861
862
863
864
865
866
867
868
# File 'lib/commands/plugin.rb', line 861

def fetch_dir(url)
  push_d(File.basename(url))
  open(url) do |stream|
    contents =  stream.read
    fetch(links(url, contents))
  end
  pop_d
end


836
837
838
839
840
841
842
843
844
# File 'lib/commands/plugin.rb', line 836

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

#lsObject



815
816
817
818
819
820
821
822
823
824
825
# File 'lib/commands/plugin.rb', line 815

def ls
  @urls_to_fetch.collect do |url|
    if url =~ /^svn:\/\/.*/
      `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_dObject



832
833
834
# File 'lib/commands/plugin.rb', line 832

def pop_d
  @cwd = File.dirname(@cwd)
end

#push_d(dir) ⇒ Object



827
828
829
830
# File 'lib/commands/plugin.rb', line 827

def push_d(dir)
  @cwd = File.join(@cwd, dir)
  FileUtils.mkdir_p(@cwd)
end