module IdCardPrinter class QueuedCardsGenerator def initialize(employer_member_keys) @employer_member_keys = Array.wrap(employer_member_keys) @layout = 'PrintCard' @pl_plan_keys = @employer_member_keys.map { |emk| emk["PlanKey"].to_s } end def call white_card_array = [] blue_card_array = [] IdCard::PrintData.where(pl_plan_key: @pl_plan_keys).destroy_all @employer_member_keys.each do |emk| employer = Employer.find_by(pl_plan_key: emk["PlanKey"]) member_keys = emk["MemberKeys"].split(", ").map(&:to_i) IdCardPrinter::EmployerDataFormatter.new(employer, member_keys).call if emk["PlanKey"] == "3" blue_card_array = IdCardPrinter::PdfProcessor.new(employer, @layout).call else white_card_array = IdCardPrinter::PdfProcessor.new(employer, @layout).call end end combine_pdfs(blue_card_array, white_card_array) end private def combine_pdfs(blue_cards, white_cards) if blue_cards.present? && white_cards.present? combined_pdfs = [] blue_filename = "queued_blue_cards_#{Date.today}.pdf" combined_pdfs << { name: blue_filename, data: blue_cards.to_pdf } white_filename = "queued_white_cards_#{Date.today}.pdf" combined_pdfs << { name: white_filename, data: white_cards.to_pdf } output_file = zip_cards(combined_pdfs) elsif blue_cards.present? output_file = CombinePDF.new blue_cards.each { |pdf| output_file << pdf } elsif white_cards.present? output_file = CombinePDF.new white_cards.each { |pdf| output_file << pdf } end output_file end def zip_cards(pdf_array) Zip::OutputStream.write_buffer do |zio| pdf_array.each do |file| zio.put_next_entry(file[:name]) zio.write(file[:data]) end end end end end