Class: LocalFileManager

Inherits:
FileManager show all
Defined in:
lib/local_file_manager.rb

Instance Attribute Summary

Attributes inherited from FileManager

#options

Instance Method Summary collapse

Methods inherited from FileManager

#initialize

Constructor Details

This class inherits a constructor from FileManager

Instance Method Details

#delete_file(file_name) ⇒ Object



48
49
50
51
52
53
# File 'lib/local_file_manager.rb', line 48

def delete_file file_name
  @logger.print "Deleting file \"#{file_name}\" from local folder \"#{root_path}\"..."
  full_file_name = File.join(root_path, file_name)
  File.delete(full_file_name) if File.exists?(full_file_name)
  @logger.puts 'done.'
end

#download_to_temp_file(file_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/local_file_manager.rb', line 26

def download_to_temp_file(file_name)
  @logger.print "Copying local file \"#{file_name}\" to folder \"#{root_path}\"..."
  full_file_name = Pathname(File.join(root_path, file_name))
  Dir.mktmpdir do |dir|
    temp_file = "#{dir}/#{full_file_name.basename}"
    FileUtils.cp(full_file_name, temp_file)
    yield(temp_file)
  end
  @logger.puts 'done.'
end

#list_files(prefix = '', file_extension = '*') ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/local_file_manager.rb', line 37

def list_files(prefix = '', file_extension = '*')
  FileUtils.mkdir_p(root_path)
  @logger.print "Listing \"#{prefix}*.#{file_extension}\" from local folder \"#{root_path}\"..."
  files = Dir[File.join(root_path, '**', prefix, "*.#{file_extension}"),
              File.join(root_path, "#{prefix}*.#{file_extension}")].map do |f|
    f[root_path.size..-1]
  end.uniq
  @logger.puts 'done.'
  files
end

#read_file(file_name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/local_file_manager.rb', line 7

def read_file file_name
  @logger.print "Reading file \"#{file_name}\" from local folder \"#{root_path}\"..."
  full_file_name = File.join(root_path, file_name)
  contents = File.open(full_file_name, 'r:UTF-8') { |f| f.read }
  @logger.puts 'done.'

  contents
rescue Errno::ENOENT
  raise FileNotFoundError.new(full_file_name)
end

#save_file(file_name, file_contents, write_options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/local_file_manager.rb', line 18

def save_file(file_name, file_contents, write_options = {})
  @logger.print "Saving file \"#{file_name}\" to local folder \"#{root_path}\"..."
  full_file_name = Pathname(File.join(root_path, file_name))
  FileUtils.mkdir_p(full_file_name.dirname)
  File.open(full_file_name, 'wb') { |f| f.write(file_contents) }
  @logger.puts 'done.'
end