class Wicoris::Postman::Copier

Attributes

logger[R]

Public Class Methods

new(job, opts = {}) click to toggle source
# File lib/wicoris/postman/copier.rb, line 9
def initialize(job, opts = {})
  @job = job
  @opts = opts
  @logger = opts[:logger]
end

Public Instance Methods

run() click to toggle source

Copy letter to destination.

# File lib/wicoris/postman/copier.rb, line 16
def run
  FileUtils.cp(source, destination, :noop => (@opts[:noop] == true))
  logger.debug :message => 'Letter copied',
               :destination => destination,
               :letter => @job.letter,
               :job => @job
end

Private Instance Methods

destination() click to toggle source

@returns [String] Full path to output file

# File lib/wicoris/postman/copier.rb, line 37
def destination
  File.join(outdir, filename)
end
filename() click to toggle source

@returns [String] Output filename

# File lib/wicoris/postman/copier.rb, line 55
def filename
  if filename_components.any? { |c| c.nil? || c.empty? }
    raise "Missing patient demographics: #{filename_components}"
  else
    filename_components.join('_') + suffix
  end
end
filename_components() click to toggle source

@returns [Array] Infos to be included in the output filename

# File lib/wicoris/postman/copier.rb, line 64
def filename_components
  [
    @job.patient_last_name,
    @job.patient_first_name,
    @job.patient_date_of_birth,
    fingerprint
  ]
end
fingerprint() click to toggle source

@returns [String] Simple fingerprint of input file.

# File lib/wicoris/postman/copier.rb, line 32
def fingerprint
  Digest::MD5.hexdigest(File.read(source))[0..3]
end
outdir() click to toggle source

@returns [String] Validated output directory

# File lib/wicoris/postman/copier.rb, line 42
def outdir
  dir = @opts[:outdir]
  raise 'No output directory given' unless dir
  raise "Output directory does not exist: #{dir}" unless
    File.exists? dir
  raise "Output directory is no directory: '#{dir}'" unless
    File.directory? dir
  raise "Output directory not writable: '#{dir}'" unless
    File.writable? dir
  @opts[:outdir]
end
source() click to toggle source

@returns [String] Input filename

# File lib/wicoris/postman/copier.rb, line 27
def source
  @job.letter
end
suffix() click to toggle source

@returns [String] Suffix for output filename

# File lib/wicoris/postman/copier.rb, line 74
def suffix
  '.pdf'
end