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.



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

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.



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

def quiet
  @quiet
end

Instance Method Details

#download(link) ⇒ Object



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

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



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

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



908
909
910
911
912
913
914
915
# File 'lib/commands/plugin.rb', line 908

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


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

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



862
863
864
865
866
867
868
869
870
871
872
# File 'lib/commands/plugin.rb', line 862

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



879
880
881
# File 'lib/commands/plugin.rb', line 879

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

#push_d(dir) ⇒ Object



874
875
876
877
# File 'lib/commands/plugin.rb', line 874

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