Class: RecursiveHTTPFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/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.



481
482
483
484
485
486
# File 'lib/rails/commands/plugin.rb', line 481

def initialize(urls_to_fetch, level = 1, cwd = ".")
  @level = level
  @cwd = cwd
  @urls_to_fetch = RUBY_VERSION >= '1.9' ? urls_to_fetch.lines : urls_to_fetch.to_a
  @quiet = false
end

Instance Attribute Details

#quietObject

Returns the value of attribute quiet.



480
481
482
# File 'lib/rails/commands/plugin.rb', line 480

def quiet
  @quiet
end

Instance Method Details

#download(link) ⇒ Object



520
521
522
523
524
525
526
527
# File 'lib/rails/commands/plugin.rb', line 520

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



529
530
531
532
533
# File 'lib/rails/commands/plugin.rb', line 529

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



535
536
537
538
539
540
541
542
543
544
# File 'lib/rails/commands/plugin.rb', line 535

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


509
510
511
512
513
514
515
516
517
518
# File 'lib/rails/commands/plugin.rb', line 509

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



488
489
490
491
492
493
494
495
496
497
498
# File 'lib/rails/commands/plugin.rb', line 488

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



505
506
507
# File 'lib/rails/commands/plugin.rb', line 505

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

#push_d(dir) ⇒ Object



500
501
502
503
# File 'lib/rails/commands/plugin.rb', line 500

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