Sha256: 7900eeb807f7ee029a2d6b477d287868512d928f213d5e2a1af2f4b30c14dff9

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require 'shared/puppet_forge/v3'
require 'shared/puppet_forge/connection'

module PuppetForge
  module V3
    # Access metadata and downloads for a specific module release.
    class ModuleRelease

      include PuppetForge::Connection

      # @!attribute [r] full_name
      #   @return [String] The hyphen delimited name of this module
      attr_reader :full_name

      # @!attribute [r] version
      #   @return [String] The version of this module
      attr_reader :version

      # @param full_name [String] The name of the module, will be normalized
      #   to a hyphen delimited name.
      # @param version [String]
      def initialize(full_name, version)
        @full_name = PuppetForge::V3.normalize_name(full_name)
        @version   = version
      end

      # @return [Hash] The complete Forge resposne for this release.
      def data
        @data ||= conn.get(resource_url).body
      end

      # @return [String] The unique identifier for this module release.
      def slug
        "#{full_name}-#{version}"
      end

      # Download this module release to the specified path.
      #
      # @param path [Pathname]
      # @return [void]
      def download(path)
        resp = conn.get(file_url)
        path.open('wb') { |fh| fh.write(resp.body) }
      end

      # Verify that a downloaded module matches the checksum in the metadata for this release.
      #
      # @param path [Pathname]
      # @return [void]
      def verify(path)
        expected_md5 = data['file_md5']
        file_md5     = Digest::MD5.file(path).hexdigest
        if expected_md5 != file_md5
          raise ChecksumMismatch.new("Expected #{path} checksum to be #{expected_md5}, got #{file_md5}")
        end
      end

      private

      def file_url
        "/v3/files/#{slug}.tar.gz"
      end

      def resource_url
        "/v3/releases/#{slug}"
      end

      class ChecksumMismatch < StandardError

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
r10k-2.0.1 lib/shared/puppet_forge/v3/module_release.rb
r10k-2.0.0 lib/shared/puppet_forge/v3/module_release.rb