Sha256: 857b7ef47c0eadbf51e2620a8bd384b369ee97d857f4258cb9e72009332a297e

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'fileutils'
require 'digest/md5'

module Wicoris
  module Postman
    class Copier
      def initialize(job, opts = {})
        @job = job
        @opts = opts
        @logger = opts[:logger]
      end

      # Copy letter to destination.
      def run
        FileUtils.cp(source, destination, :noop => (@opts[:noop] == true))
        if @logger
          msg = @job.to_hash
          msg[:destination] = destination
          msg[:message] = 'Letter delivered :-)'
          @logger.info(msg)
        end
      end

      private

      # @returns [String] Input filename
      def source
        @job.letter
      end

      # @returns [String] Simple fingerprint of input file.
      def fingerprint
        Digest::MD5.hexdigest(File.read(source))[0..3]
      end

      # @returns [String] Full path to output file
      def destination
        File.join(outdir, filename)
      end

      # @returns [String] Validated output directory
      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

      # @returns [String] Output filename
      def filename
        if filename_components.any? { |c| c.nil? || c.empty? }
          raise "Missing patient demographics: #{filename_components}"
        else
          filename_components.join('_') + suffix
        end
      end

      # @returns [Array] Infos to be included in the output filename
      def filename_components
        [
          @job.patient_last_name,
          @job.patient_first_name,
          @job.patient_date_of_birth,
          fingerprint
        ]
      end

      # @returns [String] Suffix for output filename
      def suffix
        '.pdf'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wicoris-postman-0.10.0 lib/wicoris/postman/copier.rb