Class: Transport::LocalFile
- Inherits:
-
Object
- Object
- Transport::LocalFile
- Defined in:
- app/services/transport/local_file.rb
Overview
Service object: local file.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Instance Method Summary collapse
-
#batch_download_data(directory, pattern = nil) ⇒ Object
In one shot returns a hash, hash key is filename, hash value is content of file optional pattern as regexp to match on filename.
- #batch_download_to_file(directory, local_directory = nil, pattern = nil) ⇒ Object
-
#initialize(options = {}) ⇒ LocalFile
constructor
A new instance of LocalFile.
- #list_files(directory, pattern = nil) ⇒ Object
- #rm(file_path) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ LocalFile
Returns a new instance of LocalFile.
8 9 10 11 12 |
# File 'app/services/transport/local_file.rb', line 8 def initialize( = {}) @profile = profile @options = @logger = [:logger] || Rails.logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'app/services/transport/local_file.rb', line 6 def logger @logger end |
#profile ⇒ Object (readonly)
Returns the value of attribute profile.
6 7 8 |
# File 'app/services/transport/local_file.rb', line 6 def profile @profile end |
Instance Method Details
#batch_download_data(directory, pattern = nil) ⇒ Object
In one shot returns a hash, hash key is filename, hash value is content of file
optional pattern as regexp to match on filename
21 22 23 24 25 26 27 28 |
# File 'app/services/transport/local_file.rb', line 21 def batch_download_data(directory, pattern = nil) data = {} list_files(directory, pattern).each do |file_path| logger.info "Local file #{file_path}" data[File.basename(file_path)] = File.read(file_path) end data end |
#batch_download_to_file(directory, local_directory = nil, pattern = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/services/transport/local_file.rb', line 30 def batch_download_to_file(directory, local_directory = nil, pattern = nil) data = {} local_directory ||= Dir.tmpdir list_files(directory, pattern).each do |remote_file_path| file_name = File.basename(remote_file_path) local_file_path = "#{local_directory}/#{file_name}" logger.info "Copying file #{remote_file_path}" FileUtils.cp(remote_file_path, local_file_path) data[file_name] = local_file_path end data end |
#list_files(directory, pattern = nil) ⇒ Object
14 15 16 17 |
# File 'app/services/transport/local_file.rb', line 14 def list_files(directory, pattern = nil) files = Dir.glob("#{directory}/*") files.select { |fn| pattern.nil? || File.basename(fn) =~ pattern } end |
#rm(file_path) ⇒ Object
43 44 45 46 47 |
# File 'app/services/transport/local_file.rb', line 43 def rm(file_path) logger.info "Deleting file #{file_path}" FileUtils.rm(file_path) :deleted end |