Sha256: 083e0d072dc378ef9586704f8194231ddaa23a0c2b728a6ad7a60854300841b9

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

require 'fileutils'
require 'digest/md5'

module Wicoris
  module Postman
    class Copier
      attr_reader :logger

      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))
        logger.debug :message => 'Letter copied',
                     :destination => destination,
                     :letter => @job.letter,
                     :job => @job
      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

6 entries across 6 versions & 1 rubygems

Version Path
wicoris-postman-0.13.0 lib/wicoris/postman/copier.rb
wicoris-postman-0.12.2 lib/wicoris/postman/copier.rb
wicoris-postman-0.12.1 lib/wicoris/postman/copier.rb
wicoris-postman-0.12.0 lib/wicoris/postman/copier.rb
wicoris-postman-0.11.3 lib/wicoris/postman/copier.rb
wicoris-postman-0.11.2 lib/wicoris/postman/copier.rb