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, level = 1, cwd = ".") ⇒ RecursiveHTTPFetcher

Returns a new instance of RecursiveHTTPFetcher.



884
885
886
887
888
889
# File 'lib/commands/plugin.rb', line 884

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

Instance Attribute Details

#quietObject

Returns the value of attribute quiet.



883
884
885
# File 'lib/commands/plugin.rb', line 883

def quiet
  @quiet
end

Instance Method Details

#download(link) ⇒ Object



923
924
925
926
927
928
929
930
# File 'lib/commands/plugin.rb', line 923

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



932
933
934
935
936
# File 'lib/commands/plugin.rb', line 932

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



938
939
940
941
942
943
944
945
946
947
# File 'lib/commands/plugin.rb', line 938

def fetch_dir(url)
  @level += 1
  push_d(File.basename(url)) if @level > 0
  open(url) do |stream|
    contents =  stream.read
    fetch(links(url, contents))
  end
  pop_d if @level > 0
  @level -= 1
end


912
913
914
915
916
917
918
919
920
921
# File 'lib/commands/plugin.rb', line 912

def links(base_url, contents)
  links = []
  contents.scan(/href\s*=\s*\"*[^\">]*/i) do |link|
    link = link.sub(/href="/i, "")
    next if link =~ /svnindex.xsl$/
    next if link =~ /^(\w*:|)\/\// || link =~ /^\./
    links << File.join(base_url, link)
  end
  links
end

#lsObject



891
892
893
894
895
896
897
898
899
900
901
# File 'lib/commands/plugin.rb', line 891

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



908
909
910
# File 'lib/commands/plugin.rb', line 908

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

#push_d(dir) ⇒ Object



903
904
905
906
# File 'lib/commands/plugin.rb', line 903

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