Sha256: a5de5d4ac3ed7838cb8977d3758fb9111c98645368b840f78db46326f7b6c6e8

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'open-uri'

module Menagerie
  ##
  # Artifacts are a stored file for a particular version of a project
  class Artifact
    attr_reader :name, :version

    def initialize(params = {})
      @options = params
      @logger = @options[:logger] || Menagerie.get_logger
      @options[:artifact] ? create : parse
    end

    def path
      @path ||= "#{@options[:paths][:artifacts]}/#{@name}/#{@version}"
    end

    private

    def create
      artifact = @options[:artifact]
      @name, @version = artifact.values_at(:name, :version)
      @logger.info "Downloading artifact: #{path}"
      download(artifact[:url], path) unless File.exist? path
      File.chmod(artifact[:mode], path) if artifact[:mode]
    end

    def parse
      @name = File.basename @options[:path]
      @version = File.basename File.readlink(@options[:path])
    end

    def download(url, path)
      FileUtils.mkdir_p File.dirname(path)
      File.open(path, 'wb') do |fh|
        open(url, 'rb') do |request| # rubocop:disable Security/Open
          fh.write request.read
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
menagerie-1.1.1 lib/menagerie/artifact.rb
menagerie-1.1.0 lib/menagerie/artifact.rb