Sha256: 6f7c326571bbb4ca8bd2a3e5aefbe4b1926d7d893c3b43dba2d30f61c1b19fda

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module Wicoris
  module Postman
    class FaxMachine
      DIALOUT_PREFIX = '0'
      VALID_PHONE_NUMBER = %r{
        ^0       # 1st char has to be a zero
        [1-9]{1} # 2nd char has to be a digit execpt zero
        \d{6,}$  # followed by at least 6 other digits
      }x

      attr_reader :logger

      def initialize(job, opts = {})
        @job = job
        @opts = opts
        @logger = opts[:logger]
      end

      # Actually fax the letter.
      def run
        Mixlib::ShellOut.new(command).run_command unless @opts[:noop]
        logger.debug :message => 'Letter faxed',
                     :phone => validated_phone,
                     :letter => @job.letter,
                     :job => @job
      end

      # @returns [String] Validated phone number.
      def validated_phone
        raise ArgumentError, 'Missing phone number' unless @job.phone
        phone = @job.phone.gsub(/(\s|-)+/, '')
        if phone =~ VALID_PHONE_NUMBER
          DIALOUT_PREFIX + phone
        else
          raise ArgumentError, "Invalid phone number: #{phone}"
        end
      end

      private

      # Return the command-line for sending the fax, i.e.:
      #
      #   lp -d Fax -o phone=042 "/tmp/foo.pdf"
      #
      # @returns [String] command-line
      def command
        cmd = %w(lp -d Fax) # TODO: Replace hard-coded fax printer-name 'Fax'!
        cmd << "-o phone=#{validated_phone}"
        cmd << "'#{@job.letter}'"
        cmd.join(' ')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wicoris-postman-0.13.0 lib/wicoris/postman/fax_machine.rb
wicoris-postman-0.12.2 lib/wicoris/postman/fax_machine.rb