Class: Transport::LocalFile

Inherits:
Object
  • Object
show all
Defined in:
app/services/transport/local_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LocalFile

Returns a new instance of LocalFile.



6
7
8
9
10
# File 'app/services/transport/local_file.rb', line 6

def initialize(options={})
  @profile = profile
  @options = options
  @logger = options[:logger] || Rails.logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'app/services/transport/local_file.rb', line 4

def logger
  @logger
end

#profileObject (readonly)

Returns the value of attribute profile.



4
5
6
# File 'app/services/transport/local_file.rb', line 4

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



19
20
21
22
23
24
25
26
# File 'app/services/transport/local_file.rb', line 19

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



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/transport/local_file.rb', line 28

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



12
13
14
15
# File 'app/services/transport/local_file.rb', line 12

def list_files(directory, pattern=nil)
  files = Dir.glob("#{directory}/*")
  files.select{|fn| pattern.nil? || File.basename(fn) =~ pattern }
end

#rm(file_path) ⇒ Object



41
42
43
44
45
# File 'app/services/transport/local_file.rb', line 41

def rm(file_path)
  logger.info "Deleting file #{file_path}"
  FileUtils.rm(file_path)
  :deleted
end