Sha256: 9f91b90b8d7f4ad18d75e57f2e12cf72270ebb4801e06362c68ae7bf2fd02603

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require "seven_zip_ruby"
require "zip"

module Decidim
  module Votings
    module Census
      # Public: Generates a 7z(seven zip) file with data files ready to be persisted
      # somewhere so users can download their data.
      #
      # The 7z file wraps a ZIP file which finally contains the data files.
      class AccessCodesExporter
        include TranslatableAttributes

        FILE_NAME_PATTERN = "%{voting_name}-voting-access-codes.csv"

        attr_reader :dataset, :path, :password

        # Public: Initializes the class.
        #
        # dataset       - The Voting::Census::Dataset to export the access codes for.
        # path          - The String path where to write the zip file.
        # password      - The password to protect the zip file.
        def initialize(dataset, path, password)
          @dataset = dataset
          @path = File.expand_path path
          @password = password
        end

        def export
          dirname = File.dirname(@path)
          FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
          File.open(@path, "wb") do |file|
            SevenZipRuby::Writer.open(file, password:) do |szw|
              szw.header_encryption = true
              szw.add_data(csv_data.read, format(FILE_NAME_PATTERN, voting_name: translated_attribute(dataset.voting.title).parameterize))
            end
          end
        end

        private

        def csv_data
          Decidim::Exporters::CSV.new(dataset.data, Decidim::Votings::Census::DatumSerializer).export
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-elections-0.28.2 app/services/decidim/votings/census/access_codes_exporter.rb
decidim-elections-0.28.1 app/services/decidim/votings/census/access_codes_exporter.rb
decidim-elections-0.28.0 app/services/decidim/votings/census/access_codes_exporter.rb
decidim-elections-0.28.0.rc5 app/services/decidim/votings/census/access_codes_exporter.rb
decidim-elections-0.28.0.rc4 app/services/decidim/votings/census/access_codes_exporter.rb