Module: BagIt::Fetch

Included in:
Bag
Defined in:
lib/bagit/fetch.rb

Instance Method Summary collapse

Instance Method Details

#add_remote_file(url, path, size, sha1, md5) ⇒ Object



11
12
13
14
15
# File 'lib/bagit/fetch.rb', line 11

def add_remote_file(url, path, size, sha1, md5)
  File.open(fetch_txt_file, "a") { |io| io.puts "#{url} #{size || "-"} #{path}" }
  File.open(manifest_file("sha1"), "a") { |io| io.puts "#{sha1} #{File.join "data", path}" }
  File.open(manifest_file("md5"), "a") { |io| io.puts "#{md5} #{File.join "data", path}" }
end

#fetch!Object

feth all remote files



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bagit/fetch.rb', line 18

def fetch!
  File.open(fetch_txt_file) do |io|
    io.readlines.each do |line|
      (url, _length, path) = line.chomp.split(/\s+/, 3)

      add_file(path) do |file_io|
        file_io.write URI.open(url)
      end
    end
  end

  rename_old_fetch_txt(fetch_txt_file)
  move_current_fetch_txt(fetch_txt_file)
end

#fetch_txt_fileObject



7
8
9
# File 'lib/bagit/fetch.rb', line 7

def fetch_txt_file
  File.join @bag_dir, "fetch.txt"
end

#move_current_fetch_txt(fetch_txt_file) ⇒ Object



42
43
44
# File 'lib/bagit/fetch.rb', line 42

def move_current_fetch_txt(fetch_txt_file)
  FileUtils.mv fetch_txt_file, "#{fetch_txt_file}.0"
end

#rename_old_fetch_txt(fetch_txt_file) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/bagit/fetch.rb', line 33

def rename_old_fetch_txt(fetch_txt_file)
  Dir["#{fetch_txt_file}.?*"].sort.reverse_each do |f|
    if f =~ /fetch.txt.(\d+)$/
      new_f = File.join File.dirname(f), "fetch.txt.#{Regexp.last_match(1).to_i + 1}"
      FileUtils.mv f, new_f
    end
  end
end