Sha256: 82f69b06100eaec62b5aac1c7889af7553d87329b3678ae68a40494bcf19221e

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

module Digicert
  module CLI
    class CertificateDownloader
      def initialize(certificate_id:, path:, **options)
        @path = path
        @options = options
        @certificate_id = certificate_id
        @filename = options.fetch(:filename, certificate_id)
      end

      def download
        if certificate_contents
          write_certificate_contents(certificate_contents)
        end
      end

      def self.download(attributes)
        new(attributes).download
      end

      private

      attr_reader :certificate_id, :path, :filename, :options

      def certificate_contents
        @certificate_contents ||=
          Digicert::CertificateDownloader.fetch_content(certificate_id)
      end

      def write_certificate_contents(contents)
        say("Downloaded certificate to:")

        write_to_path("root", contents[:root_certificate])
        write_to_path("certificate", contents[:certificate])
        write_to_path("intermediate", contents[:intermediate_certificate])
      end

      def write_to_path(key, content)
        certificate_name = [filename, key, "crt"].join(".")
        certificate_path = [path, certificate_name].join("/")

        say(certificate_path)
        File.open(certificate_path, "w") { |file| file.write(content) }
      end

      def say(message)
        Digicert::CLI::Util.say(message)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
digicert-cli-0.5.2 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.5.1 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.5.0 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.4.2 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.4.1 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.4.0 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.3.0 lib/digicert/cli/certificate_downloader.rb
digicert-cli-0.2.0 lib/digicert/cli/certificate_downloader.rb