Sha256: f5701024315b14c14ffe8d304fa7f3cbfdf3b8f951a0fccf93ef30abff9ece15

Contents?: true

Size: 1.94 KB

Versions: 10

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

require_dependency "renalware/letters"
require "pdf/reader"

module Renalware
  module Letters
    # This class is both a Wisper listener (subscribing to ApproveLetter events) and an ActiveJob.
    # Should be configured in the broadcast_subscription_map to listen to events from ApproveLetter
    # and be invoked aysnchronously via a background queue ie delayed_job.
    #
    # We generate a PDF version of the letter (or retrieve it from the cache) and inspect it
    # using a PDF tool to get the page count. We then save that away to the letters table.
    #
    # The page count is useful when printing letters; letters with the same page count can be
    # printed together and fed into an envelope stuffer with the same page count setting eg 2
    # (address cover sheet + duplex printed letter). Most of the time page count will be 1,
    # but the page count setting on the printer will be 2 because of the additional address cover
    # sheet.
    class CalculatePageCountJob < ApplicationJob
      pattr_initialize :letter

      # This method is the name of an event raised elsewhere by a Wisper publisher.
      # It needs to be a class method in order to be invoked asynchronously by delayed_job
      def self.letter_approved(letter)
        new(letter).call
      end

      def call
        letter.update_column(:page_count, pdf_reader.page_count)
      rescue StandardError => e
        # In Wisper async jobs, rescue_from blocks defined on the ApplicationJob class
        # do not seem to be called, so we need to invoke them ourselves with this call to a fn on
        # ActiveSupport::Rescuable which is already mixed into ActiveJob::Base.
        rescue_with_handler(e) || raise
      end

      private

      def pdf_reader
        PDF::Reader.new(pdf_data)
      end

      def pdf_data
        letter_presenter = LetterPresenter.new(letter)
        StringIO.new(PdfRenderer.call(letter_presenter))
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
renalware-core-2.1.1 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.1.0 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.167 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.166 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.165 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.164 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.163 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.162 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.161 app/jobs/renalware/letters/calculate_page_count_job.rb
renalware-core-2.0.160 app/jobs/renalware/letters/calculate_page_count_job.rb