Sha256: 3df0964cb698a8d41f547f5dd64c6e7f5c45902fdbb650a1c2e505f5b6dcb3c6

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 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') { |request| fh.write request.read }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
menagerie-1.0.0 lib/menagerie/artifact.rb